Skip to content

Latest commit

ย 

History

History
85 lines (64 loc) ยท 5.63 KB

Docker.md

File metadata and controls

85 lines (64 loc) ยท 5.63 KB

Docker

Docker ๋ฐฐํฌ ์ˆœ์„œ, DockerFile ์ž‘์„ฑ ๋“ฑ ์ „๋ฐ˜์ ์ธ ๋‚ด์šฉ์„ ๊ธฐ๋ก๐Ÿ“

Docker๋ฅผ ํ™œ์šฉํ•œ ๋ฐฐํฌ ์ˆœ์„œ

  1. Dockfile ์ž‘์„ฑ
  2. image ์ƒ์„ฑ
  3. image๋ฅผ container registry์— ์—…๋กœ๋“œ
  4. ์„œ๋ฒ„์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์‹คํ–‰

Dockerfile ๊ธฐ๋ณธ ๋ช…๋ น์–ด

๊ทธ ์™ธ ๋ช…๋ น์–ด๋Š” "์ฐธ๊ณ " ไธญ 1๋ฒˆ์งธ ๋งํฌ๋ฅผ ์ฐธ๊ณ 

  • FROM : ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€์˜ ๋ฒ ์ด์Šค๊ฐ€ ๋˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด๋กœ, ๋ฐ˜๋“œ์‹œ ์ง€์ •ํ•ด์•ผํ•˜๋Š” ๊ฐ’์ด๋ฉฐ ๋ฐ˜๋“œ์‹œ ํ•œ ๋ฒˆ์ด์ƒ ์ž…๋ ฅํ•ด์•ผ ํ•จ
    • ex) FROM golang:1.19-alpine
  • MAINTAINER : ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•œ ๊ฐœ๋ฐœ์ž ๋“ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ช…๋ น์–ด
  • COPY : ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์— ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ํŒŒ์ผ์„ Docker ์ด๋ฏธ์ง€ ๋‚ด๋ถ€๋กœ ๋ณต์‚ฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด
    • ex) COPY ~/model ./model
  • RUN : ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ํŠน์ • ํŒŒ์ผ์„ ์‹คํ–‰ํ•ด์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž„. Dockfile๋กœ๋ถ€ํ„ฐ ๋„์ปค์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๋Š” ์ˆœ๊ฐ„ ์‹คํ–‰๋จ.
    • ex) RUN apt-get update
  • CMD : ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๋ช…๋ น์–ด๋กœ ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • WORKDIR : shell์˜ cd ๋ช…๋ น๋ฌธ๊ณผ ๊ฐ™์ด ์ปจํ…Œ์ด๋„ˆ์ƒ์—์„œ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ „ํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด
  • EXPOSE : Dockerfile ๋นŒ๋“œ๋กœ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์—์„œ ๋…ธ์ถœํ•  ํฌํŠธ๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด
    • ex) EXPORT 8080
  • ENV : ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด

Dockerignore ํŒŒ์ผ

์ƒ์„ธ ๋‚ด์šฉ์€ "์ฐธ๊ณ " ไธญ 6๋ฒˆ์งธ ๋งํฌ๋ฅผ ์ฐธ๊ณ 

  • Dockerfile๋กœ ์ด๋ฏธ์ง€ ๋นŒ๋“œ ์‹œ, ์–ด๋–ค ํŒŒ์ผ์„ ์ œ์™ธ์‹œํ‚ฌ ๊ฒƒ์ธ์ง€ ๋ช…์‹œํ•˜๋Š” ํŒŒ์ผ์ž„
    • ํ•˜๋‚˜์˜ ๋„๊ตฌ๋กœ์จ, ํ•„์š”ํ•œ Docker ๋นŒ๋“œ ๋‚ด์šฉ์„ ์ •์˜ํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉ๋จ
  • .dockerignore ํŒŒ์ผ์— ํฌํ•จ๋œ ํŒŒ์ผ๋“ค๊ณผ ํด๋”๋“ค์€ ๋นŒ๋“œ ๋‚ด์šฉ์— ํฌํ•จ๋˜์ง€ ์•Š๊ฒŒ ๋จ
    • .gitignore ํŒŒ์ผ๊ณผ ๋น„์Šทํ•˜๊ฒŒ, git์— ์ž‘์—…ํ•˜์—ฌ ์˜ฌ๋ฆด ๋•Œ gitignore์— ์˜ฌ๋ฆฌ์ง€ ๋ง์•„์•ผํ•  ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Œ
  • .dockerignore ํŒŒ์ผ์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด, ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋นŒ๋“œ ์†๋„๋„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ

์‹ค์Šต

  • ์•„๋ž˜ "์ฐธ๊ณ " ไธญ 3๋ฒˆ์งธ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ, ๊ธฐ์ดˆ ์‹ค์Šต์„ ํ•™์Šตํ–ˆ์Œ๐Ÿ“
  • docker build๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ "Hello golang..."์„ ์ถœ๋ ฅํ•˜๋Š” gohello ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜์˜€์Œ.

