How to change the swap size for Docker on macOS

Long time, no post. That's what we call holidays. Anyways, here's ho to increase the size of swap available in Docker. When you try to set up an OracleDB inside a docker container, you'll probably need this.

Note: This works only with the native virtualization version of Docker for Mac. If you still use boot2docker, you need to follow the guide in this issue.

Note: This guide was updated in February 2018 because Docker has changed the underlying vm. (more info)

  1. Open any terminal, make sure that screen is installed (use Homebrew)
  2. Connect to the Docker VM: screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
  3. You might need to press enter or login to show the prompt. The username is root and the password is empty.
  4. Create a new swapfile: dd if=/dev/zero of=/var/lib/swapfile count=2000000 bs=1024
  5. Change the permissions and run mkswap: chmod go= /var/lib/swapfile && mkswap /var/lib/swapfile
  6. Activate the swap: swapon -v /var/lib/swapfile
  7. Press ctrl+a d to detach the screen session

Note that you'll lose the swap, when the machine is recreated or you restart Docker.

In this case you'll just need to repeat steps two and six to eight, because the swapfile is still present. (This doesn't apply, if you completely recreated the Docker machine)