docker build image_ flask_hello_world

Ja L·2023년 3월 16일
0

docker

목록 보기
2/4

[출처] https://psychoria.tistory.com/613

$ pwd
/home/agens

$ mkdir pr_docker_build

$ cd pr_docker_build

$ vi Dockerfile

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

# Set the working directory to /app
WORKDIR /app

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

# Install any needed packages specified in requirements.txt
RUN pip install -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"]

$ vi requirements.txt

Flask
Redis

$ vi 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)

$ docker build -t friendlyhello .

$ docker images

$ docker run -p 4000:80 friendlyhello
호스트의 4000번 포트에 컨테이너 내부의 80번 포트를 매핑

192.168.xx.xxx:4000 접속하면 Hello World! 페이지 등장

profile
DB Engineer

0개의 댓글