๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming/Docker & Kubernetes

[Docker] (4) docker pull(์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ) & docker run(์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ/์‹คํ–‰) & ํฌํŠธํฌ์›Œ๋”ฉ(ํฌํŠธ๋งคํ•‘)

by ๋ญ…์ฆค 2023. 8. 20.
๋ฐ˜์‘ํ˜•
Docker ์ „์ฒด ํฌ์ŠคํŒ…
๋”๋ณด๊ธฐ

[Docker] (1) ๋„์ปค ๊ฐœ๋… ์„ค๋ช… | ๋„์ปค๋Š” ์™œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ๊นŒ?

[Docker] (2) ๋„์ปค & ๋„์ปค์ปดํฌ์ฆˆ ๋‹ค์šด๋กœ๋“œ

[Docker] (3) ๋„์ปค ํ•ต์‹ฌ ๊ฐœ๋… - ์ด๋ฏธ์ง€, ์ปจํ…Œ์ด๋„ˆ, ๋„์ปค ํ—ˆ๋ธŒ

[Docker] (4) docker pull(์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ) & docker run(์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ/์‹คํ–‰) & ํฌํŠธํฌ์›Œ๋”ฉ(ํฌํŠธ๋งคํ•‘)

[Docker] (5) ๋„์ปค run ๋ช…๋ น์–ด ์˜ต์…˜, ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•

[Docker] (6) ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋กœ ์ ‘์†ํ•˜๊ธฐ - docker exec & docker attach

[Docker] (7) ๋„์ปค volume(๋ณผ๋ฅจ) ์˜ต์…˜ - ์ปจํ…Œ์ด๋„ˆ ํด๋”์™€ ํ˜ธ์ŠคํŠธ ํด๋” ์—ฐ๊ฒฐํ•˜๊ธฐ

[Docker] (8) ๋„์ปค commit(์ปค๋ฐ‹) & push(ํ‘ธ์‹œ) - ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒˆ๋กœ์šด ๋„์ปค ์ด๋ฏธ์ง€๋กœ ์ €์žฅํ•˜๊ณ  ๋ ˆํผ์ง€ํ† ๋ฆฌ์— ์—…๋กœ๋“œ

[Docker] (9) Dockerfile(๋„์ปคํŒŒ์ผ) - ์Šค๋งˆํŠธํ•˜๊ฒŒ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•

[Docker] (10) Dockerfile ๋นŒ๋“œ ์˜ˆ์‹œ | ENTRYPOINT ์™€ CMD์˜ ์ฐจ์ด

[Docker] (11) ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์™€ vscode ์—ฐ๋™ํ•˜๊ธฐ | ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ฐœ๋ฐœ

[Docker] (12) ๊นƒํ—ˆ๋ธŒ๋กœ ๋„์ปค ์ด๋ฏธ์ง€ ๊ณต์œ ํ•˜๊ธฐ | Packages Container Registry

 

 

Docker ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

 

