5
2

More than 3 years have passed since last update.

Docker で作ったWebサーバ(app.py by Flask)を動かすテスト

Last updated at Posted at 2020-01-04

Docker を使って、GCPを使って、いろいろやりたいなー、と思っていましたが、冬休みももう終わりそうです。

Docker の使い方がメインで、できたのは以下です。

  1. Dockerfile を書く
  2. Docker image を作る (docker build)
  3. ローカルで動かす (docker run)
  4. ローカルで確認 (localhost をWEBブラウザで見る)

サーバとして動いているのは、下記のContainer で動いているものです。

mydockerimage20190104.png

1. Dockerfile とsource file の用意

こんなDockerfileを作りました。

# our base image
FROM ubuntu

# Install python and pip
RUN apt update -y
RUN apt install python3  -y -qq --no-install-recommends 
RUN apt install python3-pip -y -qq  --no-install-recommends
RUN pip3 install --upgrade pip

# install Python modules needed by the Python app
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# copy files required for the app to run
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/

# tell the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python3", "/usr/src/app/app.py"]

Dockerfile では、

  • ubuntu をbase image にして作る
  • python3 と pipをインストールする
  • app.py をコピーする
  • 最後にCMD で実行する

と読めます。Dockerfile が置いてあるディレクトリをもとにimage を構成しています。実際のファイルはここからダウンロードできます。

2. Docker Image を作る

> docker build --no-cache -t myapp .

最後にこんなメッセージが。Windows での権限の設定の仕方はいまだに分かりませんが、今のところ問題にはなっていません。

Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

3. ローカルで動かす

> docker run -it --rm -p 8001:8000 myapp
* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 335-299-345
172.17.0.1 - - [03/Jan/2020 13:14:11] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [03/Jan/2020 13:14:11] "GET /favicon.ico HTTP/1.1" 404 -

仮想化したこのマシンが動いているかはdocker コマンドで確認できます。

> docker ps

4. Webブラウザで確認

普通にlocalhost:8001 を開くと見えると思います。あるいは以下でも。

$curl http://localhost:8001

StatusCode        : 200
StatusDescription : OK
Content           : <!-- index.html -->
                    <!DOCTYPE html>
                    <html lang="ja">
                      <head>
                      </head>
...

備忘録

この後、デプロイして?ほかのクラウドサーバで動かしたいのですが、そこまでできていないので、続きをこれから頑張ります。また、Flaskも情報が思いのほか多いのでもう少し何かできるかも。

テストコード

できた部分は、ここだけ切り取ってGithubのsamplesのここに置きました。samples/docker-python-flask-20200104 というディレクトリにあります。下記の参考サイトをもとに編集して辿り着いたものです。

参考

できればこのイメージを内部、あるいは外部のサーバで動かしたい。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2