Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Getting a Shell in the Docker for Mac Moby VM

2018 Update: Easiest option is Justin's repo and image

Just run this from your Mac terminal and it'll drop you in a container with full permissions on the Moby VM. This also works for Docker for Windows for getting in Moby Linux VM (doesn't work for Windows Containers).

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

more info:

Option 1: use Screen (not as easy as nsenter)

Note this isn't a list of commands to run in order. The first one gets you in the VM (hit return twice to see a prompt). Then other commands are for managing that connection. Not a great CLI expirence but gets the job done. Using the ctrl- options prevents garbled text on reconnect.

connect to tty on Docker for Mac VM

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

disconnect that session but leave it open in background

Ctrl-a d

list that session that's still running in background

screen -ls

reconnect to that session (don't open a new one, that won't work and 2nd tty will give you garbled screen)

screen -r

kill this session (window) and exit

Ctrl-a k

Option 2 (easier): Use nsenter in priviledged container

docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh

Phil Estes (Docker Maintainer) says:

it’s running a container (using the debian image..nothing special about it other than it apparently has nsenter installed), with pid=host (so you are in the process space of the mini VM running Docker4Mac), and then nsenter says “whatever is pid 1, use that as context, and enter all the namespaces of that, and run a shell there"

Or even easier, from Justin Cormack (Docker Maintainer)

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

Justin Says:

Personally I mostly use screen, but then I also use the above too. That's my minimal nsenter image.


This comment has been minimized.

Copy link

ssx commented Mar 12, 2017

Just a quick note, if you've created the machine with docker-machine, you can ssh in using docker-machine ssh (that name can be found by docker-machine ls)


This comment has been minimized.

Copy link

Zaijo commented Jul 13, 2017

Hello, thanks for sharing. What version of Docker are you useing here? I have

Version 17.06.0-ce-mac18 (18433)
Channel: stable

And I get some strange results when running screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty it just opens my ordinary screen empty windows.


This comment has been minimized.

Copy link

sandinmyjoints commented Oct 20, 2017

@Zaijo because you're connecting to a tty, not logging in, you won't see a prompt initially. Try typing a command and hitting enter -- you'll see the output and then a new prompt.


This comment has been minimized.

Copy link

webguywalker commented Aug 1, 2018

💯 legit


This comment has been minimized.

Copy link

jzelinskie commented Sep 25, 2018

the tty for screen got moved to $HOME/Library/Containers/com.docker.docker/Data/vms/0/tty


This comment has been minimized.

Copy link

rfay commented Jan 20, 2019

the tty for screen got moved to $HOME/Library/Containers/com.docker.docker/Data/vms/0/tty

@BretFisher could you please update the body to account for the move of the tty? It will save people debugging it or giving up and then reading down to - Thanks!


This comment has been minimized.

Copy link

vocatan commented Feb 22, 2019

The 2nd method to attach, using docker run -it --rm --privileged --pid=host justincormack/nsenter1 "just works" reliably.

I've observed that when using screen to attach to the tty, occasionally the output will get corrupted. In this case, I can't seem to reset it back using the normal 'unix-y' tricks like 'reset', 'stty sane', 'Ctl-a Z', or such. From the output of pstree, and typing the command 'sleep 100', I can see that my commands are making it through, just the output being somehow not rendered.

Curious if there is anyone knew the cause for this -- but happy to have this universal solution


This comment has been minimized.

Copy link

mkhafaga commented Apr 18, 2019

@vocatan: The output gets corrupted because you opened different screen sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.