[Docker](EN) Attach host file or directory to container using bind mounts

Post about docker bind mounts usage


Environment and Prerequisite

  • Linux
  • Docker


Bind Mounts

  • Docker supports various ways to store files in container.
  • In official doc, there are three ways like below picture.
  • This post only summarize about Bind mounts in below picture. There are several usages and options in official doc.
  • Bind mounts enables a file or directory on the host machine to be mounted into a container.
  • Use via --mount or -v option.


Bind Mounts Examples

Basic Usage Form

  • Below example is from official doc.
  • Add needed options after --mount.
  • Add host’s file or directory to source and container’s path to target.
  • In below case, host’s "$(pwd)"/target directory becomes container’s /app directory. So result of ls "$(pwd)"/target in host and ls /app in container are same.
  • It is reference of host’s file or directory so change in container would be affect to host.
docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  nginx:latest
  • General form
docker run -d \
  -it \
  --name [CONTAINER NAME] \
  --mount type=bind,source="[HOST PATH]",target="[TARGET PATH]" \
  [IMAGE NAME]:[TAG]


Needed Files For Examples

  • Host file
$ ls -al ~/test/test.txt
-rw-rw-r-- 1 twpower twpower 37 Feb 27 20:46 /home/twpower/test/test.txt
$ cat ~/test/test.txt
String
test string
Line in container
  • Host directory
$ ls -al test-docker-bind/
total 16
drwxrwxr-x  2 twpower twpower 4096 Feb 27 21:17 .
drwxr-xr-x 30 twpower twpower 4096 Feb 27 21:17 ..
-rwxrwxr-x  1 twpower twpower   25 Feb 27 21:17 test.sh
-rw-rw-r--  1 twpower twpower   19 Feb 26 22:59 test.txt
$ cat test-docker-bind/*
#!/bin/sh

echo "result"
String
test string
  • Docker image: ubuntu:18.04
$ sudo docker images
REPOSITORY    TAG              IMAGE ID       CREATED         SIZE
ubuntu        18.04            56def654ec22   5 months ago    63.2MB


Examples

Host file -> Container file

  • Mount single host file to specific container’s file
  • Need to specify file name to target.
  • If file already exists in container, then file will be overwritten.
# Mount host file "/home/twpower/test/test.txt" to container directory "/root"

sudo docker run -d -it --name file-to-container --mount type=bind,source="/home/twpower/test/test.txt",target="/root/test.txt" ubuntu:18.04
  • Name can be changed in container like below.
# Mount host file "/home/twpower/test/test.txt" to container directory "/root" with renaming

sudo docker run -d -it --name file-to-container --mount type=bind,source="/home/twpower/test/test.txt",target="/root/test-rename.txt" ubuntu:18.04
  • If you additionally specify a directory that is not in the container, the directory is created and files are mounted.
# Mount host file "/home/twpower/test/test.txt" to container directory "/home/twpower/test/new-directory-in-container"

sudo docker run -d -it --name file-to-container --mount type=bind,source="/home/twpower/test/test.txt",target="/home/twpower/test/new-directory-in-container/test.txt" ubuntu:18.04


Host directory -> Container directory

  • If you specify directory to each source and target and mount, then it appears files and directories under source directory are shown in target directory.
# Mount host directory "/home/twpower/test-docker-bind" to container directory "/root"

sudo docker run -d -it --name dir-to-container --mount type=bind,source="/home/twpower/test-docker-bind",target="/root" ubuntu:18.04
$ ls /home/twpower/test-docker-bind
test.sh  test.txt
$ sudo docker exec -it dir-to-container ls /root
test.sh  test.txt
  • If you would like to make same directory under specific directory in container, then add directory name in the end.
# Mount host directory "/home/twpower/test-docker-bind" to container directory "/root/test-docker-bind"

sudo docker run -d -it --name dir-to-container --mount type=bind,source="/home/twpower/test-docker-bind",target="/root/test-docker-bind" ubuntu:18.04
$ ls /home/twpower/test-docker-bind
test.sh  test.txt
$ sudo docker exec -it dir-to-container ls /root
test-docker-bind
$ sudo docker exec -it dir-to-container ls /root/test-docker-bind
test.sh  test.txt


Reference