226

I´m looking for the folder /var/lib/docker on my Mac after installing docker for Mac.

With docker info I get

    Containers: 5
     ...
    Server Version: 1.12.0-rc4
    Storage Driver: aufs
     Root Dir: /var/lib/docker/aufs
     Backing Filesystem: extfs
     Dirs: 339
     Dirperm1 Supported: true
    ...
    Name: moby
    ID: LUOU:5UHI:JFNI:OQFT:BLKR:YJIC:HHE5:W4LP:YHVP:TT3V:4CB2:6TUS
    Docker Root Dir: /var/lib/docker
    Debug Mode (client): false
    ....

But I don´t have a directory /var/lib/docker on my host.

I have checked /Users/myuser/Library/Containers/com.docker.docker/ but couldn´t find anything there. Any idea where it is located?

Bax
  • 4,260
  • 5
  • 43
  • 65
christian
  • 9,412
  • 10
  • 41
  • 51
  • 13
    It is located inside the vm that docker instantiates to provide the necessary Linux environment. – larsks Jul 22 '16 at 17:50
  • 1
    Could it be that File: /Users/myuser/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 – christian Jul 22 '16 at 17:52
  • 1
    That is the disk image underlying the vm, yes. But if you want to muck about with the contents of that directory you would need to log into the vm itself. You can find instructions for doing so at https://docs.docker.com/v1.8/installation/mac/ – larsks Jul 22 '16 at 17:55
  • thx, risizing this file works with qemu-img resize Docker.qcow2 +5g https://forums.docker.com/t/consistently-out-of-disk-space-in-docker-beta/9438 – christian Jul 22 '16 at 17:59
  • Do you know where to find this directory?@christian – Jason Xu Aug 02 '16 at 09:26
  • @jason not really, i think it´s embedded in /Users/myuser/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-‌​linux/Docker.qcow2 – christian Aug 02 '16 at 09:32

13 Answers13

180

As mentioned in the above answers, you will find it in:
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Once you get the tty running you can navigate to /var/lib/docker

okandas
  • 11,570
  • 2
  • 15
  • 17
Mik jagger
  • 2,052
  • 2
  • 12
  • 18
  • 4
    one slight modification (below your home, not below root): "screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty" – pdenti Jan 01 '17 at 18:12
  • 2
    Works! But how do I exit from that shell? Ctrl + d, `exit`, and `logout` won't work. – thomaskonrad Mar 30 '18 at 21:23
  • 6
    You can detach from the screen with ctrl + a + d – Mik jagger Apr 02 '18 at 10:20
  • 13
    Detaching will still keep the screen session running. You can see this by running `screen -ls`. If you have already detached, run `screen -X quit`. If you have multiple screen sessions, you'll need `-S` to specify the session name. If you haven't detached do press ctrl + a, :, q, u, i, t, ENTER. Those two just run the screen quit command in the session. – dosentmatter Apr 04 '18 at 09:09
  • 19
    I think it may be located at ~/Library/Containers/com.docker.docker/Data/vms/0/tty now. – Richard Collette Aug 03 '18 at 03:03
  • 1
    @RichardCollette, on my machine, both are just symlinks to /dev/ttys000 – dosentmatter Aug 21 '18 at 21:50
  • 1
    @Mikjagger after running the command, it outputs [screen is terminating] every time. Cannot get into the docker vm. And just like "@doesntmatter" said, tty file has a line "/dev/ttys000 " – HKIT Aug 09 '20 at 04:35
  • 1
    For me, when trying to `screen`, it would just exit immediately. `ls -l` showed the `tty` to be a symlink to `/dev/ttys013`, but that mount point didn't exist.. Instead I used the command from https://gist.github.com/BretFisher/5e1a0c7bcca4c735e716abf62afad389 to get a shell into the docker vm: `docker run -it --rm --privileged --pid=host justincormack/nsenter1` – mowwwalker Nov 09 '20 at 06:58
  • 13
    I think this was the answer. But I'm using v20.10 now, theres nothing `~/Library/Containers/com.docker.docker/Data/vms/0/tty` directory. – Tony Chou Jan 26 '21 at 16:32
  • 1
    This answer is outdated. As of 13 August 2021, the answer by RicarHincapie is correct. Keep scrolling until you find it. – Erick G. Hagstrom Aug 13 '21 at 21:51
  • 1
    No such paths on Mac M1 Moterey am afraid though voumes can be previewed via Docker app's Dashboard on Macs or Visual Code Studio with a docker extention installed (left hand side menu). I'd rather have direct linux access although this shouldn't be a problem on proper Linux UAT or PRODuction hosts under the default /var/lib/docker/volumes path. Since it's only affecting macOS which is purely dev in my case so no need to back up databases. – webcoder.co.uk Nov 18 '21 at 11:16
