본문 바로가기
Development/Docker

[Docker] flask API Container 구현

by 성딱이 2022. 1. 6.
반응형
1. Dockerfile 준비
# Base Image는 miniconda3
FROM continuumio/miniconda3

# 필수 설치요소들 설치
RUN apt-get update && apt-get install -y build-essential cmake gcc vim wget \
    && rm -rf /var/lib/apt/lists/*
RUN pip install --upgrade pip setuptools wheel
RUN pip install pandas numpy cvxpy cvxopt matplotlib pymysql sqlalchemy \
    flask flask-restful flask_cors Flask-WTF jupyter gevent gunicorn

# Jupyter Notebook의 Terminal 기본 Setting을 bash shell로 지정. (아니라면 일반 쉘 terminal로 지정 됨)
c.NotebookApp.terminado_settings = {'shell_command': ['/bin/bash']}" >> /root/.jupyter/jupyter_notebook_config.py

# RUN jupyter notebook --generate-config
# RUN echo "c.NotebookApp.password_required=True" >> /root/.jupyter/jupyter_notebook_config.py
# RUN echo "c.NotebookApp.password=themostjh" >> /root/.jupyter/jupyter_notebook_config.py

# password 암호화해서 Setting 
# RUN echo "\
# from IPython.lib import passwd \n\
# password = passwd('Juhyeon') \n\
# c.NotebookApp.password = password \n\
# c.NotebookApp.terminado_settings = {'shell_command': ['/bin/bash']}" >> /root/.jupyter/jupyter_notebook_config.py

WORKDIR /usr/local/app
COPY ./ /usr/local/app/

ENTRYPOINT ["sh", "entrypoint.sh"]

 

 

2. Dockerfile Build
$ docker build -t juhyeon/flaskapi:v0.1 .

출처 : https://sseongju1.tistory.com/19

 

[Docker] Image Build / Push / Pull (도커이미지 빌드 / 푸쉬 / 풀)

0. Docker Login $ docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username (jmnote):..

sseongju1.tistory.com

 

3. Docker Run
$ docker run -it --rm -p 8889:8889 -p 8890:8890 -v "/home/jh:/home/jh" jh/flaskapi:v0.1

 

 

4. Jupyter Run
# jupyter notebook --allow-root(루트권한허용) --no-browser(브라우저창 안띄움) --ip=*(모든대역ip허용) --port=8989 (port할당)
root@8542f86e82be:/# jupyter notebook --allow-root --no-browser --ip=* --port=8989

앞서 Build시 비밀번호를 설정했다면 비밀번호를 입력 후 접속. 그렇지 않았다면 Jupyter Notebook 띄울 시 생성된 token을 입력하여 접속 가능하다.

 

5. flask API 테스트
# test.py

from flask import Flask
from flask import request
from flask import render_template
 
web_gui = Flask(__name__)
 
@web_gui.route('/')
def hello_fnc():
    return 'Hello'
 
@web_gui.route("/page", methods = ["GET", "POST"])
def page_fnc():
    if request.method == "POST":
        return 'POST received'
    else:
        return render_template("page.html")    
 
if __name__ == "__main__":
    web_gui.run(debug=True, host='0.0.0.0', port=8890)

 

root@8542f86e82be:/jh/flaskapi_test$ python test.py

 * Serving Flask app 'test' (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://x.x.x.x:8890/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: xxx-xxx-xxx

 

 

 

테스트 성공

끄읏 (Request 및 Response로 내용 채워나갈 예정)

 

 

 

반응형

댓글