Volume๊ณผ Mount

Docker Conatiner์— ์“ฐ์—ฌ์ง„ ๋ฐ์ดํ„ฐ๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‚ญ์ œ๋  ๋•Œ ํ•จ๊ป˜ ์‚ฌ๋ผ์ง„๋‹ค. ํ•˜์ง€๋งŒ ์ด์— ๊ด€๊ณ„์—†์ด ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜์†์ ์œผ๋กœ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ํ•˜๋‚˜์˜ ํด๋”๋ฅผ ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ์ผ์ด ๋นˆ๋ฒˆํ•˜๋‹ค.

์ด๋Ÿฌํ•œ ํ•„์š”์„ฑ์— ๋Œ€ํ•ด Docker๋Š” ๋‘๊ฐ€์ง€ ์˜ต์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ์ฒซ์งธ๊ฐ€ Volume, ๋‘˜์งธ๊ฐ€ Mount์ด๋‹ค.

Volume

์ด ๋ฐฉ๋ฒ•์€ Docker์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

Volume์˜ ์ƒ์„ฑ

$ docker volume create hello
hello

Volume์˜ ์กฐํšŒ

$ docker volume ls
DRIVER              VOLUME NAME
local               hello
$ docker volume inspect hello
[
    {
        "CreatedAt": "2020-05-09T17:03:46Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/our-vol/_data",
        "Name": "our-vol",
        "Options": {},
        "Scope": "local"
    }
]

Mountpoint๋ฅผ ๋ณด๋ฉด ํ•ด๋‹น ๋ณผ๋ฅจ์ด ์–ด๋””์— ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋ณผ๋ฅจ์„ ์ปจํ…Œ์ด๋„ˆ์— ๋งˆ์šดํŠธํ•˜๊ธฐ

$ docker run -v hello:/home/app --name one hugojuhel/notebook touch /app/test.txt

docker run -v <๋ณผ๋ฅจ ์ด๋ฆ„>:<์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ> --name <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„> <image ์ด๋ฆ„> <๋ช…๋ น> <ํŒŒ๋ผ๋ฏธํ„ฐ> ํ˜•์‹์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

$ docker inspect hello
(...์ƒ๋žต...)
    "Mounts": [
        {
            "Type": "volume",
            "Name": "our-vol",
            "Source": "/var/lib/docker/volumes/our-vol/_data",
            "Destination": "/app",
            "Driver": "local",
            "Mode": "z",
            "RW": true,
            "Propagation": ""
        }
    ],
(...์ƒ๋žต...)

์‹ค์ œ๋กœ ์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์กฐ์‚ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œจ๋ฉฐ, Type์ด volume์œผ๋กœ ์ง€์ •๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์— ๋งˆ์šดํŠธํ•  ๋•Œ์—๋„ ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋œ๋‹ค.

Volume ์‚ญ์ œ

$ docker volume rm hello
Error response from daemon: remove hello: volume is in use - [f73130c9dad14644ac46b89fe4018e561a7bcbfa4118d637949642d0d5d742e4, 666dda54f6be8ca852f3150b9741a9cab5a4659fa2e83fe6ca339550072c861ex]

์‚ญ์ œํ•  ๋•Œ๋Š” mount ๋œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ชจ๋‘ ์‚ญ์ œํ•œ ๋’ค์—์•ผ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

์‚ญ์ œ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œจ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.

Volume ์ฒญ์†Œ

$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y

๋งˆ์šดํŠธ ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ๋ณผ๋ฅจ์„ ํ•œ๋ฒˆ์— ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Bind-Mount

์œ„์˜ Volume ๋ฐฉ๋ฒ•์„ ๋ณด๋ฉด, ๊ฒฝ๋กœ๋ฅผ docker๊ฐ€ ์ œ์‹œํ•œ ๊ฒฝ๋กœ์— ๋งž์ถฐ์„œ ์ƒ์„ฑ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์‹œ์Šคํ…œ์˜ ํŠน์ • ๊ฒฝ๋กœ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์—…ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ์ด๋Ÿฐ ํ•„์š”์„ฑ์— ๋Œ€ํ•ด docker๋Š” Bind-Mount๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์‚ฌ์šฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ์œ„์˜ Volume ๋ช…์ด ๋“ค์–ด๊ฐ€๋Š” ์ž๋ฆฌ์—, ์›ํ•˜๋Š” ํ˜ธ์ŠคํŠธ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ๋๋‚œ๋‹ค.

docker run -it --name jpt -v /Users/Choiwansik/Documents/internship/image_processing/share:/home/jovyan/share -p 28888:8888 hugojuhel/notebook /bin/bash

์ด ๋•Œ ์—ญ์‹œ๋‚˜ container์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์จ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

"Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/Choiwansik/Documents/internship/image_processing/share",
                "Destination": "/home/jovyan/share",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
...

Type์ด bind๋กœ ๋ฌถ์—ฌ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Volume vs Bind-Mount

๊ฒฝ๋กœ(Mount Point) ๊ด€๋ฆฌํ•ด ์ค„๊นŒ? ๋ง๊นŒ?

๋‘˜์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€, docker๊ฐ€ mount point๋ฅผ ๊ด€๋ฆฌํ•ด ์ฃผ๋Š๋ƒ์˜ ์—ฌ๋ถ€๋กœ ๋‚˜๋‰œ๋‹ค. ๊ทธ๋ƒฅ ๋ง‰ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์• ๋Š” volume์ด ๋งž์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ปจํ…Œ์ด๋„ˆํ™”๋œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” bind-mount๊ฐ€ ๋” ์œ ๋ฆฌํ•˜๋‹ค.

Reference