๐Ÿ’พ ๋„์ปค์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.


๐ŸŽฏ ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ จ ๋ช…๋ น์–ด

Docker CLI ๋„๊ตฌ๋Š” Docker ์ปจํ…Œ์ด๋„ˆ(container)์˜ ํšจ๊ณผ์ ์ธ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ๋‹ค์–‘ํ•œ ์ปค๋งจ๋“œ(command)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ ์กฐํšŒ

docker psย ์ปค๋งจ๋“œ๋Š” Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์กฐํšŒ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

# ๊ธฐ๋ณธ ์กฐํšŒ
$ docker ps

# -a ์˜ต์…˜
# ํ˜„์žฌ ์ค‘์ง€๋˜์–ด ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๊นŒ์ง€ ํ•จ๊ป˜ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
$ docker ps -a

# -s ์˜ต์…˜
# ๊ฐ ์ปจํ…Œ์ด๋„ˆ์˜ ๋””์Šคํฌ ์‚ฌ์šฉ๋Ÿ‰๊นŒ์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
$ docker ps -s

# -f ์˜ต์…˜
# ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ•„ํ„ฐ๋งํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# ์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ์ด๋ฏธ์ง€๋กœ ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ์ปจํ…Œ์ด์–ด๋งŒ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ancestor ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
$ docker ps -af "ancestor=python:alpine"
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
b1bae952f7a6        python:alpine       "python -m http.servโ€ฆ"   About an hour ago   Exited (137) 3 minutes ago                         amazing_chatelet
518d8bb6afaf        python:alpine       "python3"                About an hour ago   Exited (0) About an hour ago                       focused_gauss


์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

docker runย ์ปค๋งจ๋“œ๋Š” ์ƒ๋‹นํžˆ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ต์…˜์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

# ๊ธฐ๋ณธ ์‹คํ–‰
# $ docker run (<์˜ต์…˜>) <์ด๋ฏธ์ง€ ์‹๋ณ„์ž> (<๋ช…๋ น์–ด>) (<์ธ์ž>)
$ docker run hello-world

# -d ์˜ต์…˜
# ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰
# -d ์˜ต์…˜์—†์ด ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด, ํ•ด๋‹น ํ„ฐ๋ฏธ๋„์—์„œ Ctrl + C๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋น ์ ธ๋‚˜์˜ค๋Š” ์ˆœ๊ฐ„ ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๋Š” ์ข…๋ฃŒ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
$ docker run -d python:3.8-alpine python -m http.server

# -it ์˜ต์…˜
# -i์™€ -t๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•œ ๊ฒƒ์œผ๋กœ ํ„ฐ๋ฏธ๋„ ์ž…๋ ฅ์„ ์œ„ํ•œ ์˜ต์…˜
$ docker run -it python:3.8-alpine
$ docker run -it python:3.8-alpine /bin/sh

# -name ์˜ต์…˜
# ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ์„ค์ •
$ docker run -d --name my-server python:3.8-alpine python -m http.server

# -e ์˜ต์…˜
# ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •
## Docker ์ปจํ…Œ์ด๋„ˆ์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” -e ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
## ๋˜ํ•œ, -e ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด Dockerfile์˜ ENV ์„ค์ •๋„ ๋ฎ์–ด์จ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
$ docker run -e FOO=bar python:3.8-alpine env

# -p ์˜ต์…˜
# ํ˜ธ์ŠคํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐ (ํฌ์›Œ๋”ฉ)
$ docker run -d -p 80:8000 python:3.8-alpine python -m http.server

# -v ์˜ต์…˜
# ํ˜ธ์ŠคํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์—ฐ๊ฒฐ (๋งˆ์šดํŠธ)
$ echo Hi > test.txt
$ docker run -v `pwd`:/etc python:3.8-alpine cat /etc/test.txt
Hi

# -rm ์˜ต์…˜
# ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์ปจํ…Œ์ด๋„ˆ์™€ ๊ด€๋ จ๋œ ๋ฆฌ์†Œ์Šค(ํŒŒ์ผ ์‹œ์Šคํ…œ, ๋ณผ๋ฅจ)๊นŒ์ง€ ๊นจ๋—์ด ์ œ๊ฑฐํ•ด์ค๋‹ˆ๋‹ค.
$ docker run --rm -it ubuntu:16.04 /bin/bash


์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€

