JUST DO IT!

Docker Volume ์•Œ์•„๋ณด๊ณ  ๊ฐ„๋‹จํ•œ ์‹ค์Šตํ•ด๋ณด๊ธฐ - TIL230614 ๋ณธ๋ฌธ

TIL

Docker Volume ์•Œ์•„๋ณด๊ณ  ๊ฐ„๋‹จํ•œ ์‹ค์Šตํ•ด๋ณด๊ธฐ - TIL230614

sunhokimDev 2023. 6. 15. 18:24

๐Ÿ“š KDT WEEK 11 DAY 3 TIL

  • Docker Volume

 

๐ŸŸฅ Docker Volume

 

Docker Container๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋‹ค๊ฐ€ ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์„ ๋Œ€๋น„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•ด๋‘๋Š” ๊ธฐ๋Šฅ

 

Docker Container๋‚ด์˜ ๊ฐ€์ƒ ํŒŒ์ผ ์‹œ์Šคํ…œ๊ณผ ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ๋งตํ•‘(๋งˆ์šดํŠธ)ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

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

 

 

โš™๏ธ ๋ฐฉ๋ฒ•

 

  1. docker run -v /home/USER/logs:/var/lib/airflow/logs
    • ํ˜ธ์ŠคํŠธ ํŒŒ์ผ ์‹œ์Šคํ…œ Path : Container ํŒŒ์ผ ์‹œ์Šคํ…œ Path ๋ฐฉ์‹์œผ๋กœ ์ง์ ‘ ๋งˆ์šดํŠธ
  2.  docker run -v /var/lib/mysql/data
    • ์˜๊ตฌ์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ณ  ์‹ถ์€ Container ํŒŒ์ผ Path๋งŒ ๋ช…์‹œํ•˜๊ณ , ์•Œ์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๋„๋ก ๋ช…๋ นํ•œ๋‹ค.
    • ์ด ๊ฒฝ์šฐ, ์œ ์ง€๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์œ ๋‹ˆํฌํ•œ ID๋ฅผ ๋ฐ›์•„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  3.  docker run -v name:/var/lib/mysql/data
    • 2๋ฒˆ์˜ ๋ฐฉ๋ฒ•์—์„œ ๋งค๋ฒˆ ์œ ๋‹ˆํฌํ•œ ID๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹ค ์—ฌ๊ธฐ์„œ name์„ ๋งŒ๋“ค์–ด ID ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

๐Ÿ†Ž docker volume ๋ช…๋ น์–ด

  • docker volume ls : docker volume๋“ค ํ™•์ธํ•˜๊ธฐ
  • docker volume rm : ํŠน์ • volume ์‚ญ์ œ
  • docker volume prune : ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” volume ์‚ญ์ œ
  • docker volume inspect : ํŠน์ • volume ์ƒ์„ธํžˆ ๋ณด๊ธฐ

 

 

๐ŸŸฆ Docker Volume ์‹ค์Šต

 

docker์—์„œ nginx ๋‹ค์šด๋กœ๋“œ

nginx : ์›น ํŠธ๋ž˜ํ”ฝ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ

 

docker run -d --name=nginx -p 8081:80 nginx

ํ˜ธ์ŠคํŠธ์˜ 8081 ํฌํŠธ์™€ docker์˜ 80 ํฌํŠธ๋ฅผ ํฌํŠธํฌ์›Œ๋”ฉํ•˜๊ณ , ์˜คํ”ผ์…œ ์ด๋ฏธ์ง€์ธ nginx๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„์˜จ๋‹ค.

์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜๋„๋ก ์ด๋ฆ„๋„ nginx๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ ๋’ค localhost:8081๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ๋‹ค์Œ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

nginx์˜ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

 

๋‹ค์‹œ ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ, nignx์— ๋ฃจํŠธ ์œ ์ €๋กœ(nano ๋‹ค์šด๋กœ๋“œ๋ฅผ ์œ„ํ•ด) ์‰˜ ์ ‘์†์„ ์œ„ํ•ด ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

docker exec -it --user=root nginx sh

 

๊ทธ๋ฆฌ๊ณ  ์ฐจ๋ก€๋Œ€๋กœ ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ nano(ํŽธ์ง‘ํˆด)์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š”๋‹ค.

๊ทธ๋ฆฌ๊ณ  nano๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๊นŒ ๋กœ์ปฌํ˜ธ์ŠคํŠธ๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋‚˜์˜จ ํ™”๋ฉด์„ ํŽธ์ง‘ํ•ด๋ณด์ž.

 

apt update
apt install nano
nano /usr/share/nginx/html/index.html

 

๋‚˜๋Š” title ํƒœ๊ทธ์˜ Welcome to nginx! ๋ถ€๋ถ„์„ Welcome to Docker Volume! ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ctrl+x > y ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์„ธ์ด๋ธŒํ•˜์˜€๋‹ค.

 

nano๋กœ index.html์„ ์—ด์—ˆ์„ ๋•Œ์˜ ํ™”๋ฉด

 

์ˆ˜์ •์ด ์™„๋ฃŒ๋œ ํ›„ exit๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์‰˜์—์„œ ๋‚˜๊ฐ„ ๋’ค, ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ฐจ๋ก€๋กœ ์ž…๋ ฅํ•˜์—ฌ docker container๋ฅผ ์žฌ์‹œ์ž‘ํ•ด๋ณด์ž.

 

