カスタムなイメージを作る方法は2通りある。
ubuntu@ip-172-30-2-68:~$ docker run -dit --name webcontent -p 8080:80 httpd:2.4 02528579de2626e893d32a4f8b8d030454cef9accef2bb5c51d90cb5bc894557
ubuntu@ip-172-30-2-68:~$ docker cp index.html webcontent:/usr/local/apache2/htdocs/
[Docker ホストのIPアドレスが 3.113.249.222 の場合] http://3.113.249.222:8080/
ubuntu@ip-172-30-2-68:~$ docker commit webcontent mycustomed_httpd sha256:ca4e4c000933d7f7f03116fc29e0cd0903474fbc7c1e261bc3d446554977f410
ubuntu@ip-172-30-2-68:~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mycustomed_httpd latest ca4e4c000933 47 seconds ago 145MB httpd 2.4 f2789344c573 3 weeks ago 145MB
ubuntu@ip-172-30-2-68:~$ docker image history mycustomed_httpd IMAGE CREATED CREATED BY SIZE COMMENT ca4e4c000933 About a minute ago httpd-foreground 67B f2789344c573 3 weeks ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B <missing> 3 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 3 weeks ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B <missing> 3 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B <missing> 3 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 59.9MB <missing> 3 weeks ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B <missing> 3 weeks ago /bin/sh -c #(nop) ENV HTTPD_SHA256=eb397fee… 0B <missing> 3 weeks ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.54 0B <missing> 3 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 4.76MB <missing> 3 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B <missing> 3 weeks ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B <missing> 3 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B <missing> 3 weeks ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B <missing> 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:5bd53bff884e470b3… 80.5MB
「イメージに含めたいファイル」と「Dockerfile」を一つのディレクトリに置き、それを docker build してイメージを作成する。
$ mkdir ~/customed_httpd
$ cd ~/customed_httpd
Dockerfile |
|
$ docker build -t myimage01 . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM httpd latest: Pulling from library/httpd bd159e379b3b: Pull complete 36d838c2f6d6: Pull complete b55eda22bb18: Pull complete f6e6bfa28393: Pull complete a1b49b7ecb8a: Pull complete Digest: sha256:4400fb49c9d7d218d3c8109ef721e0ec1f3897028a3004b098af587d565f4ae5 Status: Downloaded newer image for httpd:latest ---> d16a51d08814 Step 2/2 : COPY index.html /usr/local/apache2/htdocs/ ---> 8847e6dad501 Successfully built 8847e6dad501 Successfully tagged myimage01:latest
ubuntu@ip-172-30-2-68:~/customed_httpd$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myimage01 latest 8847e6dad501 2 minutes ago 145MB mycustomed_httpd latest ca4e4c000933 3 hours ago 145MB httpd latest d16a51d08814 26 hours ago 145MB httpd 2.4 f2789344c573 3 weeks ago 145MB
ubuntu@ip-172-30-2-68:~/customed_httpd$ docker history myimage01 IMAGE CREATED CREATED BY SIZE COMMENT 8847e6dad501 2 minutes ago /bin/sh -c #(nop) COPY file:d86ee149e968cf6c… 65B d16a51d08814 26 hours ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B26 hours ago /bin/sh -c #(nop) EXPOSE 80 0B 26 hours ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B 26 hours ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B 26 hours ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 59.9MB 26 hours ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B 26 hours ago /bin/sh -c #(nop) ENV HTTPD_SHA256=eb397fee… 0B 26 hours ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.54 0B 26 hours ago /bin/sh -c set -eux; apt-get update; apt-g… 4.76MB 26 hours ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B 26 hours ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B 26 hours ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B 26 hours ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B 31 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B 31 hours ago /bin/sh -c #(nop) ADD file:b78b777208be08edd… 80.5MB
履歴を表示する |
|
ubuntu@ip-172-30-2-68:~/customed_httpd$ docker run -dit --name webcontent_docker -p 8080:80 myimage01 1656e839aa25f4aa36f1c735a9f3be41f2b33a1ec331f1d3c6203bf663a17396 ubuntu@ip-172-30-2-68:~/customed_httpd$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1656e839aa25 myimage01 "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp webcontent_docker
ubuntu@ip-172-30-2-68:~/customed_httpd$ docker stop webcontent_docker webcontent_docker ubuntu@ip-172-30-2-68:~/customed_httpd$ docker rm webcontent_docker webcontent_docker ubuntu@ip-172-30-2-68:~/customed_httpd$ docker image rm myimage01 Untagged: myimage01:latest Deleted: sha256:8847e6dad5013239f092f6e27c94c4eedc62f17dfadbce92d8c47104c5c1249a Deleted: sha256:71d87bfc454f09f33daf2bd7500b83951bc930399a45bd062a97d0021118cefa
コマンドの実行や、パッケージインストールを伴う例を考える。 debian イメージをベースに、php 入りの apache イメージを作成する。
ubuntu@ip-172-30-2-68:~$ mkdir ~/phpimage ubuntu@ip-172-30-2-68:~$ cd ~/phpimage
~/phpimage/index.php |
|
~/phpimage/Dockerfile |
|
ubuntu@ip-172-30-2-68:~/phpimage$ docker build . -t myphpimage
ubuntu@ip-172-30-2-68:~/phpimage$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myphpimage latest 4bd32a600e5e 15 seconds ago 254MB mycustomed_httpd latest ca4e4c000933 4 hours ago 145MB httpd latest d16a51d08814 27 hours ago 145MB debian latest d91720f514f7 31 hours ago 124MB httpd 2.4 f2789344c573 3 weeks ago 145MB
$ docker build . -t myphpimage --no-cache
イメージをファイル化すると、他のコンピュータに持っていくことができる。
ubuntu@ip-172-30-2-68:~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myphpimage latest 18e37a88b169 20 minutes ago 254MB mycustomed_httpd latest ca4e4c000933 4 hours ago 145MB httpd latest d16a51d08814 27 hours ago 145MB debian latest d91720f514f7 32 hours ago 124MB httpd 2.4 f2789344c573 3 weeks ago 145MB
ubuntu@ip-172-30-2-68:~$ docker save -o saved.tar myphpimage
tar ファイルの中のたくさんのディレクトリは、それぞれのレイヤにぞくするファイルである。
ubuntu@ip-172-30-2-68:~$ ls -l saved.tar -rw------- 1 ubuntu ubuntu 262885888 Oct 6 07:20 saved.tar ubuntu@ip-172-30-2-68:~$ tar tvf saved.tar -rw-r--r-- 0/0 2769 2022-10-06 06:59 18e37a88b169c57a16f89c0fec9b0ffd85385288a2dff5937fbc6b32fd8d4f8c.json drwxr-xr-x 0/0 0 2022-10-06 06:59 1a763ed6c13f7b94594ef2d62431eff6d78b6e882195642a3874f00e305dd858/ -rw-r--r-- 0/0 3 2022-10-06 06:59 1a763ed6c13f7b94594ef2d62431eff6d78b6e882195642a3874f00e305dd858/VERSION -rw-r--r-- 0/0 477 2022-10-06 06:59 1a763ed6c13f7b94594ef2d62431eff6d78b6e882195642a3874f00e305dd858/json -rw-r--r-- 0/0 133528064 2022-10-06 06:59 1a763ed6c13f7b94594ef2d62431eff6d78b6e882195642a3874f00e305dd858/layer.tar drwxr-xr-x 0/0 0 2022-10-06 06:59 d87ec64cf70a4ef44d4b3f0f299053681f419d8173c01645ad903bac39c9d342/ -rw-r--r-- 0/0 3 2022-10-06 06:59 d87ec64cf70a4ef44d4b3f0f299053681f419d8173c01645ad903bac39c9d342/VERSION -rw-r--r-- 0/0 401 2022-10-06 06:59 d87ec64cf70a4ef44d4b3f0f299053681f419d8173c01645ad903bac39c9d342/json -rw-r--r-- 0/0 129337344 2022-10-06 06:59 d87ec64cf70a4ef44d4b3f0f299053681f419d8173c01645ad903bac39c9d342/layer.tar drwxr-xr-x 0/0 0 2022-10-06 06:59 e38f7c2170a00472997f79223475f487ff46c2d0fa28a11c5dc44069d1c5f55a/ -rw-r--r-- 0/0 3 2022-10-06 06:59 e38f7c2170a00472997f79223475f487ff46c2d0fa28a11c5dc44069d1c5f55a/VERSION -rw-r--r-- 0/0 1434 2022-10-06 06:59 e38f7c2170a00472997f79223475f487ff46c2d0fa28a11c5dc44069d1c5f55a/json -rw-r--r-- 0/0 3584 2022-10-06 06:59 e38f7c2170a00472997f79223475f487ff46c2d0fa28a11c5dc44069d1c5f55a/layer.tar -rw-r--r-- 0/0 360 1970-01-01 00:00 manifest.json -rw-r--r-- 0/0 93 1970-01-01 00:00 repositories
ubuntu@ip-172-30-2-68:~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myphpimage latest 18e37a88b169 25 minutes ago 254MB mycustomed_httpd latest ca4e4c000933 4 hours ago 145MB httpd latest d16a51d08814 27 hours ago 145MB debian latest d91720f514f7 32 hours ago 124MB httpd 2.4 f2789344c573 3 weeks ago 145MB ubuntu@ip-172-30-2-68:~$ docker image rm myphpimage Untagged: myphpimage:latest Deleted: sha256:18e37a88b169c57a16f89c0fec9b0ffd85385288a2dff5937fbc6b32fd8d4f8c Deleted: sha256:4bd32a600e5e766a9ae2db56cd4cb31ff456a92e15cd73e8f8892af5b52dc7da Deleted: sha256:2bafdaef3166fc360917e3cc42ec7187353ae80d8eeae57787070eb282514b44 Deleted: sha256:35c5aa9209ce33fcac1fc5a38c6c23996812ea31af756bb12f5a0cfb89941446 Deleted: sha256:006b4206c326112ab49cd1373122359c631eb248eb2fd997de6f90c8d2237910 Deleted: sha256:d3420fb86244b0f4a0562f60cddb1d2df5f4843e91c72d4697c9577afb4467dc Deleted: sha256:35aeb6ea7d05f6262ccb09444d872e2393aa6b008c136e5469b0efc43bb1f608 Deleted: sha256:af091e7d6f32c129f5857f025d3da69dd9694b484148c0f8be47eb60dd61d184 ubuntu@ip-172-30-2-68:~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mycustomed_httpd latest ca4e4c000933 4 hours ago 145MB httpd latest d16a51d08814 27 hours ago 145MB debian latest d91720f514f7 32 hours ago 124MB httpd 2.4 f2789344c573 3 weeks ago 145MB
ubuntu@ip-172-30-2-68:~$ docker load -i saved.tar c42c46a763ac: Loading layer 133.5MB/133.5MB b0036fa91579: Loading layer 3.584kB/3.584kB Loaded image: myphpimage:latest ubuntu@ip-172-30-2-68:~$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE myphpimage latest 18e37a88b169 27 minutes ago 254MB mycustomed_httpd latest ca4e4c000933 4 hours ago 145MB httpd latest d16a51d08814 27 hours ago 145MB debian latest d91720f514f7 32 hours ago 124MB httpd 2.4 f2789344c573 3 weeks ago 145MB