์‹คํ–‰ ์ค‘์ธ Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜๋ ค๋ฉดย docker stopย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ง€ํ•˜๊ณ  ์‹ถ์€ ์ปจํ…Œ์ด๋„ˆ์˜ ์•„์ด๋””๋‚˜ ์ด๋ฆ„์„ ์ธ์ž๋กœ ๋„˜๊น๋‹ˆ๋‹ค.

# ์•„์ด๋””๋‚˜ ์ด๋ฆ„์„ ์ธ์ž๋กœ ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€
$ docker stop container_name
container_name

# ์กฐํšŒ
$ docker ps -af "name=container_name"
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
b1bae952f7a6        python:alpine       "python -m http.servโ€ฆ"   About an hour ago   Exited (137) 32 seconds ago                       container_name

# ์ปจํ…Œ์ด๋„ˆ ์ „์ฒด ์ค‘์ง€
$ docker stop $(docker ps -a -q)


์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘

์ค‘์ง€๋œ Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋ ค๋ฉดย docker startย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์žฌ์‹œ์ž‘ํ•˜๊ณ  ์‹ถ์€ ์ปจํ…Œ์ด๋„ˆ์˜ ์•„์ด๋””๋‚˜ ์ด๋ฆ„์„ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# ์•„์ด๋””๋‚˜ ์ด๋ฆ„์„ ์ธ์ž๋กœ ์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘
$ docker start b1bae952f7m1
b1bae952f7m1

$ docker ps -af "id=b1bae952f7m1"
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b1bae952f7m1        python:alpine       "python -m http.servโ€ฆ"   About an hour ago   Up About a minute                       amazing_chatelet


์ปจํ…Œ์ด๋„ˆ ์ƒ๋Œ€๋กœ ๋ช…๋ น์–ด ์‹คํ–‰

์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ์ƒ๋Œ€๋กœ ๋ช…๋ น์–ด๋ฅผ ๋‚ ๋ฆฌ๊ณ  ์‹ถ๋‹ค๋ฉดย docker execย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ๋Œ€๋กœย lsย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

$ docker exec b1bae952f7a6 ls
bin dev etc home lib media mnt ...์ƒ๋žต...

-itย ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ์˜ ์‰˜(shell)๊ณผ ์ž‘์ ‘ ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉด์„œ ์—ฐ์†์œผ๋กœ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker exec -it container_name /bin/sh

$ ls
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr


์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ํ™•์ธ

์ปจํ…Œ์ด๋„ˆ์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉดย docker logsย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

$ docker logs b1bae952f7a6

# -f ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
$ docker logs -f container_name

# ๋งˆ์ง€๋ง‰ 10์ค„๋งŒ ์ถœ๋ ฅ
$ docker logs --tail 10 container_name


์ปจํ…Œ์ด๋„ˆ ์ƒ์„ธ ์ •๋ณด

ํŠน์ • ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด,ย docker inspectย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

$ docker inspect 8c27a672e23c
[
    {
        "Id": "8c27a672e23c1c48aaaa0d2e65325aefb36db2934f724634dd4f5684397014ba",
        "Created": "2020-04-25T18:23:24.651490253Z",
        "Path": "/bin/sh",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-04-25T18:23:25.300618082Z",
            "FinishedAt": "2020-04-25T18:23:25.347690066Z"
        },
        "Image": "sha256:f70734b6a266dcb5f44c383274821207885b549b75c8e119404917a61335981a",
        ...์ƒ๋žต...
    }
]


์ปจํ…Œ์ด๋„ˆ ์ œ๊ฑฐ

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š”ย docker rmย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ง€๋˜์–ด ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๋Š” ์•„๋ฌด ์˜ต์…˜์—†์ด ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์ปจํ…Œ์ด๋„ˆ ์ œ๊ฑฐ
$ docker rm 3722f14f4d2a
3722f14f4d2a

# ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋กœ ์ œ๊ฑฐ๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
# ์ด ๋•Œ๋Š”, -f ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋จผ์ € ์ •์ง€์‹œํ‚จ ๋‹ค์Œ์— ์ œ๊ฑฐํ•ด์ค๋‹ˆ๋‹ค.
$ docker rm d2f83048485e
Error response from daemon: You cannot remove a running container d2f83048485e4fe694dac374be152906e4e20da400305e5095bbb476bcaf718d. Stop the container before attempting removal or force remove
$ docker rm -f d2f83048485e

# ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฑด๋“ค์ง€ ์•Š๊ณ  ์ค‘์ง€๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
$ docker rm $(docker ps -a -q)


