[docker] Dockerfile을 사용한 코드에 의한 서버 구축 (1)

도커 > Dockerfile 작성

develop, docker
written byzuhern1zuhern

in

2021. 02. 15


공부하는 책
완벽한 IT 인프라 구축을 위한 Docker (2판)
복습을 위해 요약한 것.

목표

docker 책 5장 내용 숙지

  • Docker 인프라의 구성 관리를 Dockerfile에 기술한다.
  • Dockerfile을 사용한 서버 구축 및 구성 관리의 방법 학습

5.1 Dockerfile을 사용한 구성 관리

5.1.1 Dockerfile 이란?

Docker 상에서 작동시킬 컨테이너의 구성 정보를 기술

  • 베이스가 될 Docker 이미지
  • Docker 컨테이너 안에서 수행한 조작(명령)
  • 환경변수 등의 설정
  • Docker 컨테이너 안에서 작동시켜둘 데몬 실행

docker build 명령은 Dockerfile에 기술된 구성 정보를 바탕으로 Docker 이미지를 작성합니다.

5.1.2 Dockerfile의 기본 구문

  • 텍스트 형식의 파일
  • 확장자는 필요 없음
  • 다른 이름으로 작성하면, 빌드할 때 명시해야 함
  • 명령 인수
  • 명령은 관례적으로 대문자로 통일
  • # 주석

5.1.3 Dockerfile 작성

  • Dockerfile에는 ‘Docker컨테이너를 어떤 Docker이미지(베이스이미지)로 부터 생성할지’ 반드시 기술

    # FROM 명령  
    FROM [이미지명]:[태그명]    # 태그생략 시 latest
    FROM [이미지명]@[다이제스트] # 이미지를 특정  
    # centOS를 베이스로 한 Dockerfile
    FROM centos:centos7

5.2 Dockerfile의 빌드와 이미지 레이어

5.2.1 Dockerfile로부터 Docker 이미지 만들기

  • (1) Build

    # build  
    docker build -t [생성할 이미지명]:[태그명] [Dockerfile의 위치]  
    % mkdir sample && cd $_
    # Dockerfile 생성
    % touch Dockerfile
    # Docker 이미지 생성
    % docker build -t sample:1.0 /Users/zuhern/workspace/sample
    [+] Building 14.3s (5/5) FINISHED
    => [internal] load build definition from Dockerfile  
    => [1/1] FROM docker.io/library/centos:centos7@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e   
    => exporting to image                                
    => => exporting layers                               
    => => writing image sha256:9e77deb8bdce3898a8a70b0501d02ae5010bc774c2c8f20bd6ae54643a1f6f0e   
    => => naming to docker.io/library/sample:1.0

    얜 어디로 갔을까..?

    % docker image ls
    REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
    nginx                    latest    298ec0e28760   7 days ago      133MB
    ubuntu                   latest    f63181f19b2f   3 weeks ago     72.9MB
    sample                   1.0       9e77deb8bdce   3 months ago    204MB
    docker/getting-started   latest    3c156928aeec   10 months ago   24.8MB

    sample 이 생성되었다.
    근데 왜 3개월 전 생성일까?
    Docker hub > centos에 가보면 태그 centos7 는 3개월전 생성되었음을 알 수 있다.

  • (2) 다시 build

    % docker build -t sample:2.0 /Users/zuhern/workspace/sample
    # -t : Name and optionally a tag in the 'name:tag' format

    같은 Dockerfile로 Tag 2.0 으로 다시 생성하면 이미 받은 이미지이기에 다시 다운받지 않음.

  • Dockerfile.base 라는 파일명을 target으로 build

    % docker build -t sample -f Dockerfile.base
    # -t : Name and optionally a tag in the 'name:tag' format
    # -f : Name of the Dockerfile (Default is 'PATH/Dockerfile')
  • (3) 표준 입력에서의 빌드

    % docker build -< Dockerfile
    # Dockerfile을 docker build의 인수로 전달하므로 -(하이픈)을 지정  
  • (4) 압축 아카이브에 의한 표준 입력에서의 빌드

    # docker.tar.gz 안에 Dockerfile, dummyfile
    % docker build - < docker.tar.gz
    [+] Building 2.2s (5/5) FINISHED
    => [internal] load remote build context              
    => copy /context /  
    => [internal] load metadata for docker.io/library/centos:centos7   
    => CACHED [1/1] FROM docker.io/library/centos:centos7@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e 
    => exporting to image                                
    => => exporting layers                               
    => => writing image sha256:9e77deb8bdce3898a8a70b0501d02ae5010bc774c2c8f20bd6ae54643a1f6f0e

5.2.2 Docker 이미지의 레이어 구조

  • (1) Dockerfile 생성

    # 4개의 명령어로 되어 있는 Dockerfile의 예  
    # step1: ubuntu (base image)
    FROM ubuntu:latest  
    # step2: nginx 
    RUN atp-get update && apt-get install -y -q nginx
    # step3: copy index.html
    COPY index.html /usr/share/nginx/html/
    # step4: start nginx
    CMD ["nginx", "-g", "daemon off;"]
  • (2) 동일 폴더내에 index.html 생성
  • (3) build

    docker build -t webap .
    => [internal] load build definition from Dockerfile  
    => [internal] load .dockerignore                     
    => [internal] load metadata for docker.io/library/ubuntu:latest               
    => CACHED [1/3] FROM docker.io/library/ubuntu:latest 
    => [internal] load build context                     
    => [2/3] RUN apt-get update && apt-get install -y -q nginx 
    => [3/3] COPY index.html /usr/share/nginx/html/      
    => exporting to image   
    => => exporting layers                                         
    => => writing image sha256:9a3a68f442da228565cd867a4ebd79eeb104e09dafbef2cf3d5bf91612944fc3 
    => => naming to docker.io/library/webap

    책의 로그와는 좀 다르다..
    layer별 이미지id가 출력되지는 않았음.
    exporting layers << 이 로그를 보아하니 layer가 생성되는 것은 맞는듯.