Dockerコンテナの作成から実行までやってみる
環境
Windows 11 25H2 (26200.8037)
WSLバージョン
Ubuntuバージョン
WSLバージョン
PS C:\Users\xxx> wsl --version
WSL バージョン: 2.3.24.0
カーネル バージョン: 5.15.153.1-2
WSLg バージョン: 1.0.65
MSRDC バージョン: 1.2.5620
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26100.1-240331-1435.ge-release
Windows バージョン: 10.0.26200.8037
WSL バージョン: 2.3.24.0
カーネル バージョン: 5.15.153.1-2
WSLg バージョン: 1.0.65
MSRDC バージョン: 1.2.5620
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26100.1-240331-1435.ge-release
Windows バージョン: 10.0.26200.8037
Ubuntuバージョン
xxx@xxx:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
手順
1. まずUbuntuのDockerコンテナを実行しbashを起動
実行結果:
2. Ubuntuコンテナ上でnginxをインストール
でも "E: Unable to locate package nginx" と表示されインストールできない。
再度 "apt-get install -y nginx" を実行
これでDockerコンテナは停止状態となる。念のため docker ps -a で確認。
3. 新しいDockerイメージを作成
4. nginxコンテナの実行
5. 動作確認
6. コンテナの停止
7. コンテナ再開
8. コンテナの削除
docker run -it --name ubuntu1 ubuntu /bin/bash
実行結果:
xxx@xxx:~$ docker run -it --name ubuntu1 ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
689b91d88a0f: Pull complete
b22f29e93d86: Download complete
Digest: sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103d36e876938d28895b
Status: Downloaded newer image for ubuntu:latest
2. Ubuntuコンテナ上でnginxをインストール
apt-get install -y nginx
でも "E: Unable to locate package nginx" と表示されインストールできない。
root@876becd7e101:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package nginx
これはパッケージリストが古いから、のようなのでパッケージリストを更新すればOK
apt-get update
root@876becd7e101:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:3 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [3505 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [331 kB]
Get:7 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [1508 kB]
Get:8 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [1989 kB]
Get:9 http://archive.ubuntu.com/ubuntu noble/restricted amd64 Packages [117 kB]
Get:10 http://archive.ubuntu.com/ubuntu noble/main amd64 Packages [1808 kB]
Get:11 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [34.8 kB]
Get:12 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [19.3 MB]
Get:13 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [2377 kB]
Get:14 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [2155 kB]
Get:15 http://archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [38.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [3692 kB]
Get:17 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [36.1 kB]
Get:18 http://archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [49.5 kB]
Get:19 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Packages [695 B]
Fetched 37.6 MB in 4s (9283 kB/s)
Reading package lists... Done
再度 "apt-get install -y nginx" を実行
root@876becd7e101:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
iproute2 libatm1t64 libbpf1 libcap2-bin libelf1t64 libmnl0 libpam-cap libxtables12 nginx-common
Suggested packages:
iproute2-doc python3:any fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
iproute2 libatm1t64 libbpf1 libcap2-bin libelf1t64 libmnl0 libpam-cap libxtables12 nginx nginx-common
0 upgraded, 10 newly installed, 0 to remove and 3 not upgraded.
Need to get 2026 kB of archives.
After this operation, 5799 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libelf1t64 amd64 0.190-1.1ubuntu0.1 [57.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble/main amd64 libbpf1 amd64 1:1.3.0-2build2 [166 kB]
Get:3 http://archive.ubuntu.com/ubuntu noble/main amd64 libmnl0 amd64 1.0.5-2build1 [12.3 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble/main amd64 libxtables12 amd64 1.8.10-3ubuntu2 [35.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libcap2-bin amd64 1:2.66-5ubuntu2.2 [34.2 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 iproute2 amd64 6.1.0-1ubuntu6.2 [1120 kB]
Get:7 http://archive.ubuntu.com/ubuntu noble/main amd64 libatm1t64 amd64 1:2.5.1-5.1build1 [22.9 kB]
Get:8 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libpam-cap amd64 1:2.66-5ubuntu2.2 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx-common all 1.24.0-2ubuntu7.6 [43.5 kB]
Get:10 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx amd64 1.24.0-2ubuntu7.6 [521 kB]
Fetched 2026 kB in 3s (731 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libelf1t64:amd64.
(Reading database ... 4381 files and directories currently installed.)
Preparing to unpack .../0-libelf1t64_0.190-1.1ubuntu0.1_amd64.deb ...
Unpacking libelf1t64:amd64 (0.190-1.1ubuntu0.1) ...
Selecting previously unselected package libbpf1:amd64.
Preparing to unpack .../1-libbpf1_1%3a1.3.0-2build2_amd64.deb ...
Unpacking libbpf1:amd64 (1:1.3.0-2build2) ...
Selecting previously unselected package libmnl0:amd64.
Preparing to unpack .../2-libmnl0_1.0.5-2build1_amd64.deb ...
Unpacking libmnl0:amd64 (1.0.5-2build1) ...
Selecting previously unselected package libxtables12:amd64.
Preparing to unpack .../3-libxtables12_1.8.10-3ubuntu2_amd64.deb ...
Unpacking libxtables12:amd64 (1.8.10-3ubuntu2) ...
Selecting previously unselected package libcap2-bin.
Preparing to unpack .../4-libcap2-bin_1%3a2.66-5ubuntu2.2_amd64.deb ...
Unpacking libcap2-bin (1:2.66-5ubuntu2.2) ...
Selecting previously unselected package iproute2.
Preparing to unpack .../5-iproute2_6.1.0-1ubuntu6.2_amd64.deb ...
Unpacking iproute2 (6.1.0-1ubuntu6.2) ...
Selecting previously unselected package libatm1t64:amd64.
Preparing to unpack .../6-libatm1t64_1%3a2.5.1-5.1build1_amd64.deb ...
Unpacking libatm1t64:amd64 (1:2.5.1-5.1build1) ...
Selecting previously unselected package libpam-cap:amd64.
Preparing to unpack .../7-libpam-cap_1%3a2.66-5ubuntu2.2_amd64.deb ...
Unpacking libpam-cap:amd64 (1:2.66-5ubuntu2.2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../8-nginx-common_1.24.0-2ubuntu7.6_all.deb ...
Unpacking nginx-common (1.24.0-2ubuntu7.6) ...
Selecting previously unselected package nginx.
Preparing to unpack .../9-nginx_1.24.0-2ubuntu7.6_amd64.deb ...
Unpacking nginx (1.24.0-2ubuntu7.6) ...
Setting up libatm1t64:amd64 (1:2.5.1-5.1build1) ...
Setting up nginx-common (1.24.0-2ubuntu7.6) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC entries checked: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.38.2 /usr/local/share/perl/5.38.2 /usr/lib/x86_64-linux-gnu/perl5/5.38 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.38 /usr/share/perl/5.38 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 8.)
debconf: falling back to frontend: Teletype
Setting up libelf1t64:amd64 (0.190-1.1ubuntu0.1) ...
Setting up libcap2-bin (1:2.66-5ubuntu2.2) ...
Setting up libmnl0:amd64 (1.0.5-2build1) ...
Setting up libxtables12:amd64 (1.8.10-3ubuntu2) ...
Setting up libpam-cap:amd64 (1:2.66-5ubuntu2.2) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC entries checked: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.38.2 /usr/local/share/perl/5.38.2 /usr/lib/x86_64-linux-gnu/perl5/5.38 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.38 /usr/share/perl/5.38 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 8.)
debconf: falling back to frontend: Teletype
Setting up libbpf1:amd64 (1:1.3.0-2build2) ...
Setting up iproute2 (6.1.0-1ubuntu6.2) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC entries checked: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.38.2 /usr/local/share/perl/5.38.2 /usr/lib/x86_64-linux-gnu/perl5/5.38 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.38 /usr/share/perl/5.38 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 8.)
debconf: falling back to frontend: Teletype
Setting up nginx (1.24.0-2ubuntu7.6) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Processing triggers for libc-bin (2.39-0ubuntu8.7) ...
nginxをインストールしたら[CTRL]+[D]でbashプロセスを修了する。これでDockerコンテナは停止状態となる。念のため docker ps -a で確認。
xxx@xxx:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
876becd7e101 ubuntu "/bin/bash" 5 minutes ago Exited (0) 42 seconds ago ubuntu1
3. 新しいDockerイメージを作成
nginxをインストールしたDockerコンテナ(ubuntu1)をベースに新しいDockerイメージを作成する。
作成するイメージ名は何でも良いが、ここでは"masa/nginx"とした。
実行結果:
docker imageを見ると新たにDockerイメージが作成されていることがわかる。
作成するイメージ名は何でも良いが、ここでは"masa/nginx"とした。
docker commit ubuntu1 masa/nginx
実行結果:
xxx@xxx:~$ docker commit ubuntu1 masa/nginx
sha256:031aa9d31a4aed2c0b9ee372341e9d22a2413996a81547ef23ea5abfa222a9e8
docker imageを見ると新たにDockerイメージが作成されていることがわかる。
xxx@xxx:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
masa/nginx latest a575318af095 4 seconds ago 146MB
ubuntu latest b28307c40a80 6 days ago 78.1MB
4. nginxコンテナの実行
ubuntu+nginxイメージができたので、あとはこれを起動すれば簡単にWebサーバーを構築できる。
実行結果:
docker run -d -p 80:80 --name nginx1 masa/nginx /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
| -d | バックグラウンドで実行 |
| -p 80:80 | コンテナのポート80とホストのポート80をマッピング |
| --name nginx1 | コンテナ名は "nginx1" とした。何でも良い |
| masa/nginx | 3.で作成したDockerイメージ名 |
| /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf | nginxの起動。-gはグローバル設定の指定で"deamon off;"でデーモン無効化→フォアグラウンドで動かす、という意味らしい。(でないとDockerでは起動できない) |
実行結果:
xxx@xxx:~$ docker run -d -p 80:80 --name nginx1 masa/nginx /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
eaee932fc1a6df74332b7cf36bab1ecaadb23c694fec7a6f6f1322ca395aad2c
5. 動作確認
curlコマンドでlocalhost:80に接続してみるとhtmlが帰り、nginx正常に動作していることがわかる。
xxx@xxx:~$ curl localhost:80Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
6. コンテナの停止
実行中のDockerコンテナ "nginx1" を停止する。
停止前:
docker stop nginx1
停止前:
xxx@xxx:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaee932fc1a6 masa/nginx "/usr/sbin/nginx -g …" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp nginx1
コンテナ停止:
xxx@xxx:~$ docker stop nginx1
nginx1
"docker ps" で実行中コンテナを確認。"nginx1" は表示されないので停止していることがわかる。
xxx@xxx:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7. コンテナ再開
停止したコンテナを再開する。
実行結果:
docker start ngnix1
実行結果:
xxx@xxx:~$ docker start nginx1
nginx1
xxx@xxx:~$ curl localhost:80
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
8. コンテナの削除
最後にコンテナを停止しコンテナの削除、イメージの削除まで行う。
コンテナ停止: docker stop
コンテナ停止: docker stop
xxx@xxx:~$ docker stop nginx1
nginx1
コンテナ削除: docker rm
xxx@xxx:~$ docker rm nginx1
nginx1
コンテナイメージ削除: docker rmi
xxx@xxx:~$ docker rmi masa/nginx
Untagged: masa/nginx:latest
Deleted: sha256:b418404f73405ae1f07b311f1c008e63544099cbed5f92dc63f01a21577a1c3a
Deleted: sha256:73a461be8ff6213f197a10dc96d5563bfa2408349234280944ff13aa7aa1ae04
ついでにubuntuコンテナも削除する:
xxx@xxx:~$ docker rm ubuntu1
ubuntu1
xxx@xxx:~$ docker rmi ubuntu:latest
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103d36e876938d28895b
Deleted: sha256:b28307c40a80946234f0ea8ff355d37d7802c7e9c93bd5f84002177ef70937b6
Deleted: sha256:5e732af9e7c568c9b41ecabc76ac93a58471934d5155070dfd64a9567667fd9d
Docker 学習