Enter Docker VM on MacOS Catalina (SSH, xhyve)

EDIT 2022:

The best way to enter the Docker VM as of 2022 is to use:

docker run -it --rm --privileged --pid=host justincormack/nsenter1


As you might know, Docker containers need a Linux kernel in order to run (for Linux containers that is). So how does this work on a Mac, then, it doesn’t have a Linux kernel? Docker Desktop for MacOS will install a small Linux OS on your Mac using the MacOS-built-in xhyve hypervisor.

So if you are curious and want to poke around in Docker, you’ll need to enter the VM. Say you want to run ps on the host in order to see the individual containers running as processes, to watch how Docker images are stored or as I will show here: to see how containers are using overlay2 to merge the container filesystem with the underlying image filesystem then run this on your Mac after you started a container to get the container path:

docker inspect --format='{{.GraphDriver.Data.UpperDir}}' <container>

Now switch to the Linux VM:

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Then cd into the path returned by inspect. You will see the changes the container made into its filesystem or run ps to see the “real” PID of the container process.

In order to exit the VM again hit CTRL-A CTRL-\ and then y to confirm the exit.

About Grischa Ekart

Follow me on Twitter: @gekart. I am a trainer and consultant for AWS, Docker, Kubernetes, Machine Learning and all things DevOps.
This entry was posted in Development, DevOps, Operating systems and tagged , , , . Bookmark the permalink.