[Docker] 도커 이미지 빌드 (Docker Image Build)
도커 파일 (Dockerfile)
: 컨테이너 빌드를 위한 명령어의 집합, 다양한 인스트럭션을 지원하고 대소문자를 구분하지 않음
❗ 베이스 이미지는 필수
FROM | 컨테이너의 베이스 이미지 (OS) |
LABEL | 컨테이너 빌더의 정보, 버전과 같은 부가정보 제공 |
RUN | 컨테이너 빌드를 위해 base image에서 실행 할 Command |
ADD | 컨테이너 빌드 시 호스트의 파일을(tar, url 포함) 컨테이너로 푸시 |
COPY | 컨테이너 빌드 시 호스트의 파일을 컨테이너로 복사 |
WORKDIR | 컨테이너 빌드 시 명령이 실행될 작업 디렉토리 설정 |
ENV | 환경 변수 지정 |
USER | 명령 및 컨테이너 실행 시 적용할 유저 설정 |
EXPOSE | 컨테이너 동작 시 외부에서 사용할 포트 지정 |
CMD | 컨테이너 동작 시 자동으로 실행 할 서비스나 스크립트 지정 |
ENTRYPOINT | CMD와 함께 사용하면서 명령어 지정 시 사용, 수정 불가 |
이미지 빌드
- centos에 apache http를 설치 한 이미지 빌드
- 이름이 Dockerfile인 파일을 생성하여 내용을 작성
FROM centos:7
LABEL maintainer="name <myemail@gmail.com>"
RUN yum install httpd curl && \
yum clean all
WORKDIR /etc
COPY conf.d/* /etc/httpd/conf.d/
COPY html/* /var/www/html/
EXPOSE 80
USER apache
CMD ["httpd","-DFOREGROUND]
- 현재 디렉토리 위치에 있는 Dockerfile의 이미지를 test:v2라는 이름으로 빌드
docker build -t test:v2 .
docker images test
Docker Hub에 배포
컨테이너 레지스트리
: 컨테이너 이미지 저장소
✔ Docker 레지스트리 구성 요소
- Registry : 컨테이너 이미지를 저장하는 서비스
- Repository : 컨테이너 이미지들의 컬렉션
- Index : 이미지 검색과 태그 및 인증을 담당
도커 허브에 배포
❗ 도커 허브 계정이 있어야함
- 도커 허브 계정명으로 태그를 변경해줌
docker tag test:v2 dockerhub-name/test:v2
- 로그인 후 push
docker login
docker push dockerhub-name/apache-php:latest
- 이미지를 가져오고싶다면 pull
docker pull dockerhub-name/apache-php:latest
docker images
Best Practice
1️⃣ 가벼운 베이스 이미지 사용
- 알파인(alpine)
alpine - Official Image | Docker Hub
About Official Images Docker Official Images are a curated set of Docker open source and drop-in solution repositories. Why Official Images? These images have clear documentation, promote best practices, and are designed for the most common use cases.
hub.docker.com
- 스크래치(scratch)
scratch - Official Image | Docker Hub
This image is most useful in the context of building base images (such as debian and busybox) or super minimal images (that contain only a single binary and whatever it requires, such as hello-world). As of Docker 1.5.0 (specifically, docker/docker#8827),
hub.docker.com
2️⃣ 컨테이너 당 하나의 애플리케이션 패키징
3️⃣ PID 1, 신호처리, 좀비 프로세스 올바르게 처리
4️⃣ 공개 이미지 사용 여부를 신중하게 고려
- 도커 허브 (Docker hub)
Docker Hub Container Image Library | App Containerization
Deliver your business through Docker Hub Package and publish apps and plugins as containers in Docker Hub for easy download and deployment by millions of Docker users worldwide.
hub.docker.com
- AWS 갤러리 (AWS ECR Gallery)
ECR Public Gallery
Amazon ECR Public Gallery is a website that allows anyone to browse and search for public container images, view developer-provided details, and see pull commands
gallery.ecr.aws
'🐳 Container > Docker' 카테고리의 다른 글
[Docker] Docker 이미지 빌드를 위한 Makefile 활용 방법 (0) | 2024.02.14 |
---|---|
[Docker] ubuntu 22.04 docker 설치 (0) | 2023.10.06 |
[Docker] 도커 네트워크 (0) | 2023.08.17 |
[Docker] 컨테이너 리소스 (0) | 2023.08.09 |
[Docker] 도커 스토리지 볼륨 마운트 (0) | 2023.05.22 |