Day 48 Docker(3)

๐Ÿ“‹ย ๊ณต๋ถ€ ๋‚ด์šฉ

Docker Volume

Container์™€ Host ์‹œ์Šคํ…œ์ด ํŠน์ • ํด๋”๋ฅผ ๊ณต์œ ํ•จ์œผ๋กœ์จ, Container๊ฐ€ ์‚ฌ๋ผ์ง€๋”๋ผ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์กดํ•˜๋Š” ๊ธฐ๋Šฅ

Container์™€ ๋ฐ์ดํ„ฐ

  • Container๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋‹ค๊ฐ€ ์ค‘๋‹จ๋˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์‹ค๋จ
  • ์ผํšŒ์„ฑ์œผ๋กœ ๋™์ž‘ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด, ๋ฐ์ดํ„ฐ๊ฐ€ ์˜๊ตฌ์ ์œผ๋กœ ๋ณด๊ด€๋˜์–ด์•ผ ํ•จ
    • ex: MySQL ๋“ฑ๊ณผ ๊ฐ™์€ DB๊ฐ€ container์—์„œ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ
  • ๋ฐ์ดํ„ฐ ๋ณด์กด์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ Docker Volume

Docker Volume ์ •์˜

  • Host ์‹œ์Šคํ…œ ํด๋” -> Docker Container ์‹œ์Šคํ…œ ํด๋”๋กœ mount (=mapping)

    • Host์—์„œ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋ฉด Docker Container ์ชฝ์—๋„ ๋ฐ˜์˜๋จ (์†์„ฑ์— ๋”ฐ๋ผ ๋ฐ˜๋Œ€๋„ ๋ฐ˜์˜๋จ)
  • Docker Container ์ƒํƒœ์™€ ๊ด€๊ณ„ ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ๋ณด๊ด€

    • Container๊ฐ€ ์ค‘๋‹จ๋˜๋”๋ผ๋„ ๋ฐ์ดํ„ฐ๊ฐ€๋‚จ๊ฒŒ ๋จ

mount in file system

  • mount & unmount
  • disk๊ฐ™์€ ๋ฌผ๋ฆฌ์ ์ธ ์žฅ์น˜๋ฅผ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ํŠน์ • ์œ„์น˜์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ
    • ex: ์™ธ์žฅํ•˜๋“œ๋ฅผ ์—ฐ๊ฒฐํ•  ๊ฒฝ์šฐ :E ์ฒ˜๋Ÿผ ์ด ์žฅ์น˜์™€ ์—ฐ๊ฒฐ๋œ ์ƒˆ๋กœ์šด ํด๋”๊ฐ€ ์ƒ๊น€

Docker Volume Type

1. Host Volumes

1
docker run -v {host_file_system_path}:{container_file_system_path}

2. Anonymous Volumes

  • ๋„์ปค๊ฐ€ ์•Œ์•„์„œ ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ์—ฐ๊ฒฐ
1
docker run -v {container_file_system_path}

3. Named Volumes

  • ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ์‹
  • ํ•˜๋‚˜์˜ Volume์„ ๋‹ค์ˆ˜์˜ Container์—์„œ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
  • ๋„์ปค๊ฐ€ ๋งŒ๋“ค์–ด ๋งˆ์šดํŠธํ•˜๋Š” ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ ํด๋”์— ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ
1
2
3
docker run -v {name}:{container_file_system_path}
# readonly volume ์„ค์ • ์˜ต์…˜, default๋Š” ์ฝ๊ธฐ&์“ฐ๊ธฐ ๊ฐ€๋Šฅ
docker run -v {name}:{container_file_system_path}:ro

Image ์ƒ์„ฑ ์‹œ Docker Volume์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

1. Dockerfile

  • VOLUME command๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, anonymous volume๋งŒ ์ง€์ • ๊ฐ€๋Šฅ

2. docker-compose

  • Host Volume์ด๋‚˜ Named Volume์„ ์‚ฌ์šฉ

Docker Volume ์‹ค์Šต

nginx container without volume