์˜ˆ์ œ2: ๊ฐ„๋‹จํ•œ ์›น์„œ๋ฒ„ ๋ฐฐํฌ

  • ์•„๋ž˜ "์ฐธ๊ณ " ไธญ 4๋ฒˆ์งธ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ, ์‹ค์Šตํ•˜์˜€์Œ

  • image ์ƒ์„ฑ ๊ด€๋ จ ๋ช…๋ น์–ด

    • docker build --tag <image_name> . : ์›ํ•˜๋Š” ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ docker ์ด๋ฏธ์ง€ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด
    • docker build : default๋กœ latest๋กœ docker ์ด๋ฏธ์ง€ ์ƒ์„ฑ๋จ
    • docker image ls : ํ˜„์žฌ๊ฒฝ๋กœ์—์„œ docker ์ด๋ฏธ์ง€ ๋ชฉ๋ก
    • docker image tag <"source"iamge_name> <new tag-image>: "source" ์ด๋ฏธ์ง€๋กœ, ์ƒˆ๋กœ์šด tag๋ฅผ ๋‹ฌ์€ ์ด๋ฏธ์ง€ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด
    • docker image rm <remove image_name>: ํŠน์ • ์ด๋ฏธ์ง€ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด
  • image > container๋กœ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด

    • docker run <image_name> : container ๋‚ด๋ถ€์—์„œ ํŠน์ • ์ด๋ฏธ์ง€ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด
    • docker run --publish <host_port>:<container_port> <image_name> : container์— ๋Œ€ํ•œ ๋‚ด๋ถ€ ํฌํŠธ๋ฅผ ์™ธ๋ถ€ํฌํŠธ๋กœ ๋…ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” --publish ํ”Œ๋ž˜๊ทธ ์‚ฌ์šฉํ•ด์•ผํ•จ.
      • ์ฆ‰, host์™€ container ํฌํŠธ ์—ฐ๊ฒฐ(ํฌํŠธํฌ์›Œ๋”ฉ)
    • docker run -d -p <host_port>:<container_port> <image_name> : container๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ -d, --detach ํ”Œ๋ž˜๊ทธ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.
    • docker run -d -p <host_port>:<container_port> --name <new_name> <image_name> : new_name์œผ๋กœ container ์ด๋ฆ„ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด
  • ํ”„๋กœ์„ธ์Šค ํ™•์ธ/์ค‘์ง€ํ•˜๋Š” ๋ช…๋ น์–ด

    • docker ps : ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” container ํ”„๋กœ์„ธ์Šค ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ, CONTAINER ID/IMAGE/COMMAND/CREATED/STATUS/PORTS/NAMES๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ๋จ.
    • docker stop <NAMES> : ์ž…๋ ฅํ•œ NAMES์— ํ•ด๋‹นํ•˜๋Š” Container ์ค‘์ง€ํ•˜๋Š” ๋ช…๋ น์–ด (container id๋ฅผ ์ž…๋ ฅํ•ด๋„ ๋จ)
    • docker ps -all : ์‹คํ–‰์ค‘์ด๊ฑฐ๋‚˜ ์ค‘์ง€๋œ ๋ชจ๋“  container๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด, ๋˜๋Š” -a๋ฅผ ์ž…๋ ฅํ•ด๋„ ๋จ

Docker-compose

  • Docker Compose : ์—ฌ๋Ÿฌ ๊ฐœ์˜ Container๋กœ ๊ตฌ์„ฑ๋œ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ์‹คํ–‰ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ž„
    • docker-compose.yml : ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜๋ฉด ๋„์ปค ์‹คํ–‰ ์˜ต์…˜์„ ๋ฏธ๋ฆฌ ์ ์–ด๋‘” ๋ฌธ์„œ๋กœ, ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์‹คํ–‰์„ค์ •์„ ์ ์–ด๋‘” ๋ฌธ์„œ์ž„. ํ„ฐ๋ฏธ๋„์—์„œ ๋„์ปค ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ์ฃฝ์ผ ์ˆ˜ ์žˆ์Œ

Docker Compose๋ฅผ ํ™œ์šฉํ•œ ๋ฐฐํฌ ์ˆœ์„œ

  1. ๊ฐ๊ฐ์˜ container์˜ Dockerfile ์ž‘์„ฑ(๊ธฐ์กด์— ๊ณต๊ฐœ๋œ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ํ•ด๋‹น์—†์Œ)
  2. docker-compose.yml ์ž‘์„ฑ ํ›„, ๊ฐ๊ฐ ๋…๋ฆฝ๋œ container์˜ ์‹คํ–‰ ์ •์˜ ์‹ค์‹œ
  3. docer-compose up cmd๋ฅผ ์‹คํ–‰ํ•˜์—ฌ docker-compose.yml๋กœ ์ •์˜ํ•œ container publishํ•จ

์ฐธ๊ณ