Hangman 웹서비스 Docker로 구동해보기

 

hangman_web이라는 repo의 main에 코드가 머지될때마다 다음을 수행

1. 테스트 수행

2. Docker Image 빌드

- Dockerfile 만들기

3. Docker Image를 Docker Hub로 푸시

 

위 과정을 Github repo에 Github Actions으로 구현

 

hangman_web 프로그램 소개

flask 웹 프레임워크를 통해 hangman 프로그램 올라가 있는 형태

- 포트 번호는 어디든 바인딩 가능하며 실행할 때 지정

- flask 관련 모듈 설치 필요: requirements.txt

- pip3 freeze > requirements.txt

 

(base) % cat requirements.txt

Flask==2.3.2

Flask-HTTPAuth==4.5.0

Flask-Login==0.6.2

Flask-SQLAlchemy==3.0.3

 

실행방법

python3 -m flask run --host=0.0.0.0 --port=4000

- app.py를 기본으로 사용

(base) cookie@bagjueun-ui-MacBookAir hangman_web % cat app.py

 

실습은 labs.play-with-docker.com 에서 진행

$ git clone https://github.com/ourjune21/hangman_web.git

 

ourjune21/hangman_web

Contribute to ourjune21/hangman_web development by creating an account on GitHub.

github.com

 

$ cd hangman_web/

$ cat requirements.txt

$ pip3 install -r requirements.txt

가상환경 생성

[node2] (local) root@192.168.0.17 ~/hangman_web $ pip3 install -r requirements.txt error: externally-managed-environment

$ python -m flask run --host=0.0.0.0 --port=4000

open port 4000

새로운 탭으로 팝업해서 웹페이지 오픈

 

 

port mapping

Docker 컨테이너 내부 프로세스가 오픈한 포트는 호스트 프로세스 브라우저에서는 안 보임

-> 외부로 노출해주기 위해 포트 맵핑 (docker가 포트포워딩해줌) 

docker run 수행시 -p 옵션 사용

$ docker run -p 4000:4000 이미지이름

외부 4000번 포트를 통해서 내부 4000번으로 가도록 setup해줌

4. docker-day2-4

숙제1 Hangman 서비스를 Docker Image로 구성

1. Dockerfile 만들기 

FROM python:3.8-slim-buster

RUN pip3 install -r requirements.txt

% ls
Dockerfile		app.py			test.py
README.md		requirements.txt

% cat Dockerfile
FROM python:3.8-slim-buster 
LABEL Maintainer="ourjune"
WORKDIR /app
COPY app.py ./
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
EXPOSE 4000
CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0", "--port=4000"]
 

2. DockerHub어카운트ID/hangman으로 이미지 빌드하기 (dockerization)

--platform=linux/amd64 사용하기 (맥 M1이라면?)*

% docker build --platform=linux/amd64 -t ourjune/hangman .

% docker image ls
REPOSITORY                   TAG            IMAGE ID       CREATED          SIZE
ourjune/hangman              latest         5ea9e22680c1   50 seconds ago   61.2MB

3. docker run

이미지 가지고 컨테이너에서 실행하기

-> 포트포워딩(port mapping, port forwarding) 고려 필요

(base) cookie@bagjueun-ui-MacBookAir hangman_web % docker inspect ourjune/hangman 
[
    {
        "Id": "sha256:5ea9e22680c1296ac227c65243f1abbf817e29111e5baaf81f922055401ee476",
        "RepoTags": [
            "ourjune/hangman:latest"
        ],
        "RepoDigests": [
            "ourjune/hangman@sha256:5ea9e22680c1296ac227c65243f1abbf817e29111e5baaf81f922055401ee476"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2024-11-18T13:27:48.824040005Z",
        "DockerVersion": "27.2.0",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "4000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568",
                "PYTHON_VERSION=3.8.17",
                "PYTHON_PIP_VERSION=23.0.1",
                "PYTHON_SETUPTOOLS_VERSION=57.5.0",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/0d8570dc44796f4369b652222cf176b3db6ac70e/public/get-pip.py",
                "PYTHON_GET_PIP_SHA256=96461deced5c2a487ddc65207ec5a9cffeca0d34e7af7ea1afc470ff0d746207"
            ],
            "Cmd": [
                "python3",
                "-m",
                "flask",
                "run",
                "--host=0.0.0.0",
                "--port=4000"
            ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "Maintainer": "ourjune"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 61181735,
        "GraphDriver": {
            "Data": null,
            "Name": "overlayfs"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e2ef8a51359d088511d34c725305c220294a1fcd5fe5e5dbe4d698c7239ce2c9",
                "sha256:ae2d55769c5efcb6230d27c88eef033128fa1d238bdafe50812402f471152bb7",
                "sha256:3054512b6f71055cacea93ed12462e1ddc7f54988d9c7b51d10a5144d99ff501",
                "sha256:997b8e79e84fa9e7b9785408364770acca5261ff5cf450e3628b305e27a51a7e",
                "sha256:e6c5004ee77f450910ca26a9ef2e476ce766b3e4c83d034edfc28ff3736297a1",
                "sha256:a0f1ab685d8c39734970b2f46b4b692e0b9c99c4de81b40a23a6e1f6b272c0ef",
                "sha256:f14bb08b5cceb5af6774e911ca8dea233cea29d123537de4cf83b05ed3819381",
                "sha256:ce51fc551b1a69405e52c6978b6d86d51a39b598619a5c3d8aeb50f2d76ae139",
                "sha256:e2451913b2119659e4fb84256156c4e1e63ad37105e88376cc2a5c0df8bc5caf"
            ]
        },
        "Metadata": {
            "LastTagTime": "2024-11-18T13:27:50.571784881Z"
        }
    }
]
(base) cookie@bagjueun-ui-MacBookAir hangman_web %
% docker run -p 4000:4000 ourjune/hangman
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:4000
 * Running on http://172.17.0.3:4000
Press CTRL+C to quit

 

* docker run -p 4000:4000 -d ourjune/hangman -d옵션을 통해서 detach 백그라운드로 돌게 할 수 있음

 

4. 테스트 완료하면 이미지를 Docker Hub에 등록

- Docker Hub에 repo를 먼저 만들지 않고 바로 docker push 명령 해서 업로드 가능

% docker push ourjune/hangman
Using default tag: latest
The push refers to repository [docker.io/ourjune/hangman]
b649d2a36aae: Pushed 
e4a6604d0c9f: Pushed 
88b71b8ffa3e: Pushed 
cbfea27109a8: Pushed 
276dfcf5deff: Pushed 
5a2494c9bdb0: Pushed 
0e6e46ec7ac4: Pushed 
8b91b88d5577: Pushed 
824416e23423: Pushed 
8f777578c172: Pushed 
latest: digest: sha256:5ea9e22680c1296ac227c65243f1abbf817e29111e5baaf81f922055401ee476 size: 856

 

5. labs.play-with-docker.com에서 다운로드 받아 실행

- Flask app 오픈해볼 수 있음

- open port 4000

 

6. Dockerfile을 github repo에 커밋

docker stop 컨테이너ID

 

참고

https://danawalab.github.io/docker/2021/01/13/dockerfile-guideline.html