์ปจํ…Œ์ด๋„ˆ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ 

๐ŸŽฏ ๋ณผ๋ฅจ/๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ ๊ด€๋ จ ๋ช…๋ น์–ด

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

์ด๋ ‡๊ฒŒ Docker ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ๋ช… ์ฃผ๊ธฐ์™€ ๊ด€๊ณ„์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ์˜์†์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก Docker๋Š” ๋‘๊ฐ€์ง€ ์˜ต์…˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ฒซ๋ฒˆ์งธ๋Š” Docker ๋ณผ๋ฅจ(volume), ๋‘๋ฒˆ์งธ๋Š” ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ(bind mount)์ž…๋‹ˆ๋‹ค.

docker bind volume

๋ณผ๋ฅจ ์ƒ์„ฑ

docker volume createย ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณผ๋ฅจ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker volume create our-vol
our-vol


๋ณผ๋ฅจ ์กฐํšŒ

docker volume lsย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ณผ๋ฅจ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker volume ls
DRIVER              VOLUME NAME
local               our-vol


๋ณผ๋ฅจ ์ƒ์„ธ ์ •๋ณด

docker volume inspectย ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ณผ๋ฅจ์„ ์ข€ ๋” ์ƒ์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

Mountpointย ํ•ญ๋ชฉ์„ ๋ณด๋ฉด ํ•ด๋‹น ๋ณผ๋ฅจ์ด ์ปดํ“จํ„ฐ์˜ ์–ด๋Š ๊ฒฝ๋กœ์— ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ณผ๋ฅจ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณผ๋ฅจ์„ ์ปจํ…Œ์ด๋„ˆ์— ๋งˆ์šดํŠธ(mount)ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.ย docker runย ์ปค๋งจ๋“œ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•  ๋•Œย -vย ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ฝœ๋ก (:)์„ ๊ตฌ๋ถ„์ž๋กœ ํ•ด์„œ ์•ž์—๋Š” ๋งˆ์šดํŠธํ•  ๋ณผ๋ฅจ๋ช… ๋’ค์—๋Š” ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๊ฒฝ๋กœ๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# `our-vol`ย ๋ณผ๋ฅจ์„ย `one`ย ์ปจํ…Œ์ด๋„ˆ์˜ย `/app`ย ๊ฒฝ๋กœ์— ๋งˆ์šดํŠธ, touch ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ ์ƒ์„ฑ
$ docker run -v our-vol:/app --name one busybox touch /app/test.txt

touch /app/test.txtย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—,ย test.txt ํŒŒ์ผ์ดย our-volย ๋ณผ๋ฅจ์˜ ๊ฒฝ๋กœ์—๋„ ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค.

$ ls /var/lib/docker/volumes/our-vol/_data
test.txt

docker inspectย ์ปค๋งจ๋“œ๋กœ ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉดย our-volย ๋ณผ๋ฅจ์ดย volumeย ํƒ€์ž…์œผ๋กœ ๋งˆ์šดํŠธ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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


๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์—๋„ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ

๊ฐ™์€ย our-volย ๋ณผ๋ฅจ์„ย twoย ์ปจํ…Œ์ด๋„ˆ์˜ย /appย ๊ฒฝ๋กœ์— ๋งˆ์šดํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.ย ls /appย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋‹ˆ,ย oneย ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ณผ๋ฅจ์— ์ƒ์„ฑํ•ด๋†“์€ ํŒŒ์ผ์ด ๊ทธ๋Œ€๋กœ ๋ณด์ด๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker run -v our-vol:/app --name two busybox ls /app
test.txt

์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•˜๋‚˜์˜ ๋ณผ๋ฅจ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ณต์œ ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋ณผ๋ฅจ ์‚ญ์ œ

docker volume rmย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณผ๋ฅจ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ œ๊ฑฐํ•˜๋ ค๋Š” ๋ณผ๋ฅจ์ด ๋งˆ์šดํŠธ๋˜์–ด ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žˆ์„ ๋•Œ๋Š” ํ•ด๋‹น ๋ณผ๋ฅจ์ด ์ œ๊ฑฐ๊ฐ€ ๋˜์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ๋Š” ํ•ด๋‹น ๋ณผ๋ฅจ์ด ๋งˆ์šดํŠธ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋จผ์ € ์‚ญ์ œํ•˜๊ณ , ๋ณผ๋ฅจ์„ ์‚ญ์ œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

# ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ
$ docker rm -f one two
one
two

# ๋ณผ๋ฅจ ์‚ญ์ œ
$ docker volume rm our-vol
our-vol


๋ณผ๋ฅจ ์ฒญ์†Œ

docker volume pruneย ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋งˆ์šดํŠธ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๋ชจ๋“  ๋ณผ๋ฅจ์„ ํ•œ๋ฒˆ์— ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ 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


๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ

Docker ์ปจํ…Œ์ด๋„ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ(bind mount)๋ผ๋Š” ๊ฒƒ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ˜ธ์ŠคํŠธ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ํŠน์ • ๊ฒฝ๋กœ๋ฅผ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ฐ”๋กœ ๋งˆ์šดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ย docker runย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ,ย -vย ์˜ต์…˜์˜ ์ฝœ๋ก (:) ์•ž ๋ถ€๋ถ„์— ๋งˆ์šดํŠธ๋ช… ๋Œ€์‹ ์— ํ˜ธ์ŠคํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ํ˜„์žฌ ๊ฒฝ๋กœ์—ย test.txtย ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น ํ˜ธ์ŠคํŠธ ๊ฒฝ๋กœ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์˜ย /appย ๊ฒฝ๋กœ์— ๋งˆ์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ์— ํ„ฐ๋ฏธ๋„์— ์ ‘์†ํ•˜์—ฌย ls /appย ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉดย test.txtย ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ํŒŒ์ผ ์ƒ์„ฑ
$ touch test.txt

# ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ, ์ปจํ…Œ์ด๋„ˆ ์‰˜ ์ ‘์†
$ docker run -v `pwd`:/app -it --name one busybox /bin/bash

# ์กฐํšŒ
$ ls /app
test.txt

๋ฐ˜๋Œ€๋กœ ์ปจํ…Œ์ด๋„ˆ์˜ย /appย ๊ฒฝ๋กœ ์ƒ์—์„œย test2.txtย ํŒŒ์ผ์„ ์‹คํ–‰ํ•ด๋ณด๋ฉด ํ˜ธ์ŠคํŠธ์˜ ํ˜„์žฌ ๊ฒฝ๋กœ์—์„œ๋„ ํ•ด๋‹น ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ํŒŒ์ผ ์ƒ์„ฑ
$ touch /app/test2.txt

# ์ปจํ…Œ์ด๋„ˆ ์‰˜ ์ข…๋ฃŒ
$ exit

# ํ˜ธ์ŠคํŠธ์—์„œ ์กฐํšŒ
$ ls
test.txt   test2.txt


๋ณผ๋ฅจ VS ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ

๋ณผ๋ฅจ(volume)๊ณผ ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ(bind mount)์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ Docker์˜ ํ•ด๋‹น ๋งˆ์šดํŠธ ํฌ์ธํŠธ ๊ด€๋ฆฌ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค.

๋ณผ๋ฅจ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์Šค์Šค๋กœ ๋ณผ๋ฅจ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•ด์•ผํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๋ณผ๋ฅจ์€ Docker ์ƒ์—์„œ ์ด๋ฏธ์ง€(image)๋‚˜ ์ปจํ…Œ์ด๋„ˆ(container), ๋„คํŠธ์›Œํฌ(network)์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ด€๋ฆฌ๊ฐ€ ๋˜๋Š” ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ๋Š” ๋ณผ๋ฅจ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋˜์ง€๋งŒ ์ปจํ…Œ์ด๋„ˆํ™”๋œ ๋กœ์ปฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•  ๋•Œ๋Š” ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ๊ฐ€ ๋” ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋กœ์ปฌ์—์„œ ๊ฐœ๋ฐœ์„ ํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํ”„๋กœ์ ํŠธ ์ €์žฅ์†Œ๋ฅผย git cloneย ๋ฐ›์•„๋†“๊ณ  ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ”์ธ๋“œ ๋งˆ์šดํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์˜ ํŠน์ • ๊ฒฝ๋กœ์— ๋งˆ์šดํŠธํ•ด์ฃผ๋ฉด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋งˆ๋‹ค ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋„ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ณผ๋ฅจ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ 

๐ŸŽฏ ๋„คํŠธ์›Œํฌ ๊ด€๋ จ ๋ช…๋ น์–ด