1. nginx container๋ฅผ ๋‹ค์šด๋ฐ›๊ณ  ์„œ๋ฒ„๋ฅผ ์‹คํ–‰

  • command
1
2
3
# -d ์˜ต์…˜์œผ๋กœ detachํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋˜๊ฒŒ ํ•จ
# --p 8081:80 ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ ํ•˜์—ฌ ํ˜ธ์ŠคํŠธ์—์„œ 8081 ํฌํŠธ๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
docker run -d --name=nginx -p 8081:80 nginx
  • http://localhost:8081/์— ์—ฐ๊ฒฐํ•œ ์›น ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด

2. ์„œ๋ฒ„์— ์ ‘์†ํ•ด์„œ html ํŒŒ์ผ ์ˆ˜์ •

  • command
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# exec์œผ๋กœ ์‹คํ–‰์ค‘์ธ nginx container์— ์—ฐ๊ฒฐ
# --user=root -it ์œผ๋กœ ์„œ๋ฒ„์— root์œ ์ €๋กœ ์ ‘์†
# sh shell script ์‹คํ–‰
docker exec --user=root -it nginx sh

# -----------------์„œ๋ฒ„ ๋‚ด๋ถ€----------------------
apt update
apt install nano
# ๋‚ด์šฉ์„ Welcome to Docker Volume์œผ๋กœ ์ˆ˜์ •
nano /usr/share/nginx/html/index.html
exit

# ----------------------------------------------
  • ์›น ๋ธŒ๋ผ์šฐ์ € ์—ฐ๊ฒฐ ํ™”๋ฉด

3. ์žฌ์‹คํ–‰ ํ›„ ํŒŒ์ผ ํ™•์ธ

  • Volume์ด ์ง€์ •๋˜์ง€ ์•Š์€ ์ƒํƒœ -> ๋ณ€๊ฒฝ์ด ์ ์šฉ๋˜์ง€ ์•Š๊ณ  ์›๋ž˜๋Œ€๋กœ ๋Œ์•„์™”์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Œ

  • command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Container ์žฌ์‹คํ–‰
docker stop nginx
docker run nginx
docker run -d --name=nginx -p 8081:80 nginx

docker exec --user=root -it nginx sh
# -----------------์„œ๋ฒ„ ๋‚ด๋ถ€----------------------
apt update
apt install nano
# ๋‚ด์šฉ์ด ์ˆ˜์ • ์ „์œผ๋กœ ๋Œ์•„๊ฐ”์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
nano /usr/share/nginx/html/index.html
exit
# ----------------------------------------------
  • ์›น ๋ธŒ๋ผ์šฐ์ € ์—ฐ๊ฒฐ ํ™”๋ฉด

nginx container with volume

1. nginx container๋ฅผ ๋‹ค์šด๋ฐ›๊ณ  ์„œ๋ฒ„๋ฅผ ์‹คํ–‰(๋ณผ๋ฅจ ์‚ฌ์šฉ ์„ค์ •)

  • command
1
2
# -v ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ Host Volumes ๋ฐฉ์‹์œผ๋กœ ์—ฐ๊ฒฐ
docker run -d --name nginx_demo -p 8081:80 -v /home/sarah/devcourse/nginx/html:/usr/share/nginx/html nginx
  • http://localhost:8081/์— ์—ฐ๊ฒฐํ•œ ์›น ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด

index.html

test.html

2. html ํŒŒ์ผ ์ˆ˜์ •

  • host ์‹œ์Šคํ…œ ์ƒ์—์„œ ํŒŒ์ผ์„ ์ง์ ‘ ์ˆ˜์ •
  • ์›น ๋ธŒ๋ผ์šฐ์ € ์—ฐ๊ฒฐ ํ™”๋ฉด

3. ์žฌ์‹คํ–‰ ํ›„ ํŒŒ์ผ ํ™•์ธ

  • host ์‹œ์Šคํ…œ ์ƒ์—์„œ ํŒŒ์ผ ํ™•์ธ
  • ์›น ๋ธŒ๋ผ์šฐ์ € ์—ฐ๊ฒฐ ํ™”๋ฉด

