Docker:【チュートリアルをやってみた】イメージ編


はじめに

全体

  • ベースのイメージを入手して加工する
    • 変更をDockerファイルに記載
    • Dockerファイルを使ってイメージをビルド
  • コンテナ実行する
  • ブラウザで表示できることを確認
  • 補足
    • ビルドするファイルの指定方法は複数ある
      ①カレントディレクトリに「Dockerfile」を作成して実行
      ②Dockerファイル(ファイル名は任意)を「-f」でパス指定
      ③gitからダウンロード(tarボールになっていてもOK)
    • 今回は①で実施

前提

準備

ベースイメージのダウンロード

sudo docker pull alpine:3.5
sudo docker images|sort
$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB

$ sudo docker pull alpine:3.5
3.5: Pulling from library/alpine
8cae0e1ac61c: Pull complete
Digest: sha256:66952b313e51c3bd1987d7c4ddf5dba9bc0fb6e524eed2448fa660246b3e76ec
Status: Downloaded newer image for alpine:3.5
docker.io/library/alpine:3.5

$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
  • docker pull alpine:3.5
    • alpine3.5をダウンロードする
    • :3.5 …… バージョンを指定
  • docker images
    • ダウンロード前後でイメージを確認

Webサーバを構成するファイルの作成

  • PythonとFlaskで動的Webページを作る
  • Pythonのファイル …… app.py
  • Flaskのファイル …… index.html

app.py作成

(22行)

cat <<+ > app.py
from flask import Flask, render_template
import random

app = Flask(__name__)

# list of cat images
images = [
   "https://broaden-your-horizons.com/ai-ss/wp-content/uploads/2022/02/futa-myfirstapp1.gif",
   "https://broaden-your-horizons.com/ai-ss/wp-content/uploads/2022/02/futa-myfirstapp2.gif",
   "https://broaden-your-horizons.com/ai-ss/wp-content/uploads/2022/02/futa-myfirstapp3.gif"
    ]

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

if __name__ == "__main__":
    app.run(host="0.0.0.0")
+
ls -l app.py
-rw-rw-r-- 1 futa futa 1825 Feb 16 17:00 app.py
  • cat <<+ > app.py
    • app.pyを作成
    • Webアプリはflask(main処理でflaskをrun実行)
    • 3つの画像からランダムに表示する機能を追加
  • ls -l app.py
    • ファイルの確認
    • app.pyが作成されている

requirements.txtファイルの作成

cat <<+ > requirements.txt
Flask==0.10.1
+
ls -l requirements.txt
-rw-rw-r-- 1 futa futa 17 Feb 16 17:08 requirements.txt
  • cat <<+ > requirements.txt
    • requirements.txtの作成
    • インストールするのは「Flask」のバージョン「0.10.1」
  • ls -l requirements.txt
    • requirements.txtが作成されていることを確認
  • 補足
    • ベースにするAlpine Linuxにインストールする機能が書かれている
    • 今回はFlaskのみ
    • Flask:軽量なWebAPフレームワーク

index.htmlの作成

(32行)

