Python을 이용한 도커라이징 - TheOpenCloudEngine/uEngine-cloud GitHub Wiki

Python을 이용한 도커라이징

1. 폴더 생성

> cd ~
> mkdir docker-test
> cd docker-test

2. 파일 생성

  1. Dockerfile
# Dockerfile

FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
ENV NAME World
CMD ["python", "app.py"]
  1. app.py
# app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
  1. requirements.txt
# requirements.txt

Flask
Redis

3. 도커 빌드 및 실행

> docker build -t my-python .

> docker images

# REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
# my-python           latest              <Image ID>          14 minutes ago      131MB
 
> docker run -p 8001:80 my-python
⚠️ **GitHub.com Fallback** ⚠️