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
- 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
- docker run -v /path/to/hostdir:/mnt $container
- copy inside the container
- 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:
- $ docker ps
B. Get the id of the entire container
- $ docker inspect -f '{{.Id}}' 步骤A获取的名称或者id
C. 在主机上拷贝文件:
- $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE
- 或者
- $ sudo cp path-file-host /var/lib/docker/devicemapper/mnt/123abc<<id>>/rootfs/root
例子:
- $ docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- d8e703d7e303 solidleon/ssh:latest /usr/sbin/sshd -D cranky_pare
- $ docker inspect -f '{{.Id}}' cranky_pare
- or
- $ docker inspect -f '{{.Id}}' d8e703d7e303
- d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5
- $ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5
3.用输入输出符
- docker run -i ubuntu /bin/bash -c 'cat > /path/to/container/file' < /path/to/host/file/
or
- docker exec -it <container_id> bash -c 'cat > /path/to/container/file' < /path/to/host/file/