Building Container Images

이정훈·2024년 8월 7일

Docker

목록 보기
12/34

Dockerfile

컨테이너 이미지를 빌드하기위해서는 Dockerfile이 필수적입니다.
Dockerfile은 어떻게 Docker 이미지를 모을 것인지에 대한 명령어들이 작성되어있습니다.
Dockerfile의 각 명령어는 이미지에 새로운 레이러를 만듭니다.

예를 들어 아래와 같은 현재 디렉토리에 있는 파이썬 코드를 파이썬 컨테이너에서 실행하는 Dockerfile을 만들 수 있습니다.

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Building an Image

앞서 만든 Dockerfile을 통해 이미지를 만들 수 있습니다.

docker build -t <image name> .

image name에는 여러분이 해당 dockerfile로 만든 image에 붙일 이름입니다.

Inspecting Images and Layers

이미지를 성공적으로 만들었다면 이미지를 검사해볼 차례입니다.
먼저 이미지가 만들어졌는지 확인하기 위해 이미지들의 목록을 가져옵니다.

docker image ls

이미지의 빌드 히스토리를 확인해 봅시다.

docker history <image_name>

이미지의 레이어를 확인하기 위해서는 아래와 같이 할 수 있습니다.

docker inspect <image_name>

이제 이미지를 제거해 봅시다.

docker image rm <image_name>

Pushing Image to a Registry

이번에는 만든 이미지를 Docker Hub와 같은 레지스트리에 저장하는 방법을 알아 봅시다.
먼저 docker에 로그인합니다.

docker login

이후 Docker Hub에 등록하려는 이미지에 태그를 달아 줍시다.

docker tag <image_name> <user_name>/<repository_name>:<tag>

이제 Docker Hub에 등록해 봅시다.

docker push username/repository:tag
profile
기록으로 흔적을 남깁니다.

0개의 댓글