doc mount directory

https://my.oschina.net/piorcn/blog/324202

http://blog.csdn.net/yangzhenping/article/details/43667785


Docker can support mounting a directory on a host into an image.

docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash

Through the -v parameter, before the colon is the host directory, which must be an absolute path, and after the colon is the path mounted in the image.

Now the files in the host can be shared within the image.

The default mount path permission is read and write. If specified as read-only, use: ro

docker run -it -v /home/dock/Downloads:/usr/Downloads:ro ubuntu64 /bin/bash


Docker also provides an advanced usage. called data volume.

Data volume: "In fact, it is a normal container, which is specially used to provide data volumes for other containers to mount." It feels like a data mount information defined by a container. Other container startups can directly mount the mount information defined in the data volume container.

See example:

docker run -v /home/dock/Downloads:/usr/Downloads  --name dataVol ubuntu64 /bin/bash

Create a normal container. Use --name to specify a name for him (if not specified, a random name will be generated).

Create a new container to use this data volume.

docker run -it --volumes-from dataVol ubuntu64 /bin/bash

--volumes-from is used to specify which data volume to mount the data from.


How to copy data to each other inside and outside the Docker container?

 

Copy files from the container to the host

[plain]  view plain copy  
  View code snippets on CODE Derive to my code slice
  1. docker cp <containerId>:/file/path/within/container /host/path/target  

 

Copy files from the host to the container


Referenced from:

http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container


1. Mount the host data volume into the container with -v


 

[plain]  view plain copy  
  View code snippets on CODE Derive to my code slice
  1. docker run -v /path/to/hostdir:/mnt $container  
  2. copy inside the container  
  3. cp /mnt/sourcefile /path/to/destfile  


 

 

2. Copy directly on the host to the container physical storage system

 


A. Get the container name or id:

[plain]  view plain copy  
  View code snippets on CODE Derive to my code slice
  1. $ docker ps  


 

 

B. Get the id of the entire container

[plain]  view plain copy  
  View code snippets on CODE Derive to my code slice
  1. $ docker inspect -f   '{{.Id}}'  步骤A获取的名称或者id  


 

 

C. 在主机上拷贝文件:

[plain]  view plain  copy
  View code snippets on CODE Derive to my code slice
  1. $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE   
  2. 或者  
  3. $ sudo cp path-file-host /var/lib/docker/devicemapper/mnt/123abc<<id>>/rootfs/root  


 

例子:

[plain]  view plain  copy
  View code snippets on CODE Derive to my code slice
  1. $ docker ps  
  2.   
  3. CONTAINER ID      IMAGE    COMMAND       CREATED      STATUS       PORTS        NAMES  
  4.   
  5. d8e703d7e303   solidleon/ssh:latest      /usr/sbin/sshd -D                      cranky_pare  
  6.   
  7. $ docker inspect -f   '{{.Id}}' cranky_pare  
  8.   
  9. or   
  10. $ docker inspect -f   '{{.Id}}' d8e703d7e303  
  11.   
  12. d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5  
  13.   
  14. $ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5  


3.用输入输出符


 

[plain]  view plain  copy
  View code snippets on CODE Derive to my code slice
  1. docker run -i ubuntu /bin/bash -c 'cat > /path/to/container/file' < /path/to/host/file/  


or

[plain]  view plain copy  
  View code snippets on CODE Derive to my code slice
  1. docker exec -it <container_id> bash -c 'cat > /path/to/container/file' < /path/to/host/file/  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326147149&siteId=291194637