Docker ํ™˜๊ฒฝ ํด๋ฆฐ์—…

์ปจํ…Œ์ด๋„ˆ ์ค‘๋‹จ ํ›„ ์‚ญ์ œ -> ์ด๋ฏธ์ง€ ์‚ญ์ œ

CLI

  1. ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ
1
2
3
4
# -f : force, ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋„ ๋ฐ”๋กœ ์‚ญ์ œ
# ls -a : ์‹คํ–‰์ค‘์ด ์•„๋‹Œ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก๋„ ๊ฐ€์ ธ์˜ด
# ls -aq : ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ์˜ container id๋งŒ ๋ฐ›์•„์˜ด
docker container rm -f $(docker container ls -aq)
  1. ์ด๋ฏธ์ง€ ์‚ญ์ œ
1
docker image rm -f $(docker image ls -q)

Docker Desktop

  • Troubleshoot > Clean/Purge data ๋ฅผ ํ†ตํ•ด ์‰ฝ๊ณ  ์ง๊ด€์ ์ด๊ฒŒ ์‚ญ์ œ ๊ฐ€๋Šฅ

Docker ์‹ค์Šต : ์—ฌ๋Ÿฌ container๋ฅผ ๊ฐ€์ง„ ์†Œํ”„ํŠธ์›จ์–ด

์„ค๋ช… ๋ฐ ๊ตฌ์กฐ

์—ฌ๋Ÿฌ Container๋ฅผ ๊ฐ€์ง„ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ Docker์—์„œ ๋นŒ๋“œํ•œ๋‹ค.

Docker์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ : example-voting-app

voting-app-architecture

์‹ค์Šต : ์ผ์ผ์ด ๋งค๋‰ด์–ผํ•˜๊ฒŒ ์‹คํ–‰

1. Build

1
2
3
4
5
docker build -t vote ./vote
docker build -t result ./result
docker build -t worker ./worker

docker images

2. Run

1
2
3
4
5
docker run -d --name=redis redis
docker run -d -e POSTGRES_PASSWORD=postgres --name=db postgres
docker run -d --name=vote -p 5001:80 vote
docker run -d --name=result -p 5002:80 result
docker run -d --name=worker worker
  • ์ด ๊ฒฝ์šฐ ๊ฐ ์ปดํฌํ„ดํŠธ๋“ค ๊ฐ„ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์ด ๋˜์ง€ ์•Š์Œ

3. ๋„คํŠธ์›Œํฌ ๊ด€๋ จ ์ด์Šˆ ํ™•์ธ

  • vote์— ๋กœ๊ทธ์ธํ•˜์—ฌ iputils-ping ์„ค์น˜ ํ›„ ping
1
2
3
4
docker exec --user=root -it vote sh
apt update
apt install iputils-ping
ping redis

Result of ping redis(failled) in connected network

์‹ค์Šต : ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“ค์–ด ์—ฐ๊ฒฐํ•˜์—ฌ ์‹คํ–‰

๋„คํŠธ์›Œํฌ ์ด์Šˆ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  • docker network ๊ธฐ๋Šฅ ์‚ฌ์šฉ

    • ์ด์ „์—๋Š” docker run - link ์˜ต์…˜์„ ์‚ฌ์šฉํ–ˆ์—ˆ์Œ
  • network๋ฅผ ๋งŒ๋“ค๊ณ , ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋„คํŠธ์›Œํฌ ์•ˆ์œผ๋กœ ์ง€์ •

    • ์—ฐ๊ฒฐ์— ๋”ฐ๋ผ ๋ณ„๊ฐœ์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ๋„ ๊ฐ€๋Šฅ (back-tier, front-tier)

1. Create newtork

  • ๋จผ์ € ์ด์ „์— ์ƒ์„ฑํ•œ container๋“ค์€ ์‚ญ์ œํ•œ๋‹ค. ๋นŒ๋“œํ•œ ์ด๋ฏธ์ง€๋Š” ์‚ญ์ œํ•˜์ง€ ์•Š์•„๋„ ๋จ
