2

I am new to docker. Learning it from a book called Learn Fundamentals of Docker 18.x From Packt Publishing. In the chapter on Docker Volume the author creates a vm using docker-machine and then creates a volume called my-data. I have executed the commands as below in first terminal window:

ssh into default docker-machine and create my-data volume and cd into it by elevating myself to sudo

Then the author creates a container mounting that volume using -v command. I have executed the commands as below in a second terminal window):

Volume Mounted and created a data.txt file

When I try listing the files in that volume back in my first terminal window I get nothing.

So my understanding was

  1. Create volume in docker.
  2. Mount the volume in a container.
  3. Create some data.
  4. Exit the container. And the data will be present.

I also notice that the docker volume inspect my-data provides different mount points:

Mount point in first terminal window Mount point in second terminal window

By this I understand they are different volumes, right?

But I am not seeing any data in the volume. Is my understanding correct? Can anybody explain to me how volumes in docker work?

Also I would like to know how do I navigate to this location from my Mac terminal?

Thanks in advance.

1 Answer 1

0

You are running docker-machine which essentially creates a virtual machine on the host with the docker daemon installed. When you run docker volume create in there, it creates the volume within the vm. I suspect you may be running the docker commands from outside of the docker-machine, but you have created the volume within the docker-machine, which is why they seem to be different volumes.

Ideally you want to configure your host to talk to the docker-machine when executing any docker commands. From the docs:

 $ docker-machine env default
 export DOCKER_TLS_VERIFY="1"
 export DOCKER_HOST="tcp://172.16.62.130:2376"
 export DOCKER_CERT_PATH="/Users/<yourusername>/.docker/machine/machines/default"
 export DOCKER_MACHINE_NAME="default"
 # Run this command to configure your shell:
 # eval "$(docker-machine env default)"

Otherwise, your understanding of docker volumes is correct. Docker volumes are created and managed by docker - meaning their physical location can change within the docker host. But data within the volume will always persist until you delete it. According to the docs:

When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Yeah I am creating volume inside vm. But executing commands outside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.