์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋„์ปค ์ด๋ฏธ์ง€์™€ ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด๋ฅผ ์ •๋ฆฌํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋ ค ํ•œ๋‹ค. 

  • ๋„์ปค ์ด๋ฏธ์ง€ ๊ด€๋ จ ๋ช…๋ น์–ด
    • ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰: docker search [์ด๋ฏธ์ง€_์ด๋ฆ„]
    • ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ: docker pull [์ด๋ฏธ์ง€_์ด๋ฆ„]:[ํƒœ๊ทธ]
    • ์ด๋ฏธ์ง€ ๋ชฉ๋ก ๋ณด๊ธฐ: docker images
    • ์ด๋ฏธ์ง€ ์‚ญ์ œ: docker rmi [์ด๋ฏธ์ง€_์ด๋ฆ„]:[ํƒœ๊ทธ]
  • ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ จ ๋ช…๋ น์–ด
    • ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰: docker run [์ด๋ฏธ์ง€_์ด๋ฆ„]:[ํƒœ๊ทธ]
      • '-d' : ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰
      • '-p' : ํ˜ธ์ŠคํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ํฌํŠธ ๋งคํ•‘์„ ์„ค์ •
    • ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ๋ณด๊ธฐ: docker ps
      • '-a' : ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ๋ณด๊ธฐ (stop๋œ ๊ฒƒ๊นŒ์ง€)
    • ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€: docker stop [์ปจํ…Œ์ด๋„ˆ_ID] ๋˜๋Š” [์ปจํ…Œ์ด๋„ˆ_์ด๋ฆ„]
    • ์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘: docker start [์ปจํ…Œ์ด๋„ˆ_ID] ๋˜๋Š” [์ปจํ…Œ์ด๋„ˆ_์ด๋ฆ„]
    • ์ปจํ…Œ์ด๋„ˆ ์žฌ์‹œ์ž‘: docker restart [์ปจํ…Œ์ด๋„ˆ_ID] ๋˜๋Š” [์ปจํ…Œ์ด๋„ˆ_์ด๋ฆ„]
    • ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ: docker rm [์ปจํ…Œ์ด๋„ˆ_ID] ๋˜๋Š” [์ปจํ…Œ์ด๋„ˆ_์ด๋ฆ„]
    • ์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ๋ณด๊ธฐ: docker logs [์ปจํ…Œ์ด๋„ˆ_ID] ๋˜๋Š” [์ปจํ…Œ์ด๋„ˆ_์ด๋ฆ„]

 

 

docker pull (๋„์ปค ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ)

docker pull httpd # ๋„์ปค ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
  • ์›ํ•˜๋Š” ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋„์ปค ํ—ˆ๋ธŒ์—์„œ ๊ฒ€์ƒ‰ํ•˜๊ณ  docker pull ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์˜ˆ์‹œ์—์„œ๋Š” 'httpd' ๋ผ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•ด๋ณด๋ ค ํ•œ๋‹ค. (docker pull httpd)

 

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

docker images 	# ์กด์žฌํ•˜๋Š” ๋„์ปค ์ด๋ฏธ์ง€ ์ •๋ณด ์ถœ๋ ฅ

docker images ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•œ httpd ์ด๋ฏธ์ง€

  • ๋‹ค์šด๋กœ๋“œํ•œ ๋„์ปค ์ด๋ฏธ์ง€๋Š” ๋„์ปค ๋ฐ์Šคํฌํƒ‘์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

docker run (๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ)
docker run httpd 		# ๋„์ปค ์ด๋ฏธ์ง€ ์‹คํ–‰ -> ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ (๋กœ๊ทธ ์ถœ๋ ฅ O)
docker run -d httpd  	# ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์ƒ์„ฑ (๋กœ๊ทธ ์ถœ๋ ฅ X)
  • ๋‹ค์šด๋กœ๋“œํ•œ ๋„์ปค ์ด๋ฏธ์ง€๋Š” docker run ์ด๋ผ๋Š” ๋ช…๋ น์–ด๋กœ ์‹คํ–‰ํ•ด์•ผ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
  • ์ด๋ฏธ์ง€๋Š” ํ•˜๋‚˜์ด๋”๋ผ๋„ ์ปจํ…Œ์ด๋„ˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œ ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.
docker ps 		# ์‹คํ–‰๋œ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ •๋ณด ์ถœ๋ ฅ 
docker ps -a  	# ์กด์žฌํ•˜๋Š” ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ •๋ณด ์ถœ๋ ฅ (stop๋œ ํ”„๋กœ์„ธ์Šค๋„ ์ถœ๋ ฅ)

docker ps ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•œ ์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ

  • ์ƒ์„ฑํ•œ ์ปจํ…Œ์ด๋„ˆ๋Š” docker ps ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ ID, ์ด๋ฏธ์ง€, ํฌํŠธ ๋„˜๋ฒ„ ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ๋กœ ์ ‘์†

์ƒ์„ฑ๋œ ํฌํŠธ๊ฐ€ 80๋ฒˆ์ด๋ฏ€๋กœ localhost:80์œผ๋กœ ์ ‘์†์„ ์‹œ๋„ํ•ด ๋ณด์ง€๋งŒ, ์œ„ ์บก์ฒ˜์ฒ˜๋Ÿผ ์—ฐ๊ฒฐ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ?

