도커파일은 text-based 문서이며 컨테이너 이미지를 생성하는 데에 필요한 Instructions들 모음이다.
아래 예시를 보며 이해해보자.
FROM python:3.12
WORKDIR /usr/local/app
# Install the application dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy in the source code
COPY src ./src
EXPOSE 5000
# Setup an app user so the container doesn't run as the root user
RUN useradd app
USER app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
파이썬 기반 서버 예제
Dockerfile 에 자주 사용되는 instructions
FROM <image>- 어떤 Base Image를 사용할 지 정의한다.WORKDIR <path>- 어디로 파일들이 복사되고, command가 실행되어야 할 Working Directory를 명시하는 instructionCOPY <host-path> <image-path>빌딩 과정에서 복사할 파일들의 위치를 지정하고 이미지의 어느 위치에 복사할지 명시한다.RUN <command>빌딩 과정에서 실행해야할 커맨드를 명시함.- ex) pip install —no-cache-dir -r requirements.txt
ENV <name> <value>- 실행되는 컨테이너가 사용할 환경 변수를 설정하는 instructionEXPOSE <port-number>- this instruction sets configuration on the image that indicates a port the image would like to expose.USER <user-or-uid>- 기본 사용자 계정을 설정할 수 있음.CMD ["<command>", "<arg1>"]- 도커 컨테이너가 실행될 때 같이 실행되는 default command를 명시- ex) uvicorn app.main:app —host 0.0.0.0 —port 8080 →
- CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
Docker file에 있는 instruction을 실행할 때 docker engine은 image layer들을 재사용할 수 있는 지 확인하고 만약 기존에 비슷한 instruction을 실행했다면 캐싱된 결과를 가져온다.
[+] Building 1.0s (9/9) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 187B 0.0s
...
=> [internal] load build context 0.0s
=> => transferring context: 8.16kB 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> CACHED [3/4] COPY . . 0.0s
=> CACHED [4/4] RUN yarn install --production 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => exporting manifes'CICD > Docker' 카테고리의 다른 글
| Docker Image란 무엇일까? (6) | 2024.09.02 |
|---|---|
| Docker Container가 무엇일까? (1) | 2024.09.02 |