カスタムなイメージを作る方法は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 |
FROM httpd COPY index.html /usr/local/apache2/htdocs/ |
$ 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 history --no-trunc myimage01 IMAGE CREATED CREATED BYSIZE COMMENT sha256:8847e6dad5013239f092f6e27c94c4eedc62f17dfadbce92d8c47104c5c1249a 2 minutes ago /bin/sh -c #(nop) COPY file:d86ee149e968cf6cafd17250821ff65b955dcfa0b8d5d0e79f7b22518b0247ad in /usr/local/apache2/htdocs/ 65B sha256:d16a51d08814012645a6fcc2e3adf323ff49d7d00b836bb872de92ad7156bf53 26 hours ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B <missing> 26 hours ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 26 hours ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecdef4786f48d91a96f8f0707f6179816ccb98db661bfb96b90 in /usr/local/bin/ 138B <missing> 26 hours ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B <missing> 26 hours ago /bin/sh -c set -eux; savedAptMark="$(apt-mark showmanual)"; apt-get update; apt-get install -y --no-install-recommends bzip2 dirmngr dpkg-dev gcc gnupg libapr1-dev libaprutil1-dev libbrotli-dev libcurl4-openssl-dev libjansson-dev liblua5.2-dev libnghttp2-dev libpcre3-dev libssl-dev libxml2-dev make wget zlib1g-dev ; rm -r /var/lib/apt/lists/*; ddist() { local f="$1"; shift; local distFile="$1"; shift; local success=; local distUrl=; for distUrl in 'https://www.apache.org/dyn/closer.cgi?action=download&filename=' https://downloads.apache.org/ https://www-us.apache.org/dist/ https://www.apache.org/dist/ https://archive.apache.org/dist/ ; do if wget -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then success=1; break; fi; done; [ -n "$success" ]; }; ddist 'httpd.tar.bz2' "httpd/httpd-$HTTPD_VERSION.tar.bz2"; echo "$HTTPD_SHA256 *httpd.tar.bz2" | sha256sum -c -; ddist 'httpd.tar.bz2.asc' "httpd/httpd-$HTTPD_VERSION.tar.bz2.asc"; export GNUPGHOME="$(mktemp -d)"; for key in DE29FB3971E71543FD2DC049508EAEC5302DA568 13155B0E9E634F42BF6C163FDDBA64BA2C312D2F 8B39757B1D8A994DF2433ED58B3A601F08C975E5 31EE1A81B8D066548156D37B7D6DBFD1F08E012A A10208FEC3152DD7C0C9B59B361522D782AB7BD1 3DE024AFDA7A4B15CB6C14410F81AA8AB0D5F771 EB138C6AF0FC691001B16D93344A844D751D7F27 CBA5A7C21EC143314C41393E5B968010E04F9A89 3C016F2B764621BB549C66B516A96495E2226795 937FB3994A242BA9BF49E93021454AF0CC8B0F7E EAD1359A4C0F2D37472AAF28F55DF0293A4E7AC9 4C1EADADB4EF5007579C919C6635B6C0DE885DD3 01E475360FCCF1D0F24B9D145D414AE1E005C9CB 92CCEF0AA7DD46AC3A0F498BCA6939748103A37E D395C7573A68B9796D38C258153FA0CD75A67692 FA39B617B61493FD283503E7EED1EA392261D073 984FB3350C1D5C7A3282255BB31B213D208F5064 FE7A49DAA875E890B4167F76CCB2EB46E76CF6D0 39F6691A0ECF0C50E8BB849CF78875F642721F00 29A2BA848177B73878277FA475CAA2A3F39B3750 120A8667241AEDD4A78B46104C042818311A3DE5 453510BDA6C5855624E009236D0BC73A40581837 0DE5C55C6BF3B2352DABB89E13249B4FEC88A0BF 7CDBED100806552182F98844E8E7E00B4DAA1988 A8BA9617EF3BCCAC3B29B869EDB105896F9522D8 3E6AC004854F3A7F03566B592FF06894E55B0D0E 5B5181C2C0AB13E59DA3F7A3EC582EB639FF092C A93D62ECC3C8EA12DB220EC934EA76E6791485A8 65B2D44FE74BD5E3DE3AC3F082781DE46D5954FA 8935926745E1CE7E3ED748F6EC99EE267EB5F61A E3480043595621FE56105F112AB12A7ADC55C003 93525CFCF6FDFFB3FD9700DD5A4B10AE43B56A27 C55AB7B9139EB2263CD1AABC19B033D1760C227B 26F51EF9A82F4ACB43F1903ED377C9E7D1944C66 ; do gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key"; done; gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2; command -v gpgconf && gpgconf --kill all || :; rm -rf "$GNUPGHOME" httpd.tar.bz2.asc; mkdir -p src; tar -xf httpd.tar.bz2 -C src --strip-components=1; rm httpd.tar.bz2; cd src; patches() { while [ "$#" -gt 0 ]; do local patchFile="$1"; shift; local patchSha256="$1"; shift; ddist "$patchFile" "httpd/patches/apply_to_$HTTPD_VERSION/$patchFile"; echo "$patchSha256 *$patchFile" | sha256sum -c -; patch -p0 < "$patchFile"; rm -f "$patchFile"; done; }; patches $HTTPD_PATCHES; gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; CFLAGS="$(dpkg-buildflags --get CFLAGS)"; CPPFLAGS="$(dpkg-buildflags --get CPPFLAGS)"; LDFLAGS="$(dpkg-buildflags --get LDFLAGS)"; ./configure --build="$gnuArch" --prefix="$HTTPD_PREFIX" --enable-mods-shared=reallyall --enable-mpms-shared=all --enable-pie CFLAGS="-pipe $CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="-Wl,--as-needed $LDFLAGS" ; make -j "$(nproc)"; make install; cd ..; rm -r src man manual; sed -ri -e 's!^(\s*CustomLog)\s+\S+!\1 /proc/self/fd/1!g' -e 's!^(\s*ErrorLog)\s+\S+!\1 /proc/self/fd/2!g' -e 's!^(\s*TransferLog)\s+\S+!\1 /proc/self/fd/1!g' -e 's!^(\s*User)\s+daemon\s*$!\1 www-data!g' -e 's!^(\s*Group)\s+daemon\s*$!\1 www-data!g' "$HTTPD_PREFIX/conf/httpd.conf" "$HTTPD_PREFIX/conf/extra/httpd-ssl.conf" ; grep -E '^\s*User www-data$' "$HTTPD_PREFIX/conf/httpd.conf"; grep -E '^\s*Group www-data$' "$HTTPD_PREFIX/conf/httpd.conf"; apt-mark auto '.*' > /dev/null; [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; find /usr/local -type f -executable -exec ldd '{}' ';' | awk '/=>/ { print $(NF-1) }' | sort -u | xargs -r dpkg-query --search | cut -d: -f1 | sort -u | xargs -r apt-mark manual ; apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; httpd -v 59.9MB <missing> 26 hours ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B <missing> 26 hours ago /bin/sh -c #(nop) ENV HTTPD_SHA256=eb397feeefccaf254f8d45de3768d9d68e8e73851c49afd5b7176d1ecf80c340 0B <missing> 26 hours ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.54 0B <missing> 26 hours ago /bin/sh -c set -eux; apt-get update; apt-get install -y --no-install-recommends ca-certificates libaprutil1-ldap libldap-common ; rm -rf /var/lib/apt/lists/* 4.76MB <missing> 26 hours ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B <missing> 26 hours ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chown www-data:www-data "$HTTPD_PREFIX" 0B <missing> 26 hours ago /bin/sh -c #(nop) ENV PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 0B <missing> 26 hours ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/local/apache2 0B <missing> 31 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 31 hours ago /bin/sh -c #(nop) ADD file:b78b777208be08edd8f297035cdfbacddb45170ad778fd643c792ee045187e39 in / 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 |
<html> <body> Your IP: <?php echo $_SERVER['REMOTE_ADDR'] ?> </body> </html> |
~/phpimage/Dockerfile |
FROM debian EXPOSE 80 ENV DEBIAN_FRONTEND=noninteractive RUN apt update \ && apt install -y apache2 php libapache2-mod-php \ && apt clean \ && rm -rf /var/lib/apt/lists/* \ && rm -f /var/www/html/index.html COPY index.php /var/www/html CMD /usr/sbin/apachectl -DFOREGROUND STOPSIGNAL SIGWINCH |
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