728x90

Dockerfile 빌드


docker build -t [생성할 이미지명]:[태그명] [Dockerfile의 위치]

Dockerfile로부터 이미지 생성

  • Dockerfile 생성
# test 디렉토리에 Dockerfile 생성
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test
[root@localhost test]# touch Dockerfile
[root@localhost test]# ls
Dockerfile

 

  • Dockerfile 내용
[root@localhost test]# cat Dockerfile
FROM centos:centos7

 

  • docker build 실행
  • test: 이미지명
  • 1.0: 태그명
  • /root/test: Dockerfile이 위치한 경로
[root@localhost ~]# docker build -t test:1.0 /root/test
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM centos:centos7
centos7: Pulling from library/centos
Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Status: Downloaded newer image for centos:centos7
 ---> eeb6ee3f44bd
Successfully built eeb6ee3f44bd
Successfully tagged test:1.0

[root@localhost ~]# docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
test           1.0       eeb6ee3f44bd   9 months ago   204MB

 

  • 이미지 확인
[root@localhost ~]# docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
test           1.0       eeb6ee3f44bd   9 months ago   204MB

 

베이스 이미지가 로컬에 저장되어 있는 경우 이미지 생성

  • 두 번째 이후는 베이스 이미지를 Docker 레지스터에서 다운로드 하지 않음
[root@localhost ~]# docker build -t test:2.0 /root/test
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM centos:centos7
 ---> eeb6ee3f44bd
Successfully built eeb6ee3f44bd
Successfully tagged test:2.0

 

  • 이미지 확인
  • 베이스 이미지와 작성한 두 개의 이미지의 IMAGE ID가 동일함 > 이름은 다르지만 모두 동일한 이미지임을 알 수 있음
[root@localhost ~]# docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
centos         centos7   eeb6ee3f44bd   9 months ago   204MB
test           1.0       eeb6ee3f44bd   9 months ago   204MB
test           2.0       eeb6ee3f44bd   9 months ago   204MB

 


 

멀티 스테이지 빌드


Docker 멀티 스테이지 빌드란?

컨테이너 이미지를 만들면서 빌드 등에는 필요하지만 최종 컨테이너 이미지에는 필요 없는 환경을 제거할 수 있도록 단계를 나누어 베이스 이미지를 만드는 방법

  • Dockerfile 빌드

[출처] https://www.metricfire.com/blog/how-to-build-optimal-docker-images/

 

  • 멀티 스테이지 빌드
  • 하나의 Dockerfile을 실행하여 빌드 이미지와 실행 이미지 분리가 가능해짐
  • 배포 이미지의 용량 감소, 빌드 시간 감소
  • 보다 가벼운 크기의 컨테이너 생성

[출처] https://www.metricfire.com/blog/how-to-build-optimal-docker-images/

 

728x90

+ Recent posts