105

As of 2021 is the dance going, Mac Users get easily to the VM with the documented methods, and hence to the volumes.

There's a way Rocky Chen found to get inside the VM in Mac. With this you can actually inspect the famous /var/lib/docker/volumes.

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

Let examine the method:

  • -it goes for Keep STDIN open even if not attached + Allocate pseudo-TTY
  • --privileged "gives all capabilities to the container. Allows special cases like running docker" .
  • --pid defines to use the host VM namespace.
  • debian the actual image to use.
  • nsenter a debian's tool to run programs in different namespaces
  • -t is the target PID
  • -m mount the provided PID namespace.
  • -u enter the Unix Time Sharing (UTS) namespace.
  • -n enter the provided PID network namespace.
  • -i enter the provided PID IPC namespace.

Once run, go to /var/lib/docker/volumes/and you'll find your volumes.

The next question to address for me is:

How to take those volumes and back them up in the host?

I appreciate ideas in the comments!

UPDATE FOR VSCODE USERS

If you downloaded the Official Docker extension, sun will shine for you.

Docker Extension

Just inspect the volumes in Visual Studio Code. Right-click the files you want to have in your local, and download them. That easy!

2nd UPDATE

As of July 2021, Docker Desktop for Mac is announcing we will be able to access volumes directly from the GUI, but only for Pro and Team accounts.

enter image description here

RicHincapie
  • 3,275
  • 1
  • 18
  • 30
  • Also works for adding files to the volume. After opening the volume through "Inspect in Visual Studio Code", I drag-and-drop'd files into the volume – Joshua Swain Apr 20 '21 at 19:34
  • ***When in VS Code, right on the volume and select "Explore in a remote container" – Joshua Swain Apr 20 '21 at 21:54
  • 1
    I was able to browse the volume using the `debian nsenter` approach, the VSCode plugin works like a charm, and the Docker Desktop wants me to upgrade to Pro or Team plan. As of this moment, this is **The Correct Answer**. – Erick G. Hagstrom Aug 13 '21 at 22:13
  • I have docker compose running, it uses volumes as well. However this tab only loads for eternity. No single volume i loaded. – Fusion Sep 29 '21 at 14:51
  • I could not edit any files with this unfortunately. I tried to install nano with `apk update` but it says the files are read only – Ari Seyhun Dec 09 '21 at 13:55
  • 2
    For backup: `docker run -v :/volume --rm loomchild/volume-backup backup - > .tar.bz2` – elquimista Dec 25 '21 at 16:31
  • For restore: `cat .tar.bz2 | docker run -i -v :/volume --rm loomchild/volume-backup restore -` – elquimista Dec 25 '21 at 16:31
  • Using vs code docker extension works like a magic. > Just open vs code and into the docker icon > open the volumes tab > right-click on the volume you create and click "inspect in vs code" this will load the container files in a new vscode window. – Dotun Peter Jan 24 '22 at 04:23
  • 1
    "docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh" from this answer worked for me to get inside of Docker VM – TechGeorgii Sep 05 '22 at 06:49
60

The other answers here are outdated if you're using Docker for Mac.

Here's how I was able to get into the VM. Run the command:

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

This is the default path, but you may need to first do: cd ~/Library/Containers/com.docker.docker/Data/vms

and then ls to see which directory your VM is in and replace the "0" accordingly.

When you're in, you might just see a blank screen. Hit your "Enter" key.

This page explains that to exit from the VM you need to "Ctrl-a" then "d"

Dmitriy
  • 767
  • 6
  • 9
  • Not fully working, because if you exit VM and enter it again, you'll get messed up shell. – AbstractVoid Jan 25 '19 at 11:48
  • 2
    This isn't working for me. I end up with a messed up shell. I have gone through and killed previous screen sessions and then tried again. When hitting enter I get random characters. if you hit enter again, new random characters. And so on – Steig Hallquist Oct 08 '19 at 12:48
  • 1
    to quit screen program use ctl-a then ctl-\ `/var/lib/docker` is in VM where you are after run `screen`, so e.g. `ls /var/lib/docker` etc. – Alex Martian Nov 06 '19 at 12:52
  • 1
    I got rid of the messy shell by ```screen -ls``` and then ```screen -X -S SESSION_NAME quit``` for every session. If you disconnect with ctr-A then D you can reconnect to the screen with ```screen -R``` and get a clean shell as well. – Andreas Riedmüller Apr 28 '20 at 11:56
  • 1
    This answer is outdated. As of 13 August 2021, the answer by RicarHincapie is correct. Keep scrolling until you find it. – Erick G. Hagstrom Aug 13 '21 at 21:57
