The docker container mounts the volume, which can facilitate the data interaction between the container and the host.
This article describes how to mount a volume to a container.
The host /Users/ylspirit/Documents/blog/awk directory is mounted to the container /home/blog/awk directory.
➜ ~ docker run -i -t -h mount --name mount-test -v /Users/ylspirit/Documents/blog/awk:/home/blog/awk ccc6e87d482b /bin/bash
docker run
-v Bind mount a volume
-t Allocate a pseudo-TTY
-i Keep STDIN open even if not attached
-h Container host name
–name string Assign a name to the container
![](https://www.linuxcommands.site/wp-content/uploads/2020/02/image-39-1024x289.png)
After the mount is complete, we can exchange data through the mounted volume.
If you need to mount multiple directories, use multiple sets of -v parameters.
➜ ~ docker run -i -t -h mount --name mount-test -v /Users/ylspirit/Documents/blog/awk:/home/blog/awk -v /Users/ylspirit/Documents/blog/sed:/home/blog/sed ccc6e87d482b /bin/bash
OK, Here we have completed the volume mount of the new container.
So how do we mount volumes for an existing docker container?
As with the previous docker port mapping, we still have two ways to finish mounting volumes to existing containers.
1. Use the docker commit command to submit an existing container, run a new image, and mount the volume to implement the method of mounting a volume on an existing container.
➜ ~ docker stop container
➜ ~ docker commit container new-image
➜ ~ docker rm container
➜ ~ docker run -i -t -h hostName --name containerName -v /Users/ylspirit/Documents/blog/awk:/home/blog/awk -v /Users/ylspirit/Documents/blog/sed:/home/blog/sed new-image-id /bin/bash
2. Mount the volume of an existing container by modifying the docker configuration file.
To mount a volume on an existing container, you also need to modify two configuration files: hostconfig.json and config.v2.json
Here we test with container_id: c8e2231e1a52 container.
➜ ~ docker stop c8e2231e1a52
➜ ~ cd ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/
➜ ~ screen tty
docker-desktop:~# cd /var/lib/docker/containers/c8e2231e1a524442dc2f28f6c321d6c4
1683e174a0328d32b5e2780d16b49618/
vi hostconfig.json
Find ‘ “Binds”: ’ and modify to:
"Binds": [
"/Users/ylspirit/Documents/blog/awk:/home/blog/awk",
"/Users/ylspirit/Documents/blog/sed:/home/blog/sed"
],
Save the file and exit.
vi config.v2.json
Find ‘ “MountPoints”: ’ and modify to:
"MountPoints": {
"/home/blog/awk": {
"Source": "/Users/ylspirit/Documents/blog/awk",
"Destination": "/home/blog/awk",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/Users/ylspirit/Documents/blog/awk",
"Target": "/home/blog/awk"
},
"SkipMountpointCreation": false
},
"/home/blog/sed": {
"Source": "/Users/ylspirit/Documents/blog/sed",
"Destination": "/home/blog/sed",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/Users/ylspirit/Documents/blog/sed",
"Target": "/home/blog/sed"
},
"SkipMountpointCreation": false
}
},
Save the file and exit.
Exit screen and restart docker service, and start container.
Hey, thanks for the post, exactly what I was looking for.