フラスコで構築したWebをコンテナに配置


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 -
  • アラームトラブルシューティング
  • ENV FLASK_APP=py_app
    ENV FLASK_APP=development
    開発環境を開発と明確にしてこそ、警告は出ない.
    実はこれも適当な方法だ.
    スムーズに開発するには、Gunicornと同じWebソケットを使用する必要があります!
    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.削除警告の確認
    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
    警告が正常にクリアされました.
    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.ドックハブの推進
    root@hostos1:~/DockerLabs/flask# docker push yk1700/py_flask:1.0

    アップロードに成功しました!
    12.hostos 2でテスト
    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
    Dockerファイルが構築されていないことを確認できます!
    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

    フラスコ容器は正常に動作しています!