๐Ÿ“‚ Container & K8S/Docker

[Docker] ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ์™€ ๋„์ปคํŒŒ์ผ

dhyuck 2021. 9. 5. 02:35
๋ฐ˜์‘ํ˜•

docker image build๋Š” Dockerfile๋กœ ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ช…๋ น ์ž…๋‹ˆ๋‹ค.

  • ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ (docker image build)

      docker image build [OPTIONS] PATH | URL | -
      # Build an image from a Dockerfile
      # -t, --tag list : ์ด๋ฏธ์ง€ ์ด๋ฆ„๊ณผ ํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•œ๋‹ค
      # -f, --file string : ๋„์ปคํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ "PATH" ์— ์กด์žฌํ•˜๋Š” Dockerfile
      # --build-arg list : ๋นŒ๋“œ ๋  ๋•Œ ๋ณ€์ˆ˜๊ฐ€ ์„ธํŒ…๋œ๋‹ค. (Set build-time variables)
    
      # ex) ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” "Dockerfile"๋ฅผ "hyuckang_image:1.0" ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œํ•œ๋‹ค.
      docker build -t hyuckang_image:1.0 .
    
      # ex) ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” "hyuckang_Docerfile"๋ฅผ hyuckang_image:2.0 ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œํ•œ๋‹ค.
      sudo docker build -t hyuckang_image:2.0 -f hyuckang_Dockerfile .
    
      # ex) ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” "Dockerfile" ๋ฅผ "hyuckang_image:3.0" ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œํ• ๋•Œ
      # Docekr "ARG ver"์˜ ๊ฐ’์ด 3.0์œผ๋กœ ๋ฎ์–ด ์”Œ์–ด์ง„๋‹ค. 
      sudo docker build -t hyuckang_image:3.0 --build-arg ver=3.0 .

Docker can build images automatically by reading the instructions from a Dockerfile.
๋„์ปค๋Š” DockerFile์— ์žˆ๋Š” ๋ช…๋ น์„ ์ฝ์–ด์„œ ์ž๋™์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • FROM : ๊ธฐ๋ณธ ๋ ˆ์ด์–ด๊ฐ€ ๋˜๋Š” ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

      FROM ubuntu:20.04
  • RUN : ์ด๋ฏธ์ง€๊ฐ€ ๋นŒ๋“œ ๋  ๋•Œ ์‹คํ–‰๋˜๋Š” ๋ช…๋ น์ด๋ฉฐ, ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋ ˆ์ด์–ด ๋‹จ์œ„๋กœ ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

      # RUN <command>
      RUN apt-get update -y
    
      # RUN ["executable", "param1", "param2"]
      RUN ["pwd"]
  • EXPOSE : ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ๋  ์‚ฌ์šฉํ•  ํฌํŠธ๋ฅผ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

    (ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ํฌํŠธ๋ฅผ ์˜คํ”ˆํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•œ ์‚ฌ๋žŒ๊ณผ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•œ ์‚ฌ๋žŒ์‚ฌ์ด์— ์–ด๋–ค ํฌํŠธ๋ฅผ ์—ด์ง€์— ๋Œ€ํ•œ ์ผ์ข…์˜ ๋ฌธ์„œ ์—ญํ• ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™ ํ•  ๋•Œ -p ์˜ต์…˜์œผ๋กœ ํฌํŠธ๋ฅผ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)

      EXPOSE 80
  • CMD : ์ปจํ…Œ์ด๋„ˆ๊ฐ€ run ๋˜๊ฑฐ๋‚˜ start ๋  ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” ๊ธฐ๋ณธ ๋ช…๋ น์ด๋ฉฐ, ๋ฎ์–ด์”Œ์šธ์ˆ˜(Override) ์žˆ์Šต๋‹ˆ๋‹ค.(์‚ฌ์šฉ์ž๊ฐ€ ๋ฌด์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

    ์ด ๋ช…๋ น์€ Dockerfile์— 1๊ฐœ๋งŒ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ๋งˆ์ง€๋ง‰ CMD๊ฐ€ ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

      CMD ["apachectl", "-D", "FOREGROUND"]
  • ENTRYPOINT : ์ด๋ฏธ์ง€๋ฅผ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค. CMD์™€ ๋‹ค๋ฅด๊ฒŒ ๋ฎ์–ด์”Œ์›Œ์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (์‚ฌ์šฉ์ž๊ฐ€ ๋ฌด์‹œ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.)

      ENTRYPOINT ["top"]
  • ENV : ์ด๋ฏธ์ง€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

      ENV version 0.1
  • ARG: ๋„์ปค ํŒŒ์ผ์ด ๋นŒ๋“œ ๋  ๋•Œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž…๋‹ˆ๋‹ค. โ€”build-agr = ๋กœ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      ARG my_version=1.0
  • WORKDIR : ์ด๋ฏธ์ง€์˜ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฒฝ๋กœ๊ฐ€ ์ƒ์„ฑ ๋ฉ๋‹ˆ๋‹ค.

      WORKDIR /var/www
  • COPY : ๋กœ์ปฌ ํ˜ธ์ŠคํŠธ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ ์ด๋ฏธ์ง€ ์•ˆ์œผ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

      COPY /home/hyuckang/index.html /var/www/html
  • ADD : ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜, ์›๊ฒฉ URL ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

      ADD test.txt /home/hyuckang
  • ONBUILD : ๋นŒ๋“œ๊ฐ€ ์™„๋ฃŒ๋œ ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€์˜ ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๊ฐ€ ๋  ๋•Œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

    ์ด๋Š” ๊ธฐ์กด์— ๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ๋งŒ๋“ค๊ณ , ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

      ONBUILD RUN apt-get update
  • HEALTHCHECK : ์ปจํ…Œ์ด๋„ˆ์˜ ํ—ฌ์Šค์ฒดํฌ๋ฅผ ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

    ๋„์ปค ํŒŒ์ผ์—๋Š” 1๊ฐœ์˜ HEALTHCHECK ๋ช…๋ น์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2๊ฐœ ์ด์ƒ์ด ์กด์žฌ ํ•œ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ HEALTHCHECK๋งŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

    ํ—ฌ์Šค์ฒดํฌ์˜ ๊ฒฐ๊ณผ๋Š” docker ps์™€ docker inspect๋กœ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      # --interval=DURATION (default: 30s)
      # --timeout=DURATION (default: 30s)
      # --start-period=DURATION (default: 0s)
      # --retries=N (default: 3)
    
      HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1
  • USER : RUN, CMD, ENTRYPOINT ๋“ฑ์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•  ์œ ์ €๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

  • LABEL : ์ด๋ฏธ์ง€์— ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

      LABEL maintainer="hyuckang15@gmail.com"
  • MAINTAINER : ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ž ํ•„๋“œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (์ด์ œ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ)

  • SHELL : ๊ธฐ๋ณธ ์‰˜์„ ์žฌ์ •์˜ ํ•ฉ๋‹ˆ๋‹ค.


๋ฐ˜์‘ํ˜•