반응형

이미지를 만들었으니 이제 컨테이너로 실행시켜보자

docker run [OPTIONS] IMAGENAME [COMMAND] [ARG...]

docker run -d -p 0.0.0.0:8080:8080/tcp --name dockertest flasktest:latest
  • 옵션
    • -p : 포트(port) 설정
    • --name : 컨테이너 이름 설정(옵션 생략시 컨테이너명이 랜덤으로 설정된다.)
    • -d : run 동작을 백그라운드에서 진행하도록 설정
      • 백그라운드 모드로 실행시 컨테이너의 ID는 콘솔에 표시됩니다.
      • 백그라운드 프로세스로 실행되고 있는지 아래 명령어를 통해 확인할 수 있습니다.
docker logs -t [container name]

docker logs -t dockertest

 

반응형

'programming > docker' 카테고리의 다른 글

[Docker] Dockerfile 생성  (0) 2022.12.17
반응형

Dockerfile 작성

  • 파일명은 Dockerfile
  • Flask를 이미지로 만들기 위해 Dockerfile을 사용했습니다.
  • base image는 버전에 맞춰서 이용했습니다. (ex. 3.10.7-slim)
  • 아래 주소에서 이미지를 찾을 수 있습니다.
  • https://hub.docker.com/_/python
 

python - Official Image | Docker Hub

python •• Python is an interpreted, interactive, object-oriented, open-source programming language.

hub.docker.com

 

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.10.7-slim

# Allow statements and log messages to immediately appear in the Knative logs
# ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install production dependencies.
RUN pip install -r requirements.txt

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD python ./run.py

명령어 의미

  1. FROM → 사용할 base image를 적어준다.
  2. ENV → 환경설정 APP_HOME를 호출하면 /app
  3. WORKDIR → 작업 위치 위 파일은 환경설정에서 정해줌 (ex. /app)
  4. COPY → 현재 디렉토리(.)의 내용을 컨테이너 내의 /app 디텍토리에 복사
  5. RUN → 이미지에 프로젝트에 필요한 패키지 및 shell 명령어 실행
  6. CMD → run.py 실행

Build

Dockerfile을 작성했으면 이제 docker build를 통해 이미지화 시켜주자!!

  • Dockerfile이 작성되어 있는 곳에서 명령 프롬프트 실행 후 아래 명령어를 입력해준다.
docker build -t <repositoryname>:<version> .

명령어 의미

  • -t는 태그설정 옵션
  • version(tag)는 따로 적어주지 않으면 기본 latest버전으로 설정됨
  • 맨 뒤 .은 명령어 실행 위치에 있는 Dockerfile를 통해 이미지화 작업을 진행
  • Dockerfile 수정 후 재빌드 하면, 기존 latest버전은 none로 변경됨
반응형

'programming > docker' 카테고리의 다른 글

[Docker] 도커 실행  (0) 2022.12.26

+ Recent posts