Docker ์ปจํ…Œ์ด๋„ˆ(container)๋Š” ๊ฒฉ๋ฆฌ๋œ ํ™˜๊ฒฝ์—์„œ ๋Œ์•„๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์™€์˜ ํ†ต์‹ ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•˜๋‚˜์˜ Docker ๋„คํŠธ์›Œํฌ(network)์— ์—ฐ๊ฒฐ์‹œํ‚ค๋ฉด ์„œ๋กœ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

๋„คํŠธ์›Œํฌ ์ข…๋ฅ˜

Docker ๋„คํŠธ์›Œํฌ๋Š”ย bridge,ย host,ย overlayย ๋“ฑ ๋ชฉ์ ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๋„คํŠธ์›Œํฌ ๋“œ๋ผ์ด๋ฒ„(driver)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

  • bridgeย ๋„คํŠธ์›Œํฌ๋Š” ํ•˜๋‚˜์˜ ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋“ค์ด ์„œ๋กœ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.
  • hostย ๋„คํŠธ์›Œํฌ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์™€ ๋™์ผํ•œ ๋„คํŠธ์›Œํฌ์—์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋Œ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • overlayย ๋„คํŠธ์›Œํฌ๋Š” ์—ฌ๋Ÿฌ ํ˜ธ์ŠคํŠธ์— ๋ถ„์‚ฐ๋˜์–ด ๋Œ์•„๊ฐ€๋Š” ์ปจํ…Œ์ด๋„ˆ๋“ค ๊ฐ„์— ๋„คํŠธ์›Œํ‚น์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋„คํŠธ์›Œํฌ ์ƒ์„ฑ

docker network createย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ Docker ๋„คํŠธ์›Œํฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker network create our-net
e6dfe4a9a5ec85abcb484662c30a3a0fc76df217dde76d52fac39fae8412ca68


๋„คํŠธ์›Œํฌ ์กฐํšŒ

docker network ls ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” Docker ๋„คํŠธ์›Œํฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
143496b94e57        bridge              bridge              local
311d6534f79f        host                host                local
aa89f58200a6        none                null                local


๋„คํŠธ์›Œํฌ ์ƒ์„ธ ์ •๋ณด

docker network inspectย ์ปค๋งจ๋“œ๋กœ ๋„คํŠธ์›Œํฌ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker network inspect our-net


๋„คํŠธ์›Œํฌ์— ์ปจํ…Œ์ด๋„ˆ ์—ฐ๊ฒฐ

๋จผ์ € ์ปจํ…Œ์ด๋„ˆ ํ•˜๋‚˜๋ฅผย one ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์‹คํ–‰ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

$ docker run -itd --name one busybox
660bafdce2996378cde070dfd894731bb90745e46d2ab10d6504c0cc9f4bdea9

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•  ๋•Œย --networkย ์˜ต์…˜์„ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด, ๊ธฐ๋ณธ์ ์œผ๋กœย bride ๋ผ๋Š” ์ด๋ฆ„์˜ ๋””ํดํŠธ ๋„คํŠธ์›Œํฌ์— ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