1
docker container rm -f $(docker container ls -aq)
  • ๋„คํŠธ์›Œํฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
1
docker network create mynetwork

2. Run in network

  • ์•ž์„œ ์ž…๋ ฅํ•œ ์ปค๋งจ๋“œ์— --network mynetwork ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋„คํŠธ์›Œํฌ ์•ˆ์— ์ง€์ •ํ•œ๋‹ค.
1
2
3
4
5
6
docker run -d --name=redis --network mynetwork redis
# password๋ฅผ Postgres๋กœ ์ฃผ์–ด์•ผ ์„œ๋กœ ์—ฐ๊ฒฐํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
docker run -d --name=db -e POSTGRES_PASSWORD=postgres --network mynetwork postgres
docker run -d --name=vote -p 5001:80 --network mynetwork vote
docker run -d --name=result -p 5002:80 --network mynetwork result
docker run -d --name=worker --network mynetwork worker

Result of ping redis(success) in connected network

3. ๊ฒฐ๊ณผ ํ™”๋ฉด

  • votes

votes web page

  • result

result(of votes) web page that show 2 votes, 100% of votes were dog

๐Ÿ‘€ย CHECK

(์–ด๋ ต๊ฑฐ๋‚˜ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๋“ฑ ๋‹ค์‹œ ํ™•์ธํ•  ๊ฒƒ๋“ค)

Docker review quiz

Ubuntu 22.04์—์„œ Docker๋กœ airflow ์‹คํ–‰ํ•˜๋Š” ๋ฒ•

  • ๊ฐ•์˜์—์„œ ์•Œ๋ ค์ค€๋Œ€๋กœ ์‹คํ–‰ํ–ˆ๋Š”๋ฐ webserver ์—ฐ๊ฒฐ์ด ์•ˆ๋˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ
  • ๋กœ๊ทธ๋ฅผ ๋ณด๊ณ  ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ๊นŒ ๊ถŒํ•œ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์•„์„œ chat gpt์—๊ฒŒ ๋ฌผ์–ด๋ด„
  • airflow ์„ค์ •ํ•˜๋Š”๋ฐ ์“ฐ์ธ docker-compose.yml๋ฅผ ๋ณด๋‹ˆ, user id ์„ธํŒ…์ด airflow id ์„ธํŒ…๊ฐ’์ธ 50000:0 ์ธ๊ฑธ ํ™•์ธ
  • ๋‚ด ํ˜ธ์ŠคํŠธ์˜ uid / gid๋ฅผ ํ™•์ธํ•˜๋‹ˆ (id -u, id -g) 1001:1002์ž„์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Œ
  • ๊ทธ๋ž˜์„œ .yml๋ฅผ ์ˆ˜์ •ํ•˜๊ณ , chown chmod๋กœ host ์‹œ์Šคํ…œ ํŒŒ์ผ ๊ฒฝ๋กœ์— ๊ถŒํ•œ๋„ ์คฌ์Œ
    1
    2
    
    sudo chown -R 1001:1002 /path/to/your/airflow/directory
    sudo chmod -R 775 /path/to/your/airflow/directory
    
  • ๋‹ค์‹œ ๋นŒ๋“œํ•ด์•ผํ•˜๋‚˜ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ ์›น ์„œ๋ฒ„๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜๋”๋‹ˆ ์ ‘์†์ด ๋จ!
  • docker exec์œผ๋กœ ์„œ๋ฒ„ ์ ‘์†ํ•ด์„œ uid/gid ํ™•์ธํ•˜๋‹ˆ๊นŒ 50000:0 ๊ทธ๋Œ€๋กœ์ธ๊ฑธ ๋ด์„  ๊ถŒํ•œ์„ ์ฃผ๋Š”๊ฒŒ ๋‹ต์ด์—ˆ๋˜๋“ฏ

โ— ๋Š๋‚€ ์ 

Hugo๋กœ ๋งŒ๋“ฆ
Jimmy์˜ Stack ํ…Œ๋งˆ ์‚ฌ์šฉ ์ค‘