docker (3) - volume mount
이전 글에서 docker의 container 내에서 생성된 데이터는 임시적인 것을 확인했다. docker에서 생성된 데이터를 영구 저장하기 위해서는 volume을 사용해야 한다.
volume은 docker container가 데이터를 저장하고 유지하기 위해 호스트 시스템에 마운트하는 저장공간이다.
다음과 같은 3종의 volume이 있지만 이 중 bind mount에 대해서만 알아보겠다.
- 호스트 시스템의 특정 file이나 directory를 연결한 bind mounts
- docker가 직접 관리하는 volumes (linux의 경우 /var/lib/docker/volumes)
- 메모리상의 tmpfs mounts
하나의 file mount하기
우선 현재 디렉토리에 index.html 파일을 하나 만들겠다.
➜ hello-world echo '<html><body>This is the HTML file on the host computer.</body></html>' > index.html
그리고 이 파일을 이미지 속 특정한 파일에 대응하는 -v 를 사용하면서 container를 띄워보자.
➜ hello-world docker run -d -p 80:80 --name my-nginx -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
31ce7ceb6d44: Already exists
f1359798dfe4: Already exists
4de1e0313830: Already exists
7745719004b6: Already exists
0f17732d34d5: Already exists
0eb0ed12e64c: Already exists
5836f8c1cebc: Already exists
Digest: sha256:86e53c4c16a6a276b204b0fd3a8143d86547c967dc8258b3d47c3a21bb68d3c6
Status: Downloaded newer image for nginx:latest
ee77b06c823b62134ff5d5ab21a093648a8649f52f5d5de4b75e5a2608bff7fd
➜ hello-world

이렇게 하면 이전 글처럼 특정한 파일이 들어간 image를 별도로 생성하지 않고, 공식 nginx image + 직접 mount한 index.html 로 바로 container를 띄울 수 있다.
directory mount
directory를 통채로 mount 하려면 다음과 같이 하면 된다.
docker run -d -p 80:80 --name my-nginx -v "$(pwd)/www:/usr/share/nginx/html" nginx
결론
이렇게 file 또는 directory를 mount해서 사용하면 container내에서 이것들에 대해 일으킨 변화들은 오롯이 보존된다.