2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker image の作り方 (Flask)

Last updated at Posted at 2021-07-01

こちらのページにあるのと同様のことを行いました。
Flask を Docker で動かします。
labs/beginner/chapters/webapps.md
2.2 Docker Images

次のファイルを作成

$ tree
.
├── app.py
├── Dockerfile
├── requirements.txt
└── templates
    └── index.html
app.py
from flask import Flask, render_template
import random

app = Flask(__name__)

# list of cat images
images = [
	"fmervo000001gsle.jpg",
	"rn2ola000000lk6e.jpg",
	"rn2ola000001gogf.jpg",
	"6fujishigai_s_s.jpg",
	"rn2ola000000lk6r.jpg",
	"5fujikawarakuza_s_s.jpg"
	]

url_base = "https://www.city.fuji.shizuoka.jp/page/gazou/fmervo000001dsro-img/"



@app.route('/')
def index():
	url = url_base + random.choice(images) 
	return render_template('index.html', url=url)

if __name__ == "__main__":
	app.run(host="0.0.0.0")
Dockerfile
# our base image
FROM alpine:3.13

# Install python and pip
RUN apk add --update py3-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"]
requirements.txt
Flask==2.2.2
templates/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<title>富士山</title>
</head>
<body>
<div class="container">
<p>富士山 航空写真</p>
<blockquote>
<img src="{{url}}" />
</blockquote>
<p>Courtesy: <a href="https://www.city.fuji.shizuoka.jp/page/gazou/fmervo000001dsro.html">富士市</a></p>
</div>
Jul/2/2021 AM 11:29<br />
</body>
</html>

Flask として問題のないことを確認

$ python app.py 
 * Serving Flask app '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: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.1.5:5000/ (Press CTRL+C to quit)

ブラウザーで
http://localhost:5000/
にアクセス

flask_aa.png

Docker image の作成

docker build -t uchida/myfirstapp .
$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
uchida/myfirstapp   latest    71f862cd6bd4   34 seconds ago   75MB
alpine              3.13      6dbb9cc54074   2 months ago     5.61MB

起動

docker run -p 8888:5000 --name myfirstapp uchida/myfirstapp

再起動

docker run -p 8888:5000 uchida/myfirstapp

ブラウザーで
http://localhost:8888/
にアクセス
docker_aa.png

確認したバージョン

$ docker --version
Docker version 20.10.21, build 20.10.21-0ubuntu3
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?