6

I'm running the Docker community edition on MacOS BigSur (11.2.2), and am trying to get into the virtual environment.

This article from 2018 says to do

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

and this one from February 2020 says

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

But neither of those things seem to work in my current install.

$docker --version Docker version 20.10.5, build 55c4c88

philolegein
  • 1,099
  • 10
  • 28

1 Answers1

12

The methods you have found are backdoors for entering in the virtual machine, and they change when the releases are changing, and both mentioned methods are no longer supported on the latest Docker-for-mac.

The most canonical way to get terminal access to the virtual machine (create a sh process in the virtual machine and get tty from it), you need the following command.

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

This approach will create a container and will join it to the namespace of the host, after which it will create a new shell in the namespace of the init (pid 1) by executing the nsenter command. This will not change much with the later releases since it relies on stabile docker features to get the access to the vm. In the example I had used debian, but you can replace this with any image that has nsenter (ex. alpine, busybox, etc.)

Also, you can get access trough the current debug socket which will create a shell directly in the virtual machine and connect to it. This is more a backdor created for debugging and might be removed/changed in future releases.

stty -echo -icanon && nc -U ~/Library/Containers/com.docker.docker/Data/debug-shell.sock && stty sane
jordanvrtanoski
  • 5,104
  • 1
  • 20
  • 29
  • 1
    The second answer got me in. The first ("most canonical") wanted to have debian (`Unable to find image 'debian:latest' locally latest: Pulling from library/debian`). Once I got in using the second method, `uname -a` says `Linux docker-desktop 4.19.121-linuxkit #1 SMP Thu Jan 21 15:36:34 UTC 2021 x86_64 Linux`. Should I use a different arg than debian in the first method? – philolegein Mar 17 '21 at 10:49
  • You can use `docker pull debian` to get the image. In fact, you can use any image that has the `nsenter`. It works with `alpine:latest` as well . I had updated the answer to clarify this part. – jordanvrtanoski Mar 17 '21 at 11:01
  • 1
    For those curious about that `nsenter` command, expanded out it is `nsenter --target 1 --mount --uts --net --ipc`, i.e., enter the disk and network and ipc namespaces for pid 1. UTS ‘UNIX Time-Sharing’ refers to the [hostname and domain name](https://en.wikipedia.org/wiki/Linux_namespaces#UTS). `--target=1 --all` seems to work as well for me. – andrewdotn Jan 20 '23 at 13:32