80๋ฒˆ ํฌํŠธ๋Š” ์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ์˜ ๋‚ด๋ถ€ ํฌํŠธ ๋ฒˆํ˜ธ์ด๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ localhost:80์œผ๋กœ ์ ‘์†ํ•˜๋ฉด ํ˜ธ์ŠคํŠธ์˜ 80๋ฒˆ ํฌํŠธ๋กœ ์ ‘์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋•Œ๋ฌธ์— ํ˜ธ์ŠคํŠธ์˜ ํฌํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ํฌํŠธํฌ์›Œ๋”ฉ์ด ํ•„์š”ํ•˜๋‹ค!

 

์ปจํ…Œ์ด๋„ˆ stop

ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•˜๊ธฐ ์ „์— ์ด์ „์— ์‹คํ–‰ํ•œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ stopํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด์ž.

docker stop {์ข…๋ฃŒํ•  ์ปจํ…Œ์ด๋„ˆ์˜ ์ปจํ…Œ์ด๋„ˆ ID ์•ž ์ผ๋ถ€๋ถ„}

์šฐ์„  docker ps๋กœ ์‹คํ–‰๋œ ์ปจํ…Œ์ด๋„ˆ์˜ ์ปจํ…Œ์ด๋„ˆ ID๋ฅผ ํ™•์ธํ•ด์ฃผ๊ณ  docker stop์œผ๋กœ ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์Šคํƒ‘์‹œํ‚จ๋‹ค. ์ด ๋•Œ ์ปจํ…Œ์ด๋„ˆ ID์˜ ์•ž ์ผ๋ถ€๋ถ„๋งŒ ์ ์–ด์ค˜๋„ ๋ช…๋ น ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (๋ฌผ๋ก  ์ค‘๋ณต๋˜๋Š” ID๊ฐ€ ์—†์„ ๋•Œ)

 

 

ํฌํŠธํฌ์›Œ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

์ด์ œ ํฌํŠธํฌ์›Œ๋”ฉ์„ ํ•ด์ค€ ์ƒํƒœ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž.

docker run -d -p 8080:80 httpd	# ํ˜ธ์ŠคํŠธ 8080ํฌํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ 80 ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐ

 

docker run ์ดํ›„์˜ -d๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์˜ต์…˜์ด๊ณ , -p๋Š” ํฌํŠธํฌ์›Œ๋”ฉ์„ ์œ„ํ•œ ์˜ต์…˜์ด๋‹ค. 8080:80 ์ด๋ผ๊ณ  ์ž…๋ ฅํ•˜๋ฉด ํ˜ธ์ŠคํŠธ 8080ํฌํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ 80 ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋œ๋‹ค.

 

ํฌํŠธํฌ์›Œ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ํ›„ docker ps๋ฅผ ์ด์šฉํ•œ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ

์ดํ›„ docker ps๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ํ™•์ธํ•˜๋ฉด PORTS ๋ž€์— 0.0.0.0:8080 -> 80 ์œผ๋กœ ํฌํŠธํฌ์›Œ๋”ฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹ค์ œ๋กœ localhost:8080 ์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๋„์ปค ๋ฐ์Šคํฌํƒ‘ ์ด์šฉ

๋ฌผ๋ก  ์ด ๋ชจ๋“  ๊ณผ์ •์„ GUI๊ฐ€ ์žˆ๋Š” Docker Desktop์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋„์ปค ํ—ˆ๋ธŒ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋„์ปค ๋ฐ์Šคํฌํƒ‘์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์›ํ•˜๋Š” ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Images์—์„œ ์›ํ•˜๋Š” ์ด๋ฏธ์ง€์˜ ์‹คํ–‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด docker run ๋ช…๋ น์–ด์™€ ๊ฐ™์€ ํšจ๊ณผ๋กœ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋จ
  • ์ดํ›„ Optional Settings ์ฐฝ์ด ๋– ์„œ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„, ํฌํŠธํฌ์›Œ๋”ฉ ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

 

  • Containers ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ์‹คํ–‰ํ•œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
๋ฐ˜์‘ํ˜•