$ docker network inspect bridge
... ์ƒ๋žต ...
        "Containers": {
            "660bafdce2996378cde070dfd894731bb90745e46d2ab10d6504c0cc9f4bdea9": {
                "Name": "one",
                "EndpointID": "40b4bbd8385debf86eef2fc2136315e1a82fa1ef72877bfae25477d6e8e46726",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
        },
... ์ƒ๋žต ...


์ด์ œ oneย ์ปจํ…Œ์ด๋„ˆ์™€ย our-netย ๋„คํŠธ์›Œํฌ๋ฅผ ์—ฐ๊ฒฐํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
Docker ๋„คํŠธ์›Œํฌ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ๋Š”ย docker network connectย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

$ docker network connect our-net one

our-netย ๋„คํŠธ์›Œํฌ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋ฉดย Containersย ํ•ญ๋ชฉ์—ย oneย ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.ย oneย ์ปจํ…Œ์ด๋„ˆ์— IPย 172.19.0.2๊ฐ€ ํ• ๋‹น๋œ ๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker network inspect our-net
[
    {
        "Name": "our-net",
        "Id": "e6dfe4a9a5ec85abcb484662c30a3a0fc76df217dde76d52fac39fae8412ca68",
        "Created": "2020-04-26T19:23:04.563643516Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "660bafdce2996378cde070dfd894731bb90745e46d2ab10d6504c0cc9f4bdea9": {
                "Name": "one",
                "EndpointID": "cc490148a533d40b3aff33a421cc9a01c731c75a8deb70ab729a5358f2fd381c",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]


๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ ์ปจํ…Œ์ด๋„ˆ ์—ฐ๊ฒฐ ํ•ด์ œ

ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„คํŠธ์›Œํฌ์— ๋™์‹œ์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ดˆ์—ย oneย ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œย bridgeย ๋„คํŠธ์›Œํฌ ๋ถ™์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ˜„์žฌย oneย ์ปจํ…Œ์ด๋„ˆ๋Š”ย our-netย ๋„คํŠธ์›Œํฌ์™€ย bridgeย ๋„คํŠธ์›Œํฌ์— ๋™์‹œ์— ๋ถ™์–ด์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

oneย ์ปจํ…Œ์ด๋„ˆ๋ฅผย bridgeย ๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ ํ•ด์ œ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Docker ๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ ์ปจํ…Œ์ด๋„ˆ์˜ ์—ฐ๊ฒฐ์„ ๋Š์„ ๋•Œ๋Š”ย docker network disconnectย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

$ docker network disconnect bridge one


๋‘๋ฒˆ์งธ ์ปจํ…Œ์ด๋„ˆ ์—ฐ๊ฒฐ

ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋”ย our-netย ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š”ย --networkย ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•  ๋„คํŠธ์›Œํฌ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

$ docker run -itd --name two --network our-net busybox
0e7fe8a59f9d3f8bd545d3e557ffd34100a09b8ebe92ae5a375f37a5d072873d


์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ๋„คํŠธ์›Œํ‚น

์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์„ ํ˜ธ์ŠคํŠธ๋„ค์ž„(hostname)์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker exec one ping two
PING two (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.119 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.105 ms
64 bytes from 172.19.0.3: seq=2 ttl=64 time=0.116 ms
64 bytes from 172.19.0.3: seq=3 ttl=64 time=0.883 ms
64 bytes from 172.19.0.3: seq=4 ttl=64 time=0.127 ms


๋„คํŠธ์›Œํฌ ์ œ๊ฑฐ

docker network rmย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œย our-netย ๋„คํŠธ์›Œํฌ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žˆ์„ ๋•Œ๋Š” ์ œ๊ฑฐ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํ•ด๋‹น ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐ๋˜์–ด ์‹คํ–‰ ์ค‘์ธ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€์‹œํ‚ค๊ณ , ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ญ์ œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

# ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€
$ docker stop one two
one
two

# ๋„คํŠธ์›Œํฌ ์‚ญ์ œ
$ docker network rm our-net
our-net


๋„คํŠธ์›Œํฌ ์ฒญ์†Œ

docker network pruneย ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ํ•œ๋ฒˆ์— ๋ชจ๋‘ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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


๋„คํŠธ์›Œํฌ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ 

๐ŸŽฏ ์ด๋ฏธ์ง€ ๊ด€๋ จ ๋ช…๋ น์–ด

Docker CLI ๋„๊ตฌ๋Š” Docker ์ด๋ฏธ์ง€(image)์˜ ํšจ๊ณผ์ ์ธ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ๋‹ค์–‘ํ•œ ์ปค๋งจ๋“œ(command)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฏธ์ง€ ์กฐํšŒ

docker images ์ปค๋งจ๋“œ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์ด๋ฏธ์ง€ ์กฐํšŒ
$ docker images

## ํŠน์ • ๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ(repository)์— ํ•ด๋‹นํ•˜๋Š” ์ด๋ฏธ์ง€๋งŒ ํ•„ํ„ฐ๋งํ•ด์„œ ๋ณด๊ณ  ์‹ถ์„ ๋•Œ๋Š”
## ๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
$ docker images node
node                10-alpine           34a10d47f150        2 weeks ago         83.5MB
node                12-alpine           f77abbe89ac1        2 weeks ago         88.1MB

# ํƒœ๊ทธ๊นŒ์ง€ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด ํŠน์ • ์ด๋ฏธ์ง€๊ฐ€ ์กฐํšŒ๋ฉ๋‹ˆ๋‹ค.
$ docker images node:12-alpine
node                12-alpine           f77abbe89ac1        2 weeks ago         88.1MB


์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ

docker pull ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

run ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ์—†์„ ๋•Œ ์ž๋™์œผ๋กœ ๋‹ค์šด๋ฐ›์œผ๋‹ˆย pull ๋ช…๋ น์–ด๋ฅผ ์–ธ์ œ ์“ฐ๋Š”์ง€ ๊ถ๊ธˆํ•  ์ˆ˜ ์žˆ๋Š”๋ฐย pull์€ ์ด๋ฏธ์ง€๋ฅผ ์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ๋‹ค์‹œ ๋‹ค์šด ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํƒœ๊ทธ์ง€๋งŒ ์ด๋ฏธ์ง€๊ฐ€ ์—…๋ฐ์ดํŠธ ๋œ ๊ฒฝ์šฐ๋Š”ย pull ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ์ƒˆ๋กœ ๋‹ค์šด๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์ด๋ฏธ์ง€ ๋‹ค์šด
$ docker pull ubuntu:14.04


์ด๋ฏธ์ง€ ๋นŒ๋“œ

docker buildย ์ปค๋งจ๋“œ๋Š” Docker ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œ(build)ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Docker ๋ฐ๋ชฌ์ด ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๋ ค๋ฉด ์†Œ์œ„ ์ปจํ…์ŠคํŠธ(context)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” Dockerfile์„ ๋‹ด๊ณ  ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Dockerfile์€ ๋ณดํ†ต ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ์žˆ๋Š” ์„ค์ • ํŒŒ์ผ์ด๋‚˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€์˜ ๋นŒ๋“œ ์ ˆ์ฐจ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

# ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ธฐ์ค€ ์ด๋ฏธ์ง€ ๋นŒ๋“œ
$ docker build .

# ๋„คํŠธ์›Œํฌ ์ƒ์˜ Git repository ์ฃผ์†Œ๋‚˜ ์••์ถ• ํŒŒ์ผ ๋นŒ๋“œ
$ docker build github.com/creack/docker-firefox

# -t ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Docker ์ด๋ฏธ์ง€์— ํƒœ๊ทธ(tag)๋ฅผ ์„ค์ •
## ํƒœ๊ทธ๋Š” ๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ(repository):ํƒœ๊ทธ(tag) ํ˜•ํƒœ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ
## ํƒœ๊ทธ ๋ถ€๋ถ„์„ ์ƒ๋žตํ•˜๋ฉด latest๊ฐ€ ํƒœ๊ทธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
$ docker build -t test:1.0 .

# -f ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋””ํดํŠธ์ธ Dockerfile ๋Œ€์‹ ์— ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ๋ถ€ํ„ฐ Docker ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œ
$ docker build -f Dockerfile.dev .


์ด๋ฏธ์ง€ ํƒœ๊น…

์ด๋ฏธ์ง€ ํƒœ๊น…์€ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œ ํ•œ ํ›„์— ๋ณ„๋„๋กœ ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ด ๋•Œย docker tagย ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์ด๋ฏธ์ง€ID, ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ ๋˜๋Š” ํƒœ๊ทธ๊นŒ์ง€ ๋„˜๊ฒจ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

$ docker tag 8bae72379b48 test:2.0


์ด๋ฏธ์ง€ ์‚ญ์ œ

docker rmiย ์ปค๋งจ๋“œ๋กœ Docker ์ด๋ฏธ์ง€๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œํ•˜๋ ค๋Š” ์ด๋ฏธ์ง€์˜ ID๋‚˜ ํƒœ๊ทธ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ํ•ด๋‹น ์ด๋ฏธ์ง€๊ฐ€ ์‚ญ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

# ์ด๋ฏธ์ง€ ์‚ญ์ œ
$ docker rmi f77abbe89ac1
$ docker rmi node:12-alpine

# ์ด๋ฏธ์ง€ ์กฐํšŒ
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test2               latest              8bae72379b48        56 minutes ago      5.6MB
test                latest              8bae72379b48        56 minutes ago      5.6MB

# -f ์˜ต์…˜์„ ์ฃผ๊ณ  ์ด๋ฏธ์ง€ ID๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด ์ด๋ฏธ์ง€ ์‚ญ์ œ
$ docker rmi -f 8bae72379b48
Untagged: test2:latest
Untagged: test:latest
Deleted: sha256:8bae72379b4

# ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ด๋ฏธ์ง€ ์ „์ฒด ์‚ญ์ œ
$ docker rmi $(docker images -q) -f


์ด๋ฏธ์ง€ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ 

๐Ÿ™‡๐Ÿปโ€โ™‚๏ธ ์ฐธ๊ณ ์‚ฌ์ดํŠธ

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