Docker

Python-flask 도커 이미지 만들기

채윤아빠 2021. 5. 4. 08:29
728x90
반응형

환경 구성

OS : Ubuntu-20.04.2 x64

Docker 환경설치

sudo apt  install docker.io

설치된 Docker 버전 확인

$ sudo docker version
Client:
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.13.8
 Git commit:        20.10.2-0ubuntu1~20.04.2
 Built:             Tue Mar 30 21:24:57 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.8
  Git commit:       20.10.2-0ubuntu1~20.04.2
  Built:            Mon Mar 29 19:10:09 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.3-0ubuntu2.3
  GitCommit:
 runc:
  Version:          spec: 1.0.2-dev
  GitCommit:
 docker-init:
  Version:          0.19.0
  GitCommit:

Dockerfile 작성

기본 파이썬 도커 이미지를 "python - Docker Official Images":https://hub.docker.com/_/python/ 에서 가장 최신인 "python:alpine" 이미지를 기초로 다음과 같이 작성하였습니다.

FROM python:alpine


LABEL org.label-schema.build-date="$COMMIT_TIME" \
    org.label-schema.name="python-flask" \
    org.label-schema.description="Python for Flask service : Docker image running on Alpine Linux" \
    org.label-schema.vcs-ref="$CI_APPLICATION_TAG_SHORT" \
    org.label-schema.vcs-url="https://github.com/hanwhhanwh/docker" \
    org.label-schema.architecture="AMD64/x86_64" \
    org.label-schema.vendor="hanwhhanwh" \
    org.label-schema.maintainer="Wonhee Han <hbesthee@naver.com>" \
    org.label-schema.version="1.0.0" \
    org.label-schema.schema-version="1.0"


ENV FLASK_HOST="0.0.0.0" \
    FLASK_PORT=38080 \
    FLASK_DEBUG="--no-debugger" \
    FLASK_ENV=production \
    FLASK_APP=server.py \
    FLASK_ROOT="/app/public"


COPY start-flask.sh /start-flask.sh
COPY requirements.txt /tmp/


WORKDIR "${FLASK_ROOT}"


RUN apk add --no-cache --virtual .build-deps gcc build-base musl-dev alpine-sdk ; \
    pip install -r /tmp/requirements.txt ; \
    apk del .build-deps gcc build-base musl-dev alpine-sdk ; \
    rm -f /var/cache/apk/* ; \
    mkdir -p "${FLASK_ROOT}"


EXPOSE 38080


CMD ["start-flask.sh"]

"start-flask.sh"을 일단 다음과 같이 간단하게 작성하였습니다.

#!/bin/sh
set -eo pipefail

cd /app/public

# pip install -r requirements.txt

echo "Flask ENV = flask run ${FLASK_DEBUG} --host=${FLASK_HOST} --port=${FLASK_PORT}"
python -m flask run ${FLASK_DEBUG} --host=${FLASK_HOST} --port=${FLASK_PORT}

"python-flask" 도커 이미지 만들기

다음과 같이 도커 이미지를 만듭니다.

$ docker build . -t python-flask


sha256=47bd186805c5a529b335b45f0988b01bbb43029a537a9089814f836123f99377
  Stored in directory: /root/.cache/pip/wheels/0f/2b/b7/53b19a989d756529b072fbb17db8e6dead9bb630e8a170142f
  Building wheel for greenlet (setup.py): started
  Building wheel for greenlet (setup.py): finished with status 'done'
  Created wheel for greenlet: filename=greenlet-1.0.0-cp39-cp39-linux_x86_64.whl size=89595 sha256=9758bf710fe214954a67241ee75d2d0e7f9b94270c7eb79cba114fcd256a55e7
  Stored in directory: /root/.cache/pip/wheels/1a/11/93/3f8bdb4e78c076da4b3cea0a621ce94a15544792b093b2d0d6
Successfully built MarkupSafe sqlalchemy greenlet
Installing collected packages: MarkupSafe, Werkzeug, six, Jinja2, itsdangerous, click, pytz, protobuf, greenlet, Flask, aniso8601, sqlalchemy, qrcode, mysql-connector-python, flask-restful
Successfully installed Flask-1.1.2 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 aniso8601-9.0.1 click-7.1.2 flask-restful-0.3.8 greenlet-1.0.0 itsdangerous-1.1.0 mysql-connector-python-8.0.24 protobuf-3.15.8 pytz-2021.1 qrcode-6.1 six-1.15.0 sqlalchemy-1.4.13
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.13/main: No such file or directory
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.13/community: No such file or directory
(1/37) Purging .build-deps (20210504.084002)
(2/37) Purging alpine-sdk (1.0-r0)
(3/37) Purging abuild (3.7.0-r0)
(4/37) Purging fakeroot (1.25.3-r3)
(5/37) Purging openssl (1.1.1k-r0)


Successfully built 833ef799dc88
Successfully tagged python-flask:latest

중간에 WARNING 이 나오는데, 이용하는데 큰 문제는 없었습니다.

python flask 웹 애플리케이션 실행

다음과 유사하게 python flask 웹 애플리케이션을 실행할 수 있습니다.

docker run -d --name user --network=host --rm \
    -v /home/hunature/work/python-flask:/app/public \
    python-flask

소스 다운로드 : https://github.com:hanwhhanwh/docker.git

참고자료