# nginx ๋ฉˆ์ถ”๊ณ  ์‚ญ์ œ
docker stop nginx
docker rm nginx

# ์žฌ์‹คํ–‰
docker run -d --name=nginx -p 8081:80 nginx
docker exec -it --user=root nginx sh

# ์‰˜ ์ง„์ž… ํ›„, ์•„๊นŒ ์ˆ˜์ •ํ–ˆ๋˜ ํŒŒ์ผ ํ™•์ธ
cat /usr/share/nginx/html/index.html

 

์•„๊นŒ ํŽธ์ง‘ํ•˜๊ณ  ์ €์žฅํ–ˆ๋˜ index.html์„ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋‹ˆ, ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ๋Œ์•„์˜จ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํƒ€์ดํ‹€ ํƒœ๊ทธ๋ฅผ ์ˆ˜์ •ํ–ˆ์ง€๋งŒ, ๊ทธ๋Œ€๋กœ์ด๋‹ค.

 

๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ ์œ ์ง€๋ฅผ ์œ„ํ•ด Docker Volume์ด ํ•„์š”ํ•˜๋‹ค.

์ผ๋‹จ Docker Volume์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด html ํด๋”๋ฅผ docker run nginx๊ฐ€ ์‹คํ–‰๋œ ํด๋”์— ๋งŒ๋“ค์—ˆ๋‹ค.

 

๋‚˜๋Š” html ํด๋” ์•ˆ์— index.html๊ณผ test.html๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋„ฃ์—ˆ๋‹ค.

๊ฐ๊ฐ์˜ ๋‚ด์šฉ์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•ด๋‘์—ˆ๋‹ค.

 

<h1>Hello from Docker Volume!!</h1> <!-- index.html  -->
<h1> Testing... </h1> <!-- test.html  -->

 

๊ทธ๋ฆฌ๊ณ  html์ด ์ €์žฅ๋œ ํด๋”์˜ ๊ฒฝ๋กœ๋ฅผ ํ˜ธ์ŠคํŠธ ํŒŒ์ผ ์‹œ์Šคํ…œ ๊ฒฝ๋กœ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณต์‚ฌํ•ด๋‘”๋‹ค.

 

html์ด ์ €์žฅ๋œ ํด๋”์˜ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค.

 

 

์•„๊นŒ ์‚ฌ์šฉํ•˜๋˜ nginx ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ , ์ด๋ฒˆ์—๋Š” Docker Volume ์˜ต์…˜์„ ๋ถ™์—ฌ docker run์„ ํ•ด๋ณด์ž.

-v ์˜ต์…˜์„ ๋„ฃ๊ณ  ์•„๊นŒ์˜ pwd ๊ฒฝ๋กœ๋ฅผ ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ ํŒŒ์ผ ๊ฒฝ๋กœ๋กœ, ์‰˜ ์•ˆ์—์„œ index.html ํŒŒ์ผ์„ ํŽธ์ง‘ํ–ˆ๋˜ ๊ฒฝ๋กœ๋ฅผ Container ์‹œ์Šคํ…œ ํŒŒ์ผ ๊ฒฝ๋กœ๋กœ ๋‘๊ณ  ๋งˆ์šดํŠธํ•œ๋‹ค.

 

# ์•„๊นŒ ์ผœ๋†จ๋˜ nginx ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ
docker stop nginx
docker rm nginx

# -v ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ๊ฒฝ๋กœ๋ฅผ ๋งˆ์šดํŠธํ•œ๋‹ค.
# ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ ํŒŒ์ผ ๊ฒฝ๋กœ : Container ์‹œ์Šคํ…œ ํŒŒ์ผ ๊ฒฝ๋กœ
docker run -p 8081:80 --name nginx -v C:\Users\USER\kdt\nginx\html:/usr/share/nginx/html nginx

 

์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด localhost:8081๋กœ ์ ‘์†ํ•ด๋ณด์ž.

 

 

๋กœ์ปฌํ˜ธ์ŠคํŠธ์˜ ์ ‘์† ํ™”๋ฉด์ด ๋ฐ”๋€Œ์—ˆ๋‹ค.

 

/test.html ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์•„๊นŒ ์ž‘์„ฑํ•œ test.html๋„ ๋ณด์ธ๋‹ค.

 

์ด๋ฒˆ์—๋Š” ์‰˜์ด ์•„๋‹Œ ๋ฐ–์—์„œ ์ง์ ‘ test.html์„ ์ง์ ‘ ์ˆ˜์ •ํ•œ ๋’ค, localhost:8081/test.html ์„ ํ™•์ธํ•ด๋ณด๋ฉด..

 

์ˆ˜์ •์ด ๋ฐ”๋กœ ๋ฐ˜์˜๋˜์–ด ๋‚˜ํƒ€๋‚œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด๋ฒˆ์—๋Š” ์•„๊นŒ์ฒ˜๋Ÿผ docker container๊ฐ€ ์‚ญ์ œ๋œ ํ›„์—๋„ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ง€๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

 

docker stop nginx
docker rm nginx

docker run -p 8081:80 --name nginx -v C:\Users\USER\kdt\nginx\html:/usr/share/nginx/html nginx

 

๊ทธ๋ฆฌ๊ณ  ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์ ‘์†ํ•ด๋„ ์ˆ˜์ •๋œ ๋‚ด์šฉ์ด ์œ ์ง€๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.