flask를 통해 구축한 웹을 컨테이너로 배포해보자
1.디렉터리 생성
root@hostos1:~/DockerLabs# mkdir flask
root@hostos1:~/DockerLabs# cd flask
2.도커파일 작성
root@hostos1:~/DockerLabs/flask# vi Dockerfile
FROM python:3.8-alpine
RUN apk update && \
apk add --no-cache \
bash
RUN apk add --update build-base python3-dev py-pip
ENV LIBRARY_PATH=/lib:/usr/lib
EXPOSE 9000
WORKDIR /py_app
COPY . /py_app
RUN pip install -r ./app/requirements.txt
ENTRYPOINT ["python"]
CMD ["./add/py_app.py"]
- py_app.py 작성
from flask import Flask
py_app =Flask(__name__)
@py_app.route('/')
def python_flask():
return """
<h1 style="text-algin:center; color:deepskyblue;">
Docker container application: Python & Flask!</h1>
<p style="text-algin:center; color:deepskyblue;">
This is micro web framework for running Flask
inside Docker.</p>
"""
if __name__ == '__main__':
py_app.run(host='0.0.0.0', port=9000, debug=True)
4.파일 정리
root@hostos1:~/DockerLabs/flask# mkdir app
root@hostos1:~/DockerLabs/flask# mv py_app.py app/
root@hostos1:~/DockerLabs/flask# mv requirements.txt app/
.dockerignore
빌드시 여기에 적혀있는 파일들은 컨테이너에 포함되지 않는다
root@hostos1:~/DockerLabs/flask# vi .dockerignore
Dockerfile
5.빌드
root@hostos1:~/DockerLabs/flask# docker build -t py_flask:1.0 .
6.빌드된 이미지 확인
root@hostos1:~/DockerLabs/flask# docker images | grep py_flask
py_flask 1.0 9421d7a3995b 2 minutes ago 358MB
root@hostos1:~/DockerLabs/flask# docker image history py_flask:1.0
IMAGE CREATED CREATED BY SIZE COMMENT
9421d7a3995b 2 minutes ago /bin/sh -c #(nop) CMD ["./app/py_app.py"] 0B
69e969404687 2 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["python"] 0B
a8f83f64b2a9 2 minutes ago /bin/sh -c pip install -r ./app/requirements… 11.7MB
483c930f5110 2 minutes ago /bin/sh -c #(nop) COPY dir:7e4164a91bf7cf098… 466B
4f0280fb6cd4 2 minutes ago /bin/sh -c #(nop) WORKDIR /py_app 0B
b97629209e51 2 minutes ago /bin/sh -c #(nop) EXPOSE 9000 0B
89e317819c2a 2 minutes ago /bin/sh -c #(nop) ENV LIBRARY_PATH=/lib:/us… 0B
0eea2d150d39 2 minutes ago /bin/sh -c apk add --update build-base pytho… 300MB
1a3d68bb49b4 3 minutes ago /bin/sh -c apk update && apk add --n… 3.44MB
7.컨테이너 실행
root@hostos1:~/DockerLabs/flask# docker run -it -p 9000:9000 py_flask:1.0
* Serving Flask app "py_app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.8:9000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 550-471-881
192.168.56.104 - - [13/Aug/2021 05:23:59] "GET / HTTP/1.1" 200 -
- warning 문제 해결
ENV FLASK_APP=py_app
ENV FLASK_APP=development
개발 환경을 development로 명시해줘야 warning이 뜨지 않는다
사실 이것도 임시방편
제대로 개발을 하려면 gunicorn과 같은 웹소켓을 사용해야한다!
FROM python:3.8-alpine
RUN apk update && \
apk add --no-cache \
bash
RUN apk add --update build-base python3-dev py-pip
ENV LIBRARY_PATH=/lib:/usr/lib
EXPOSE 9000
WORKDIR /py_app
COPY . /py_app
RUN pip install -r ./app/requirements.txt
ENV FLASK_APP=py_app
ENV FLASK_APP=development
ENTRYPOINT ["python"]
CMD ["./app/py_app.py"]
10.warning 제거 확인
root@hostos1:~/DockerLabs/flask# docker run -it -p 9004:9000 yk1700/py_flask:1.0
* Serving Flask app "py_app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.8:9000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 908-847-921
warning이 성공적으로 제거되었다!
9.재빌드
root@hostos1:~/DockerLabs/flask# docker build -t py_flask:2.0 .
10.태그 변경
root@hostos1:~/DockerLabs/flask# docker image tag py_flask:2.0 yk1700/py_flask:1.0
11.도커허브 push
root@hostos1:~/DockerLabs/flask# docker push yk1700/py_flask:1.0
성공적으로 업로드되었다!
12.hostos2에서 테스트
root@hostos2:~# docker pull yk1700/py_flask:1.0
13.실행
root@hostos2:~# docker run -it -p 10000:100001 yk1700/py_flask:1.0
* Serving Flask app "py_app" (lazy loading)
* Environment: production
* Debug mode: on
* Running on all addresses.
* Running on http://172.17.0.3:9000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 129-806-730
도커파일 없이 빌드된 것을 확인할 수 있다!!
root@hostos2:~# docker exec -it 83887ba48dd5 bash
bash-5.1# ls
app
bash-5.1# cd app/
bash-5.1# ls
py_app.py requirements.txt
flask 컨테이너가 정상적으로 작동하고 있다!
Author And Source
이 문제에 관하여(flask를 통해 구축한 웹을 컨테이너로 배포해보자), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yk1700/flask를-통해-구축한-웹을-컨테이너로-배포해보자저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)