55

See this answer

When using Docker for Mac Application, it appears that the containers are stored within the VM located at:

~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2

Community
  • 1
  • 1
PeterM
  • 769
  • 9
  • 7
  • 94
    **If you came here from Google**, scroll down to find the right answer. – user1717828 Sep 16 '18 at 01:07
  • 6
    I think this was the answer. But I'm using v20.10 now, theres nothing `~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux` directory. – Tony Chou Jan 26 '21 at 16:31
15

Just as @Dmitriy said:

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

and can use ctrl a + d to detach the screen

and use screen -dr to re-attach the screen again(since if you simply attach screen again, the terminal text will be garbled.)

Reference

or if you want to exit, use ctrl + a + k,then choose y to kill the screen.

May
  • 171
  • 1
  • 8
13

I think the new version of docker (my version is 20.10.5) uses socket instead of TTY to communicate with the virtual machine so you can use the nc command instead of the screen command.

nc -U ~/Library/Containers/com.docker.docker/Data/debug-shell.sock
Ehsan Ahmadi
  • 1,382
  • 15
  • 16
7

some what of a zombie thread but as I just found it here is another solution that doesn't need screen nor messes up shell etc.

The path listed from a docker volume inspect <vol_name>

returns the path for the container, something like:

"Mountpoint": "/var/lib/docker/volumes/coap_service_db_data/_data"

the _data component being the last component of the path you setup in the volumes: section of the service using a given volume eg:

volumes: - db_data:/var/lib/postgresql/data , obvs your mileage will vary.

To get there on the mac the easiest method I have found is to actually start a small container running and mount the root of the host to the /docker directory in the image, this gives you access to the volumes used on the host.

docker run --rm -it -v /:/docker alpine:edge

from this point you can cd to the volume

cd /var/lib/docker/volumes/coap_service_db_data/_data

lbdl
  • 353
  • 1
  • 4
  • 8
4

Looks like the new version of docker for Mac has moved this to a UI element which you can see here. Clicking on that button which says CLI will launch a terminal which you can use to browse the docker file system.Docker CLI Button

Ricky Sahu
  • 23,455
  • 4
  • 42
  • 32
1

Run:

docker run -it --privileged --pid=host debian nsenter -t 1 -a bash
ls /var/lib/docker
Rafael Eyng
  • 4,720
  • 2
  • 33
  • 34
1

For MacOS I use the following steps:

  1. login into docker virtual-machine (on MacOS docker can be run only inside virtual machine, in my case I have VirtualBox tool with docker VM): docker-machine ssh
  2. as soon as I logged-in I need to switch to super user from docker user: sudo -i
  3. now I'm able to check /var/lib/docker directory
ultraon
  • 2,220
  • 2
  • 28
  • 27
0

This path comes from Docker Host (not from MacOS) before "Docker for Mac Application" times, where there was a VirtualBox VM "default" and inside this VM, the mentioned path exists (for sure), now in "Docker for Mac Application" times there is a Docker.qcow2 image, which is qemu base vm. To jump inside this VM @mik-jagger way is ok (but there are few more)

DevOpsGuY
  • 127
  • 12
itiic
  • 3,284
  • 4
  • 20
  • 31
0

I would say that the file:

/var/run/docker.sock

Is actually at:

/Volumes/{DISKNAME}/var/run/docker.sock

If you run this, it should prove it, as long as your running VirtualBox 5.2.8 or later and the share for /Volumes is setup to be auto-mounted and permanent AND you generated the default docker-machine while on that version of Virtualbox:

#!/bin/bash
docker run -d --restart unless-stopped -p 9000:9000 \
-v /var/run/docker.sock:/var/run/docker.sock portainer/portainer \
--no-auth

Then, access Portainer at: 192.168.99.100:9000 or localhost:9000

djangofan
  • 28,471
  • 61
  • 196
  • 289
0

Docker logs are not in /var/lib/docker on MacOS.

MacOs users can find the docker logs on this path;

/Users/Barrack.Kenya/Library/Containers/com.docker.docker/Data/log/host

  • job_name: docker static_configs:

    • targets:
      • docker labels: job: dockerlogs path: (Please put the path)

    pipeline_stages:

    • docker: {}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 18 '22 at 14:52