mkdir templates
cat <<+ > ./templates/index.html
<html>
  <head>
    <style type="text/css">
      body {
        background: black;
        color: white;
      }
      div.container {
        max-width: 500px;
        margin: 100px auto;
        border: 20px solid white;
        padding: 10px;
        text-align: center;
      }
      h4 {
        text-transform: uppercase;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h4>Sample Docker of the day</h4>
      <h5 style="text-align:right;">produced by futa</h5>
      <img src="{{url}}" />
      <p><small>broaden-your-horizons: <a href="https://broaden-your-horizons.com/ai-ss/docker_summary/">Summury of Docker</a></small></p>
    </div>
  </body>
</html>
+
ls -l ./templates/index.html
-rw-rw-r-- 1 futa futa 17 Feb 16 17:11 ./templates/index.html
  • cat <<+ > ./templates/index.html
    • index.htmlの作成(内容説明は割愛)
  • ls -l ./templates/index.html
    • index.htmlが作成されていることを確認

Dockerイメージをビルド

Dockerファイル作成

(22行)

cat <<+ > Dockerfile
# our base image
FROM alpine:3.5

# Install python and pip
RUN apk add --update py2-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 ["python", "/usr/src/app/app.py"]
+
ls -l ./Dockerfile
-rw-rw-r-- 1 futa futa 482 Feb 16 18:10 ./Dockerfile
  • cat <<+ > Dockerfile
    • 「Dockerfile」と言う名前のDockerファイルを作成
  • ls -l ./Dockerfile
    • 「Dockerfile」が作成されたことを確認
  • Dockerファイル設定
    • FROM
      • ベースイメージ
      • alpine:3.5を指定
    • COPY
      • requirements.txtやindex.htmlを適切な位置に配置
    • RUN
      • pip installを実行
    • EXPOSE
      • リスンポートを5000に設定
    • CMD
      • Pythonを実行
      • Pythonの実行ファイルは/usr/src/app/に配置した「app.py」
  • 補足

イメージのビルド

buildコマンドを実行

sudo docker build -t futa/myfirstapp .
$ sudo docker build -t futa/myfirstapp .
Sending build context to Docker daemon  5.59MB
Step 1/8 : FROM alpine:3.5
 ---> f80194ae2e0c
Step 2/8 : RUN apk add --update py2-pip
 ---> Running in 9d52a876ae97
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
(1/12) Installing libbz2 (1.0.6-r5)
(2/12) Installing expat (2.2.0-r1)
(3/12) Installing libffi (3.2.1-r2)
(4/12) Installing gdbm (1.12-r0)
(5/12) Installing ncurses-terminfo-base (6.0_p20171125-r1)
(6/12) Installing ncurses-terminfo (6.0_p20171125-r1)
(7/12) Installing ncurses-libs (6.0_p20171125-r1)
(8/12) Installing readline (6.3.008-r4)
(9/12) Installing sqlite-libs (3.15.2-r2)
(10/12) Installing python2 (2.7.15-r0)
(11/12) Installing py-setuptools (29.0.1-r0)
(12/12) Installing py2-pip (9.0.0-r1)
Executing busybox-1.25.1-r2.trigger
OK: 62 MiB in 23 packages
Removing intermediate container 9d52a876ae97
 ---> 7d0c87a40274
Step 3/8 : COPY requirements.txt /usr/src/app/
 ---> f1a92dc9dbda
Step 4/8 : RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
 ---> Running in 9dacd6b6b54d
Collecting Flask==0.10.1 (from -r /usr/src/app/requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/db/9c/149ba60c47d107f85fe52564133348458f093dd5e6b57a5b60ab9ac517bb/Flask-0.10.1.tar.gz (544kB)
Collecting Werkzeug>=0.7 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
Collecting Jinja2>=2.4 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/7e/c2/1eece8c95ddbc9b1aeb64f5783a9e07a286de42191b7204d67b7496ddf35/Jinja2-2.11.3-py2.py3-none-any.whl (125kB)
Collecting itsdangerous>=0.21 (from Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.10.1->-r /usr/src/app/requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz
Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask
  Running setup.py install for MarkupSafe: started
    Running setup.py install for MarkupSafe: finished with status 'done'
  Running setup.py install for Flask: started
    Running setup.py install for Flask: finished with status 'done'
Successfully installed Flask-0.10.1 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 itsdangerous-1.1.0
You are using pip version 9.0.0, however version 22.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 9dacd6b6b54d
 ---> dd6cde260bd1
Step 5/8 : COPY app.py /usr/src/app/
 ---> 51d068a27687
Step 6/8 : COPY templates/index.html /usr/src/app/templates/
 ---> e7133bffc62c
Step 7/8 : EXPOSE 5000
 ---> Running in 5fe33d71d4bc
Removing intermediate container 5fe33d71d4bc
 ---> 8597e1dba290
Step 8/8 : CMD ["python", "/usr/src/app/app.py"]
 ---> Running in e72119586b1b
Removing intermediate container e72119586b1b
 ---> 8d54f7f7249b
Successfully built 8d54f7f7249b
Successfully tagged futa/myfirstapp:latest
  • docker build
    • Dockerファイルを使って新しいイメージをビルド
    • カレントディレクトリにあるDockerfileを読み込む
  • -t
    • 「futa/myfirstapp」の名前でタグ付け

作成されたイメージを確認

sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
alpine                      3.5       f80194ae2e0c   3 years ago     4MB
alpine                      latest    c059bfaa849c   2 months ago    5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago     191MB
futa/myfirstapp             latest    8d54f7f7249b   6 minutes ago   56.8MB
hello-world                 latest    feb5d9fea6a5   4 months ago    13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago     104MB
  • REPOSITORY列で作成した「futa/myfirstapp」が確認できる
  • 補足
    • ベースイメージを利用して作りたい環境のイメージを作成
    • 作成したイメージを配布すればDockerがある環境であればすぐに環境構築できる


Posted by futa