简体   繁体   中英

cd into volume created by docker

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进入默认的docker-machine并通过将自己提升为sudo来创建my-data卷和cd

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

卷挂载并创建一个data.txt文件

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:

第一个终端窗口中的安装点 第二个终端窗口中的安装点

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM