[Docker] Bind mounts를 이용해 호스트 파일이나 디렉토리를 컨테이너에 붙여 사용하기

도커 bind mounts 사용법에 대한 정리


환경

  • Linux
  • Docker


Bind Mounts

  • 도커에서는 파일을 여러가지 방식으로 저장할 수 있게 지원해준다.
  • 공식문서에는 3가지 방식이 나왔있는데 아래 그림과 같다.
  • 이 글에서는 아래에 있는 Bind mounts 예시들만 정리한다. 사용에 대한 자세한 옵션들은 공식문서에 나와있다.
  • Bind mounts는 호스트 경로에 있는 디렉토리나 파일을 컨테이너에 붙여 직접 접근할 수 있도록 해준다.
  • --mount-v 옵션을 통해 사용할 수 있다.


Bind Mounts 예시

기본 사용 형태

  • 아래는 공식문서에 나와있는 예시
  • --mount 옵션 뒤에 필요한 값들을 넣어주면 된다.
  • 어떤 호스트 파일 혹은 디렉토리를 컨테이너의 어디에 넣을건지 각각 sourcetarget에 값을 주면된다.
  • 아래의 경우 호스트의 "$(pwd)"/target에 있는 디렉토리가 컨테이너에서 /app에 해당하는 디렉토리가 된다. 그렇기에 호스트에서 ls "$(pwd)"/target의 결과와 컨테이너에서 ls /app의 결과가 같다.
  • 호스트의 파일이나 디렉토리를 참조하는거기 때문에 컨테이너에서 변경하면 호스트에서도 동일하게 변경된다.
docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  nginx:latest
  • 일반화
docker run -d \
  -it \
  --name [CONTAINER NAME] \
  --mount type=bind,source="[HOST PATH]",target="[TARGET PATH]" \
  [IMAGE NAME]:[TAG]


예시에 사용할 파일들

  • 호스트 파일
$ 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
  • 호스트 디렉토리
$ 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
  • 도커 이미지: ubuntu:18.04
$ sudo docker images
REPOSITORY    TAG              IMAGE ID       CREATED         SIZE
ubuntu        18.04            56def654ec22   5 months ago    63.2MB


예시

호스트 파일 -> 컨테이너 파일

  • 하나의 호스트 파일을 컨테이너의 특정 파일로 마운트
  • 파일명을 target에 명시해줘야한다.
  • 파일이 컨테이너에 이미 존재한다면 덮어쓰고 호스트 파일을 마운트한다.
# 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
  • 이름을 컨테이너에서 아래처럼 변경도 가능하다.
# 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
  • 컨테이너에 없는 디렉토리를 추가적으로 명시하면 해당 디렉토리를 만들어서 파일을 마운트한다.
# 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


호스트 디렉토리 -> 컨테이너 디렉토리

  • sourcetarget에 각각 디렉토리를 명시해 마운트하면 source 하위에 있는 파일들과 디렉토리들이 target 디렉토리에 들어간 형태로 나온다.
# 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
  • 동일한 디렉토리를 특정 디렉토리 하위에 만들고 싶다면 끝에 디렉토리명을 추가해주면 된다.
# 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


참고자료