ラベル Docker の投稿を表示しています。 すべての投稿を表示
ラベル Docker の投稿を表示しています。 すべての投稿を表示

Docker:ホストからコンテナにファイルをコピーする方法

公開:2026.05.17(日) 19:03

ホストからコンテナにファイルをコピーする方法

関連:Docker #13:コンテナからホストへのファイルコピー

DockerのホストからDockerコンテナにファイルをコピーするには docker cp コマンドを使用する。

docker cp ファイルパス コンテナID:コンテナ内のディレクトリパス

WSLの場合 /mnt/Windowsのドライブレター でWindowsのファイルにもアクセスできるので、WindowsのファイルをDockerコンテナにコピーすることも可能と思われる。


Docker:WSL+Docker+PHP+MySQL+phpMyAdmin環境の作成

公開:2026.05.11(月) 02:23

WSL+Docker+PHP+MySQL+phpMyAdmin環境の作成

Docker Composeを使い、以下3つのコンテナを起動するためのcompose.yamlを作成する。

・php:8.3-apache は ホストのポート 8080 で公開
・php.ini-debug はあらかじめ準備しておく
・phpmyadmin/phpmyadmin は ホストのポート 8081 で公開
・mysql:8.0 のパスワードは compose.yamlと同階層に .env ファイルを置き、そこにパスワード設定

cpompose.yaml
services:
  php:
    image: php:8.3-apache
    volumes:
      - ./www/html:/var/www/html
      - ./php.ini-debug:/usr/local/etc/php/php.ini
    ports:
      - "8080:80"

  db:
    image: mysql:8.0
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
  
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8081:80"
    environment:
      PMA_HOST: db

.env
MYSQL_ROOT_PASSWORD=pswd

あとは "docker compose up -d" で起動でOK


"Docker Compose 設定"

Docker:コンテナ内のファイルをVSCodeで編集する方法

公開:2026.04.19 07:22

コンテナ内のファイルをVSCodeで編集する方法

Windows + WSL + Ubuntu + Docker環境で、Dockerコンテナ内のファイルをWindowsのVSCodeから編集する方法。

→ 拡張機能 "Dev Containers (Microsoft)" をインストールすることで可能らしい。

手順

1. VSCode左下のマーク(マークの名前は不明)をクリックする
下記例では "WSL:Ubuntu" と表示されているところ

2. 選択リストが表示されるので "実行中のコンテナーにアタッチ" を選択

3. 注意メッセージが表示されるので [了解] をクリック

4. 実行中のコンテナ一覧が表示されるので、対象のコンテナを選択

5. 新しいVSCodeが開くのでフォルダを開く

これでDockerコンテナ内のファイルが開けた!
VSCode凄すぎ。


WSL Docker VScode Tips

Docker 学習 #4:Docker Composeを学ぶ

公開:2026.04.16 09:39

Docker Composeを学ぶ

前回はDockerfileを使ってみたが
Docker 学習 #2:Dockerfileを学ぶ
Docker 学習 #3:Dockerfileを学ぶ #2
Dockerfileとは別に "Docker Compose" というのも存在する。

なんだか似たような物にも見えるが、
・Dockerfile … Dockerイメージ(Dockerコンテナのテンプレート的なもの)を作るための設計図
・Docker Compose … DockerイメージよりDockerコンテナを作成・実行するための設計図
と理解した。

過去にDockerコンテナをいくつか作っていたので、これらをDocker Composeで対応してみる。
※ 対象記事:WSL #32:Docker上にPHP環境を作る

環境

Windows 11 25H2 (26200.8037)

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

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

Docker Compose CPIプラグインのインストール確認

Compose CLIがインストールされていることを確認する。

docker compose version

実行結果:

      root@xxx:~/docker/php# docker compose version
      Docker Compose version v5.1.2
    

基準フォルダの作成

WSL上に作業用フォルダを作成する。
~/docker/php フォルダを作業フォルダとして作成した。

また、HTMLを配置するフォルダをWindows側にて D:\docker\data\www\html\ のフォルダとして作成した。

YAMLファイル

ファイル名:compose.yaml
バージョン記載:必要なし

ネット上の記事を見ていると "docker-compose.yml"だったり、"compose.yml"だったり色々なファイル名のサンプルが出ているが、現在の推薦は "compose.yaml" とのこと。
またcompose.yamlの先頭に "version: x.x" の指定は非推薦となっているので書かなくてよい。
https://docs.docker.jp/compose/compose-file/index.html#compose-spec-compose-file

必要なファイルの抽出

php:8.3-apacheイメージに含まれているPHP.iniファイルを抽出しておく。

◆ コンテナ作成:

docker create --name tmp php:8.3-apache

実行結果:

  root@xxx:~/docker# docker create --name tmp php:8.3-apache
  Unable to find image 'php:8.3-apache' locally
  8.3-apache: Pulling from library/php
  Digest: sha256:7912dada009d971264115de9e43cb2e97878ddfd09bd19418901217b8aa3c09a
  Status: Downloaded newer image for php:8.3-apache
  ec4fa7782cccafdd022b787c40c043444b538d9fc7ba52a35d200667995c1e05
    

◆ iniファイル抽出とコンテナ削除:

docker cp tmp:/usr/local/etc/php/php.ini-development ./php.ini-debug
docker cp tmp:/usr/local/etc/php/php.ini-production ./php.ini-production
docker rm tmp

実行結果:

      root@xxx:~/docker/php# docker create --name tmp php:8.3-apache
      a44488f822ae886df5ae51662fa5d5c13e35aba089353aec8a35a2d7bfe9f059
      root@xxx:~/docker/php# docker cp tmp:/usr/local/etc/php/php.ini-development ./php.ini-debug
      Successfully copied 73.3kB (transferred 75.3kB) to /root/docker/php/php.ini-debug
      root@xxx:~/docker/php# docker cp tmp:/usr/local/etc/php/php.ini-production ./php.ini-production
      Successfully copied 73.4kB (transferred 75.3kB) to /root/docker/php/php.ini-production
      root@xxx:~/docker/php# docker rm tmp
      tmp
    

compose.yaml作成

イメージは php:8.3-apache とする。
HTMLファイルルートを WindowsのD:\docker\data\www\html\ へマッピング、
ポートは80番をホストの8081番にマッピングする。

※先頭の半角スペースは "_" で記載
services:
__php:
____image: php:8.3-apache
____volumes:
______- /mnt/d/docker/data/www/html:/var/www/html
______- ./php.ini-debug:/usr/local/etc/php/php.ini
____ports:
______- "8081:80"

ここまでの状態で動くか実行してみる。

docker compose up -d

実行結果:

root@xxx:~/docker/php# docker compose up -d
[+] up 1/1
 ✔ Container php-php-1 Started                                                                                      0.4s
  

ブラウザで localhost:8081 へ接続
またhtmlを配置していないのでエラーとなるが、動いてるっぽい。

Windowsのd:\docker\data\www\html\phpinfo.phpを作成する。

    
  

問題なし。
ついでに "display_errors" の値が "On" になっているか確認。
"On"であればphp.ini-debugが正しく読み込まれている。

production版のphp環境作成

先ほどはdebug環境のコンテナだったが、ついでにproduction版の環境も作れるようにしておく。
production-compose.yamlを作成し以下を記載

services:
__php:
____volumes:
______- ./php.ini-production:/usr/local/etc/php/php.ini

コンテナ起動:

docker compose -f compose.yaml -f production-compose.yaml up -d

実行結果:

root@xxx:~/docker/php# docker compose -f compose.yaml -f production-compose.yaml up -d
[+] up 1/1
 ✔ Container php-php-1 Started                                                                                      0.3s
  

ブラウザで localhost:8081/phpinfo.php を表示、display_errorsが "Off" であればphp.ini-productionが正常に読み込まれている。

いい感じ



Docker 学習 #3:Dockerfileを学ぶ #2

公開:2026.04.13 16:37

Dockerfileを学ぶ #2

以下記事を参考にDockerfileを使ってみる。
Dockerfileとdocker buildコマンドでDockerイメージの作成

環境

Windows 11 25H2 (26200.8037)

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

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

おさらい

前回までで、やっとDockerfileを使った一覧の動作を試すことができた。
今回は作成したDockerイメージをdocker runで実行する際に指定するパラメータもDockerfileで指定してみよう、という内容。

ちなみに前回使ったdocker runコマンドは以下の感じ。

docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

準備

まずは前回実行したコンテナの停止、削除を行う。

docker stop nginx1
docker rm nginx1

CMD命令とENTRYPOINT命令

コンテナ起動時に実行するコマンドを指定するために CMD命令、ENTRYPOINT命令が使える。

ENTRYPOINT命令は、コンテナのデフォルトの実行可能ファイルを設定する。
つまり、コンテナ起動時に必ず実行するコマンドのこと。
docker runで指定した引数は、このETNRYPOINT命令で指定したコマンドへの引数として追加される。
基本はENTRYPOINT命令で指定した内容が必ず実行されるが docker runコマンド実行時に --entrypointフラグでコマンドを指定するとENTRYPOINT命令で設定したコマンドは上書きされる。

CMD命令は、コンテナがDockerイメージから起動された時に実行する規定のコマンドを指定する。
docker runでコマンドを指定しなければCMD命令で指定した値が使用される。
docker runコマンドでコマンドライン引数を指定した場合、CMD命令で値はdocker runコマンドで指定した値で上書きされる。


手順

1. Dockerfile修正

ということでDockerfileの最後に nginxを起動するためのコマンドをENTRYPOINT命令で追加してみる。

FROM ubuntu
LABEL maintainer="xxxx@gmail.com"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
ENTRYPOINT /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

2. Dockerイメージ作成

前回作成したイメージは残しておいて、タグ名を"1.0"→"1.1"としてDockerイメージを作成する。

docker build -t takipone/nginx:1.1 .

実行結果:
エラーが出てしまった。

root@xxx:~/nginx1# docker build -t takipone/nginx:1.1 .
[+] Building 1.8s (8/8) FINISHED                                                             docker:default
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 233B                                                                   0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       1.2s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 31B                                                                       0.0s
 => CACHED [2/3] RUN apt-get update && apt-get install -y nginx                                        0.0s
 => CACHED [3/3] COPY index.html //var/www/html/                                                       0.0s
 => exporting to image                                                                                 0.2s
 => => exporting layers                                                                                0.0s
 => => exporting manifest sha256:2e80db00b8f0856137d07781f95cc7ad752063fcc6f1b60af169452a1f606d4d      0.0s
 => => exporting config sha256:37913b24c791dbdcec5e27f3c073eb2c0edaf964721fbcedf0d68f2f4986cae2        0.0s
 => => exporting attestation manifest sha256:c36c542ebf03bb8d62154eafb8952713e741fde0afc3ff63a9651c00  0.1s
 => => exporting manifest list sha256:77ad25ee6bfc7edff996be2e559248ab075030d74a1d86d1ace3fcb66d503f3  0.0s
 => => naming to docker.io/takipone/nginx:1.1                                                          0.0s
 => => unpacking to docker.io/takipone/nginx:1.1                                                       0.0s

 1 warning found (use docker --debug to expand):
 - JSONArgsRecommended: JSON arguments recommended for ENTRYPOINT to prevent unintended behavior related to OS signals (line 5)
    

Dockerfileを学ぶ #2

以下記事を参考にDockerfileを使ってみる。
Dockerfileとdocker buildコマンドでDockerイメージの作成

環境

Windows 11 25H2 (26200.8037)

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

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

おさらい

前回までで、やっとDockerfileを使った一覧の動作を試すことができた。
今回は作成したDockerイメージをdocker runで実行する際に指定するパラメータもDockerfileで指定してみよう、という内容。

ちなみに前回使ったdocker runコマンドは以下の感じ。

docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

準備

まずは前回実行したコンテナの停止、削除を行う。

docker stop nginx1
docker rm nginx1

CMD命令とENTRYPOINT命令

コンテナ起動時に実行するコマンドを指定するために CMD命令、ENTRYPOINT命令が使える。

ENTRYPOINT命令は、コンテナのデフォルトの実行可能ファイルを設定する。
つまり、コンテナ起動時に必ず実行するコマンドのこと。
docker runで指定した引数は、このETNRYPOINT命令で指定したコマンドへの引数として追加される。
基本はENTRYPOINT命令で指定した内容が必ず実行されるが docker runコマンド実行時に --entrypointフラグでコマンドを指定するとENTRYPOINT命令で設定したコマンドは上書きされる。

CMD命令は、コンテナがDockerイメージから起動された時に実行する規定のコマンドを指定する。
docker runでコマンドを指定しなければCMD命令で指定した値が使用される。
docker runコマンドでコマンドライン引数を指定した場合、CMD命令で値はdocker runコマンドで指定した値で上書きされる。


手順

1. Dockerfile修正

ということでDockerfileの最後に nginxを起動するためのコマンドをENTRYPOINT命令で追加してみる。

FROM ubuntu
LABEL maintainer="xxxx@gmail.com"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
ENTRYPOINT /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

2. Dockerイメージ作成

前回作成したイメージは残しておいて、タグ名を"1.0"→"1.1"としてDockerイメージを作成する。

docker build -t takipone/nginx:1.1 .

実行結果:
なんか警告っぽいのが出た。

root@xxx:~/nginx1# docker build -t takipone/nginx:1.1 .
[+] Building 1.8s (8/8) FINISHED                                                             docker:default
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 233B                                                                   0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       1.2s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 31B                                                                       0.0s
 => CACHED [2/3] RUN apt-get update && apt-get install -y nginx                                        0.0s
 => CACHED [3/3] COPY index.html //var/www/html/                                                       0.0s
 => exporting to image                                                                                 0.2s
 => => exporting layers                                                                                0.0s
 => => exporting manifest sha256:2e80db00b8f0856137d07781f95cc7ad752063fcc6f1b60af169452a1f606d4d      0.0s
 => => exporting config sha256:37913b24c791dbdcec5e27f3c073eb2c0edaf964721fbcedf0d68f2f4986cae2        0.0s
 => => exporting attestation manifest sha256:c36c542ebf03bb8d62154eafb8952713e741fde0afc3ff63a9651c00  0.1s
 => => exporting manifest list sha256:77ad25ee6bfc7edff996be2e559248ab075030d74a1d86d1ace3fcb66d503f3  0.0s
 => => naming to docker.io/takipone/nginx:1.1                                                          0.0s
 => => unpacking to docker.io/takipone/nginx:1.1                                                       0.0s

 1 warning found (use docker --debug to expand):
 - JSONArgsRecommended: JSON arguments recommended for ENTRYPOINT to prevent unintended behavior related to OS signals (line 5)
    

3. Dockerfile修正

警告の意味は、ENTRYPOINTのパラメーターをJSON形式にした方がいいよ、ということらしい。ということでDockerfileを修正する。

FROM ubuntu
LABEL maintainer="xxxx@gmail.com"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]

docker build実行

root@xxx:~/nginx1# docker build -t takipone/nginx:1.1 .
[+] Building 2.2s (8/8) FINISHED                                                             docker:default
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 247B                                                                   0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       1.6s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 31B                                                                       0.0s
 => CACHED [2/3] RUN apt-get update && apt-get install -y nginx                                        0.0s
 => CACHED [3/3] COPY index.html //var/www/html/                                                       0.0s
 => exporting to image                                                                                 0.2s
 => => exporting layers                                                                                0.0s
 => => exporting manifest sha256:7ae563324ed96ed865d90d4b838f4137b3ceae058030c51a857930b04da23971      0.0s
 => => exporting config sha256:f178d674921f316b017b996de84a119e88b579352d6e851e96a67f3357f0c923        0.0s
 => => exporting attestation manifest sha256:1cff542974d56aa19b0c5f3942426389a478953a8fea547ae729c98d  0.1s
 => => exporting manifest list sha256:446704b9a6dea8067f20e28c117d7dea2947e090d1d8c006e6b810dd5732b0c  0.0s
 => => naming to docker.io/takipone/nginx:1.1                                                          0.0s
 => => unpacking to docker.io/takipone/nginx:1.1                                                       0.0s
    

今度はうまくいった。

4. コンテナ起動

作成したDockerイメージを nginx2 の名前で実行してみる。
今回はnginxの起動もDockerfileに含んでいるのでその辺りの記述が不要になっている。

docker run -d --name nginx2 -p 80:80 takipone/nginx:1.1

問題なく動作した。

root@xxx:~/nginx1# docker run -d --name nginx2 -p 80:80 takipone/nginx:1.1
f0ca46cc8fb05fe1a709b0b8479302e4721b68655f3b7d5b07906b3d7d02f009
root@xxx:~/nginx1# curl localhost
Hello!
    


Docker 学習

Docker コマンド:docker build

公開:2026.04.13 09:54


docker build

DockerfileとコンテキストからDockerイメージを作成する。

docker build [ -t {イメージ名}:{タグ名}] {パス}


Docker コマンド

Docker 学習 #2:Dockerfileを学ぶ

Dockerfileを学ぶ

以下記事を参考にDockerfileを使ってみる。
Dockerfileとdocker buildコマンドでDockerイメージの作成

環境

Windows 11 25H2 (26200.8037)

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

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

手順

基本的には「Docker 学習 #1 コンテナの作成から実行までやってみる」で行なった内容をDockerfile化する、という流れである。

1. 作業フォルダ作成

Ubuntu上に"nginx1"という作業フォルダを作成する。(フォルダ名は何でも良い)
mkdir nginx1
cd nginx1

実行結果:
root@xxx:~# mkdir nginx1
root@xxx:~# cd nginx1      
    

2. Dockerfile作成

"Dockerfile"というファイル名に新規にファイルを作成し以下内容を記述する。
FROM ubuntu
MAINTAINER takipone <xxxx@gmail.com>
RUN apt-get install -y nginx
ADD index.html /usr/share/nginx/html/

参考サイトではvimで編集しているが、今回はVSCodeのリモートエクスプローラーで編集を行なった。

前回 "apt-get update" を先に実行しておかないと nginxのインストールができなかったので、多分上記ではうまく行かないと思うがとりあえずサンプルのままやってみる。

3. テスト用htmlファイル作成

作業フォルダにテスト用html(index.html)を作成する。

echo 'Hello!' > index.html

私の場合は、2.と同じくVSCodeを使い、nginx1¥index.htmlファイルを作成、内容として「Hello!」を書き込んだ。

3. フォルダ構成確認

nginx1フォルダには "Dockerfile"と"index.html"の2つが存在する状態である。

実行結果:

root@xxx:~/nginx1# ls
Dockerfile  index.html
      

4. Dockerイメージ作成

docker buildコマンドを使いDockerイメージを作成する。
作成するイメージ名を -t(または--tag)オプションで指定し、名前は "takipone/nginx"、タブ名は "1.0" とする。(何でも良い)
docker build -t takipone/nginx:1.0 .

実行結果:

root@xxx:~/nginx1# docker build -t takipone/nginx .
[+] Building 6.2s (6/7)                                                                      docker:default
 => [internal] load build definition from dockerfile                                                   0.0s
 => => transferring dockerfile: 153B                                                                   0.0s
 => WARN: MaintainerDeprecated: Maintainer instruction is deprecated in favor of using label (line 2)  0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       2.1s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  3.6s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.0s
 => => sha256:689b91d88a0f4086057ec826027b128902ecf2b516be510371c115bc55da19a6 29.73MB / 29.73MB       3.1s
 => => extracting sha256:689b91d88a0f4086057ec826027b128902ecf2b516be510371c115bc55da19a6              0.4s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 44B                                                                       0.0s
 => ERROR [2/3] RUN apt-get install -y nginx                                                           0.4s
------
 > [2/3] RUN apt-get install -y nginx:
0.288 Reading package lists...
0.292 Building dependency tree...
0.293 Reading state information...
0.293 E: Unable to locate package nginx
------

 1 warning found (use docker --debug to expand):
 - MaintainerDeprecated: Maintainer instruction is deprecated in favor of using label (line 2)
dockerfile:3
--------------------
   1 |     FROM ubuntu
   2 |     MAINTAINER takipone 
   3 | >>> RUN apt-get install -y nginx
   4 |     ADD index.html /usr/share/nginx/html/
   5 |
--------------------
ERROR: failed to build: failed to solve: process "/bin/sh -c apt-get install -y nginx" did not complete successfully: exit code: 100
      

"apt-get install -y nginx"の行でエラーになったので、やはり "apt-get update" が必要なんだと思う。ということで以下のように変更。

RUN apt-get update && apt-get install -y nginx

&& は apt-get updateが成功したら次を実行、という意味で、Dockerではこの形式で指定するのが推薦だそう。
RUNコマンドを2行に分けて記述するとそれぞれが別レイヤーとなり、思わぬキャッシュを使うなどしてバグの原因になる(by ChatGPT)らしい。

また、2行目の"MAINTAINER"で警告が発生。
https://docs.docker.jp/engine/reference/builder.html によるとMAINTAINERは非推薦で、代替としてLABEL命令を使え、とのこと。
MAINTAINERフィールドに相当するラベルは、以下のように指定する。

LABEL maintainer="xxx@gmail.com"

上記を踏まえ、Dockerfileを以下のように修正する。

FROM ubuntu
LABEL maintainer="xxxx@gmail.com"
RUN apt-get update && apt-get install -y nginx
ADD index.html /usr/share/nginx/html/

再度 docker build を実行する。

docker build -t takipone/nginx:1.0 .

実行結果:

root@xxx~/nginx1# docker build -t takipone/nginx:1.0 .
[+] Building 17.9s (8/8) FINISHED                                                            docker:default
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 170B                                                                   0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       1.1s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => CACHED [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831cc  0.1s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.0s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 31B                                                                       0.0s
 => [2/3] RUN apt-get update && apt-get install -y nginx                                              13.8s
 => [3/3] ADD index.html /usr/share/nginx/html/                                                        0.1s
 => exporting to image                                                                                 2.5s
 => => exporting layers                                                                                1.8s
 => => exporting manifest sha256:13cc310082b1a95a8d72b255c885a242dddc590222ea41894284691d2ad0dc3f      0.0s
 => => exporting config sha256:8c0db8b83f49fc5567911598b09335e05f8c3e3f0f96b5962cec25ce48b5719b        0.0s
 => => exporting attestation manifest sha256:66ab69b59bd4fd7f88ca34c329c841e5c427da22446fe40643a8e02b  0.1s
 => => exporting manifest list sha256:c979f07e3ccc99200cbbc283e752b438fb4b8a6a83abd8b8696424fa4c635fd  0.0s
 => => naming to docker.io/takipone/nginx:1.0                                                          0.0s
 => => unpacking to docker.io/takipone/nginx:1.0                                                       0.4s
      
うまくいったようだ。

5. イメージ確認

docker imagesコマンドでイメージが作成されたことを確認。

root@xxx:~/nginx1# docker images
                                                                                        i Info →   U  In Use
IMAGE                                    ID             DISK USAGE   CONTENT SIZE   EXTRA
takipone/nginx:1.0                       c979f07e3ccc        231MB         74.7MB
      

6. コンテナ実行

作成したDockerイメージをdocker runで実行してみる。

docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

実行結果:

root@xxx:~/nginx1# docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
2bd815cbf7f8d7f925d2f5a179cf715c5561ea96dca99f940c8c9f315e964f6a
      

curlコマンドで動作確認。

curl localhost

実行結果: Hello! と表示されるはずが nginx標準のindex.htmlが表示されてしまっている。

root@xxx:~/nginx1# curl localhost



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.

7. Dockerfile修正

先ほど思った動作にならなかったのは index.htmlの格納先を /usr/share/nginx/html/ として指定していたのが原因。
正しくは /var/www/html/ とのこと。

また、ファイルのコピーだけであればADDではなくCOPY命令を使った方がいいらしい。
(参考:Docker のベストプラクティス: Dockerfile の ADD 命令と COPY 命令の違いを理解する)
ということでADD命令をCOPY命令へ変更する。

修正後のDockerfile:

FROM ubuntu
LABEL maintainer="xxxx@gmail.com"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/

起動中コンテナ停止、削除から再度コンテナの実行までを行う。

docker stop nginx1
docker rm nginx1
docker rmi takipone/nginx:1.0
docker build -t takipone/nginx:1.0 .
docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
curl index.html
root@xxx:~/nginx1# docker stop nginx1
nginx1
root@xxx:~/nginx1# docker rm nginx1
nginx1
root@xxx:~/nginx1# docker rmi takipone/nginx:1.0
Untagged: takipone/nginx:1.0
Deleted: sha256:c979f07e3ccc99200cbbc283e752b438fb4b8a6a83abd8b8696424fa4c635fd5
    
root@xxx:~/nginx1# docker build -t takipone/nginx:1.0 .
[+] Building 2.8s (8/8) FINISHED                                                             docker:default
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 162B                                                                   0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                       1.5s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                        0.0s
 => [1/3] FROM docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => => resolve docker.io/library/ubuntu:latest@sha256:84e77dee7d1bc93fb029a45e3c6cb9d8aa4831ccfcc7103  0.1s
 => [internal] load build context                                                                      0.0s
 => => transferring context: 31B                                                                       0.0s
 => CACHED [2/3] RUN apt-get update && apt-get install -y nginx                                        0.0s
 => [3/3] COPY index.html //var/www/html/                                                              0.1s
 => exporting to image                                                                                 0.8s
 => => exporting layers                                                                                0.2s
 => => exporting manifest sha256:134387c39e899fafa193bf37a94a00303cd4dcc1ca87af8245c6d7c4d990b872      0.0s
 => => exporting config sha256:edc638c3b0d7aae863f5554af9dd0404a504fd0d76e0a8b1def16caa20e0b4d4        0.0s
 => => exporting attestation manifest sha256:138e8eeb22d6e4f0c5aa36fef4ac1771724f9b57aaaffbc65275ebcb  0.1s
 => => exporting manifest list sha256:0e7cfd872fc98478c614b234a5c1fe718decac28ea9b8d9bb44bdfaba2d5619  0.0s
 => => naming to docker.io/takipone/nginx:1.0                                                          0.0s
 => => unpacking to docker.io/takipone/nginx:1.0                                                       0.4s
    
root@xxx:~/nginx1# docker run -d -p 80:80 --name nginx1 takipone/nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
b336b6287d8caa0d3d60df81e4d0604b304a056bd0e341474a69a3502e0c57bc
    

うまくいった。

root@xxx:~/nginx1# curl localhost
Hello!
    
長くなったので次回に続ける。


Docker 学習

Docker 学習 #1 コンテナの作成から実行までやってみる

公開:2026.04.10 17:57

Dockerコンテナの作成から実行までやってみる

以下記事を参考にDockerコンテナの作成から実行まで一通り試してみる。
ついに1.0がリリース! Dockerのインストールと主なコマンドの使い方

環境

Windows 11 25H2 (26200.8037)

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

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

手順

1. まずUbuntuのDockerコンテナを実行しbashを起動
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 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: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.


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
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 学習

Docker コマンド:docker start

公開:2026.04.10 11:32


docker start

停止中のDockerコンテナを再実行する。
docker start [-i] {コンテナ名} | {コンテナID}

-iオプションは docker run 同様にbashなどのシェルを実行する際に付加する。


Docker

Docker コマンド:docker rmi

公開:2026.04.10 11:16


docker rmi

Dockerイメージを削除する。
docker rmi {イメージ名} | {イメージID}


Docker

Docker コマンド:docker rm

公開:2026.04.10 11:15


docker rm

Dockerコンテナを削除する。
docker rm {コンテナ名} | {コンテナID}


Docker

Docker コマンド:docker stop

公開:2026.04.10 11:12


docker stop

実行中のDockerコンテナを停止する。
docker stop {コンテナ名} | {コンテナID}


Docker

Docker コマンド:docker commit

公開:2026.04.10 10:57


docker commit

Dockerコンテナから新しいDockerイメージを作成する。
docker commit {コンテナ名} | {コンテナID} [{ユーザー名}/]{イメージ名}

例)"docker run -it --name ubuntu1 ubuntu /bin/bash" で作成したコンテナがあり、そこにnginxをインストール、コンテナは[ctrl]+[D]で終了した。
このコンテナを"masa/nginx"の名前でイメージ作成する。
docker commit ubuntu1 masa/nginx


Dockerdocker ps

Docker コマンド:docker ps

公開:2026.04.10 10:42


docker ps

Dockerコンテナの一覧を表示する。
docker ps [-a]

実行中のコンテナ一覧を表示する。
-aオプションを付けると停止中のコンテナも表示する。


Dockerdocker ps

Docker コマンド:docker run

公開:2026.04.10 10:14


docker run

Dockerコンテナの作成、実行を行う。
docker run [オプション] [--name {コンテナ名}] {イメージ名} [:{タグ名}] [コンテナで実行するコマンド] [引数]

主なオプション

-d バックグラウンドで実行する
-i コンテナの標準入力を開く
-t tty(端末デバイス)を確保する
-p ポートマッピング {ホストのポート番号}:{コンテナのポート番号}


Docker

Docker コマンド:docker images

公開:2026.04.10 08:39


docker images

ダウンロードしたDockerイメージ(ローカルキャッシュ)の一覧を表示する。
docker images


Docker

Docker コマンド:docker pull

公開:2026.04.10 08:32


docker pull

Dockerイメージをダウンロードする。
docker pull {イメージ名(Repository)}:{タグ名(Tag)}

{イメージ名}でDockerイメージを指定し、{タグ名}でDockerイメージのバージョンを指定。
{タグ名}を指定しなければ "latest" (最新)を指定したことになる。
{イメージ名}は通常「ユーザー名/イメージ名」の形式であるが、Docker公式(トップレベルレポジトリ)の場合はユーザー名が付かない。


Docker

Docker:コマンド一覧

更新:2026.04.13 10:11
公開:2026.04.10 08:21


Docker コマンド一覧

Dockerコマンド、使わないとすぐに忘れちゃうのでメモ

コマンド 説明
docker pull Dockerイメージをダウンロード
docker images ダウンロードしたDockerイメージの一覧
docker run Dockerコンテナの作成・実行
docker ps Dockerコンテナの一覧
docker commit Dockerコンテナから新しいDockerイメージを作成
docker stop 実行中のDockerコンテナを停止
docker rm Dockerコンテナを削除
docker rmi Dockerイメージを削除
docker start 停止中のDockerコンテナを再実行
docker build DockerfileとコンテキストからDockerイメージを作成


Docker 記事一覧

Docker:WSL上にDocker環境を構築する方法

更新:2026.05.01(金) 22:10
公開:2026.03.16 16:15

WSL上にDocker環境を構築する方法

WSL上にDocker環境を構築したいが、1コマンドで完了、という訳にはいかないっぽいので手順をメモ

環境

Windows 11 Home 25H2 (26200.8037)

WSL:
PS C:\Users\xxx> wsl --version
WSL バージョン: 2.6.3.0
カーネル バージョン: 6.6.87.2-1
WSLg バージョン: 1.0.71
MSRDC バージョン: 1.2.6353
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26100.1-240331-1435.ge-release
Windows バージョン: 10.0.26200.8037

Ubuntu:
xxx@hppc:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.4 LTS
Release: 24.04
Codename: noble

手順

1. Dockerの前提パッケージのインストール

sudo apt install -y apt-transport-https ca-certificates gnupg-agent software-properties-common

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo apt install -y apt-transport-https ca-certificates gnupg-agent software-properties-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20240203).
ca-certificates set to manually installed.
The following NEW packages will be installed:
  apt-transport-https gnupg-agent
The following packages will be upgraded:
  python3-software-properties software-properties-common
2 upgraded, 2 newly installed, 0 to remove and 93 not upgraded.
Need to get 53.1 kB of archives.
After this operation, 64.5 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 apt-transport-https all 2.8.3 [3970 B]
Get:2 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 gnupg-agent all 2.4.4-2ubuntu17.4 [4672 B]
Get:3 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 software-properties-common all 0.99.49.4 [14.4 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 python3-software-properties all 0.99.49.4 [30.0 kB]
Fetched 53.1 kB in 1s (45.7 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 40805 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.8.3_all.deb ...
Unpacking apt-transport-https (2.8.3) ...
Selecting previously unselected package gnupg-agent.
Preparing to unpack .../gnupg-agent_2.4.4-2ubuntu17.4_all.deb ...
Unpacking gnupg-agent (2.4.4-2ubuntu17.4) ...
Preparing to unpack .../software-properties-common_0.99.49.4_all.deb ...
Unpacking software-properties-common (0.99.49.4) over (0.99.49.3) ...
Preparing to unpack .../python3-software-properties_0.99.49.4_all.deb ...
Unpacking python3-software-properties (0.99.49.4) over (0.99.49.3) ...
Setting up apt-transport-https (2.8.3) ...
Setting up python3-software-properties (0.99.49.4) ...
Setting up gnupg-agent (2.4.4-2ubuntu17.4) ...
Setting up software-properties-common (0.99.49.4) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for dbus (1.14.10-4ubuntu4.1) ...
    

2. DockerのGPGキーの取得とリポジトリの追加

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
Repository: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu noble stable'
Description:
Archive for codename: noble components: stable
More info: https://download.docker.com/linux/ubuntu
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-noble.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-noble.list
Get:1 https://download.docker.com/linux/ubuntu noble InRelease [48.5 kB]
Get:2 https://download.docker.com/linux/ubuntu noble/stable amd64 Packages [52.9 kB]
Hit:3 http://archive.ubuntu.com/ubuntu noble InRelease
Hit:4 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:5 http://archive.ubuntu.com/ubuntu noble-updates InRelease
Hit:6 http://archive.ubuntu.com/ubuntu noble-backports InRelease
Fetched 101 kB in 1s (76.3 kB/s)
Reading package lists... Done
W: https://download.docker.com/linux/ubuntu/dists/noble/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
    

3. Docker本体のインストールとdocker-composeのインストール

sudo apt install -y docker-ce docker-ce-cli containerd.io

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo apt install -y docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  docker-buildx-plugin docker-ce-rootless-extras docker-compose-plugin iptables libip4tc2 libip6tc2
  libnetfilter-conntrack3 libnfnetlink0 libnftables1 libnftnl11 libslirp0 nftables pigz slirp4netns
Suggested packages:
  cgroupfs-mount | cgroup-lite docker-model-plugin firewalld
The following NEW packages will be installed:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin iptables
  libip4tc2 libip6tc2 libnetfilter-conntrack3 libnfnetlink0 libnftables1 libnftnl11 libslirp0 nftables pigz
  slirp4netns
0 upgraded, 17 newly installed, 0 to remove and 93 not upgraded.
Need to get 95.9 MB of archives.
After this operation, 369 MB of additional disk space will be used.
Get:1 https://download.docker.com/linux/ubuntu noble/stable amd64 containerd.io amd64 2.2.3-1~ubuntu.24.04~noble [23.6 MB]
Get:2 http://archive.ubuntu.com/ubuntu noble/main amd64 libip4tc2 amd64 1.8.10-3ubuntu2 [23.3 kB]
Get:3 https://download.docker.com/linux/ubuntu noble/stable amd64 docker-ce-cli amd64 5:29.4.2-2~ubuntu.24.04~noble [17.0 MB]
Get:4 http://archive.ubuntu.com/ubuntu noble/main amd64 libip6tc2 amd64 1.8.10-3ubuntu2 [23.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble/main amd64 libnfnetlink0 amd64 1.0.2-2build1 [14.8 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/main amd64 libnetfilter-conntrack3 amd64 1.0.9-6build1 [45.2 kB]
Get:7 http://archive.ubuntu.com/ubuntu noble/main amd64 libnftnl11 amd64 1.2.6-2build1 [66.0 kB]
Get:8 http://archive.ubuntu.com/ubuntu noble/main amd64 iptables amd64 1.8.10-3ubuntu2 [381 kB]
Get:9 https://download.docker.com/linux/ubuntu noble/stable amd64 docker-ce amd64 5:29.4.2-2~ubuntu.24.04~noble [22.7 MB]
Get:10 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libnftables1 amd64 1.0.9-1ubuntu0.1 [359 kB]
Get:11 https://download.docker.com/linux/ubuntu noble/stable amd64 docker-buildx-plugin amd64 0.33.0-1~ubuntu.24.04~noble [16.9 MB]
Get:12 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 nftables amd64 1.0.9-1ubuntu0.1 [69.8 kB]
Get:13 http://archive.ubuntu.com/ubuntu noble/universe amd64 pigz amd64 2.8-1 [65.6 kB]
Get:14 http://archive.ubuntu.com/ubuntu noble/main amd64 libslirp0 amd64 4.7.0-1ubuntu3 [63.8 kB]
Get:15 http://archive.ubuntu.com/ubuntu noble/universe amd64 slirp4netns amd64 1.2.1-1build2 [34.9 kB]
Get:16 https://download.docker.com/linux/ubuntu noble/stable amd64 docker-ce-rootless-extras amd64 5:29.4.2-2~ubuntu.24.04~noble [6560 kB]
Get:17 https://download.docker.com/linux/ubuntu noble/stable amd64 docker-compose-plugin amd64 5.1.3-1~ubuntu.24.04~noble [8023 kB]
Fetched 95.9 MB in 4s (24.4 MB/s)
Selecting previously unselected package containerd.io.
(Reading database ... 40813 files and directories currently installed.)
Preparing to unpack .../00-containerd.io_2.2.3-1~ubuntu.24.04~noble_amd64.deb ...
Unpacking containerd.io (2.2.3-1~ubuntu.24.04~noble) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../01-docker-ce-cli_5%3a29.4.2-2~ubuntu.24.04~noble_amd64.deb ...
Unpacking docker-ce-cli (5:29.4.2-2~ubuntu.24.04~noble) ...
Selecting previously unselected package libip4tc2:amd64.
Preparing to unpack .../02-libip4tc2_1.8.10-3ubuntu2_amd64.deb ...
Unpacking libip4tc2:amd64 (1.8.10-3ubuntu2) ...
Selecting previously unselected package libip6tc2:amd64.
Preparing to unpack .../03-libip6tc2_1.8.10-3ubuntu2_amd64.deb ...
Unpacking libip6tc2:amd64 (1.8.10-3ubuntu2) ...
Selecting previously unselected package libnfnetlink0:amd64.
Preparing to unpack .../04-libnfnetlink0_1.0.2-2build1_amd64.deb ...
Unpacking libnfnetlink0:amd64 (1.0.2-2build1) ...
Selecting previously unselected package libnetfilter-conntrack3:amd64.
Preparing to unpack .../05-libnetfilter-conntrack3_1.0.9-6build1_amd64.deb ...
Unpacking libnetfilter-conntrack3:amd64 (1.0.9-6build1) ...
Selecting previously unselected package libnftnl11:amd64.
Preparing to unpack .../06-libnftnl11_1.2.6-2build1_amd64.deb ...
Unpacking libnftnl11:amd64 (1.2.6-2build1) ...
Selecting previously unselected package iptables.
Preparing to unpack .../07-iptables_1.8.10-3ubuntu2_amd64.deb ...
Unpacking iptables (1.8.10-3ubuntu2) ...
Selecting previously unselected package libnftables1:amd64.
Preparing to unpack .../08-libnftables1_1.0.9-1ubuntu0.1_amd64.deb ...
Unpacking libnftables1:amd64 (1.0.9-1ubuntu0.1) ...
Selecting previously unselected package nftables.
Preparing to unpack .../09-nftables_1.0.9-1ubuntu0.1_amd64.deb ...
Unpacking nftables (1.0.9-1ubuntu0.1) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../10-docker-ce_5%3a29.4.2-2~ubuntu.24.04~noble_amd64.deb ...
Unpacking docker-ce (5:29.4.2-2~ubuntu.24.04~noble) ...
Selecting previously unselected package pigz.
Preparing to unpack .../11-pigz_2.8-1_amd64.deb ...
Unpacking pigz (2.8-1) ...
Selecting previously unselected package docker-buildx-plugin.
Preparing to unpack .../12-docker-buildx-plugin_0.33.0-1~ubuntu.24.04~noble_amd64.deb ...
Unpacking docker-buildx-plugin (0.33.0-1~ubuntu.24.04~noble) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../13-docker-ce-rootless-extras_5%3a29.4.2-2~ubuntu.24.04~noble_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:29.4.2-2~ubuntu.24.04~noble) ...
Selecting previously unselected package docker-compose-plugin.
Preparing to unpack .../14-docker-compose-plugin_5.1.3-1~ubuntu.24.04~noble_amd64.deb ...
Unpacking docker-compose-plugin (5.1.3-1~ubuntu.24.04~noble) ...
Selecting previously unselected package libslirp0:amd64.
Preparing to unpack .../15-libslirp0_4.7.0-1ubuntu3_amd64.deb ...
Unpacking libslirp0:amd64 (4.7.0-1ubuntu3) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../16-slirp4netns_1.2.1-1build2_amd64.deb ...
Unpacking slirp4netns (1.2.1-1build2) ...
Setting up libip4tc2:amd64 (1.8.10-3ubuntu2) ...
Setting up libip6tc2:amd64 (1.8.10-3ubuntu2) ...
Setting up libnftnl11:amd64 (1.2.6-2build1) ...
Setting up docker-buildx-plugin (0.33.0-1~ubuntu.24.04~noble) ...
Setting up containerd.io (2.2.3-1~ubuntu.24.04~noble) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /usr/lib/systemd/system/containerd.service.
Setting up docker-compose-plugin (5.1.3-1~ubuntu.24.04~noble) ...
Setting up docker-ce-cli (5:29.4.2-2~ubuntu.24.04~noble) ...
Setting up libslirp0:amd64 (4.7.0-1ubuntu3) ...
Setting up pigz (2.8-1) ...
Setting up libnfnetlink0:amd64 (1.0.2-2build1) ...
Setting up docker-ce-rootless-extras (5:29.4.2-2~ubuntu.24.04~noble) ...
Setting up libnftables1:amd64 (1.0.9-1ubuntu0.1) ...
Setting up nftables (1.0.9-1ubuntu0.1) ...
Setting up slirp4netns (1.2.1-1build2) ...
Setting up libnetfilter-conntrack3:amd64 (1.0.9-6build1) ...
Setting up iptables (1.8.10-3ubuntu2) ...
update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in auto mode
update-alternatives: using /usr/sbin/ip6tables-legacy to provide /usr/sbin/ip6tables (ip6tables) in auto mode
update-alternatives: using /usr/sbin/iptables-nft to provide /usr/sbin/iptables (iptables) in auto mode
update-alternatives: using /usr/sbin/ip6tables-nft to provide /usr/sbin/ip6tables (ip6tables) in auto mode
update-alternatives: using /usr/sbin/arptables-nft to provide /usr/sbin/arptables (arptables) in auto mode
update-alternatives: using /usr/sbin/ebtables-nft to provide /usr/sbin/ebtables (ebtables) in auto mode
Setting up docker-ce (5:29.4.2-2~ubuntu.24.04~noble) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.7) ...
    
sudo apt install docker-compose

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo apt install docker-compose
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  python3-compose python3-docker python3-dockerpty python3-docopt python3-dotenv python3-packaging python3-texttable
  python3-websocket
Recommended packages:
  docker.io
The following NEW packages will be installed:
  docker-compose python3-compose python3-docker python3-dockerpty python3-docopt python3-dotenv python3-packaging
  python3-texttable python3-websocket
0 upgraded, 9 newly installed, 0 to remove and 93 not upgraded.
Need to get 338 kB of archives.
After this operation, 1779 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-websocket all 1.7.0-1 [38.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble/main amd64 python3-packaging all 24.0-1 [41.1 kB]
Get:3 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 python3-docker all 5.0.3-1ubuntu1.1 [89.1 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-dockerpty all 0.4.1-5 [11.4 kB]
Get:5 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-docopt all 0.6.2-6 [26.1 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-dotenv all 1.0.1-1 [22.3 kB]
Get:7 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-texttable all 1.6.7-1 [11.0 kB]
Get:8 http://archive.ubuntu.com/ubuntu noble/universe amd64 python3-compose all 1.29.2-6ubuntu1 [84.6 kB]
Get:9 http://archive.ubuntu.com/ubuntu noble/universe amd64 docker-compose all 1.29.2-6ubuntu1 [14.0 kB]
Fetched 338 kB in 2s (207 kB/s)
Selecting previously unselected package python3-websocket.
(Reading database ... 41336 files and directories currently installed.)
Preparing to unpack .../0-python3-websocket_1.7.0-1_all.deb ...
Unpacking python3-websocket (1.7.0-1) ...
Selecting previously unselected package python3-packaging.
Preparing to unpack .../1-python3-packaging_24.0-1_all.deb ...
Unpacking python3-packaging (24.0-1) ...
Selecting previously unselected package python3-docker.
Preparing to unpack .../2-python3-docker_5.0.3-1ubuntu1.1_all.deb ...
Unpacking python3-docker (5.0.3-1ubuntu1.1) ...
Selecting previously unselected package python3-dockerpty.
Preparing to unpack .../3-python3-dockerpty_0.4.1-5_all.deb ...
Unpacking python3-dockerpty (0.4.1-5) ...
Selecting previously unselected package python3-docopt.
Preparing to unpack .../4-python3-docopt_0.6.2-6_all.deb ...
Unpacking python3-docopt (0.6.2-6) ...
Selecting previously unselected package python3-dotenv.
Preparing to unpack .../5-python3-dotenv_1.0.1-1_all.deb ...
Unpacking python3-dotenv (1.0.1-1) ...
Selecting previously unselected package python3-texttable.
Preparing to unpack .../6-python3-texttable_1.6.7-1_all.deb ...
Unpacking python3-texttable (1.6.7-1) ...
Selecting previously unselected package python3-compose.
Preparing to unpack .../7-python3-compose_1.29.2-6ubuntu1_all.deb ...
Unpacking python3-compose (1.29.2-6ubuntu1) ...
Selecting previously unselected package docker-compose.
Preparing to unpack .../8-docker-compose_1.29.2-6ubuntu1_all.deb ...
Unpacking docker-compose (1.29.2-6ubuntu1) ...
Setting up python3-dotenv (1.0.1-1) ...
Setting up python3-texttable (1.6.7-1) ...
Setting up python3-docopt (0.6.2-6) ...
Setting up python3-packaging (24.0-1) ...
Setting up python3-websocket (1.7.0-1) ...
Setting up python3-dockerpty (0.4.1-5) ...
Setting up python3-docker (5.0.3-1ubuntu1.1) ...
Setting up python3-compose (1.29.2-6ubuntu1) ...
Setting up docker-compose (1.29.2-6ubuntu1) ...
Processing triggers for man-db (2.12.0-4build2) ...      
    

4. Dockerサービス開始

sudo service docker start

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo service docker start
    

5. 動作確認

hello-worldイメージを実行してみる。
sudo docker run hello-world

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete
d5e71e642bf5: Download complete
Digest: sha256:f9078146db2e05e794366b1bfe584a14ea6317f44027d10ef7dad65279026885
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
    

6. その他

sudoを付けずにdockerコマンドを実行したい場合は以下記事を参照。
Docker:一般ユーザーでDockerコマンドを使う

7. 片付け

先ほど作成したhello-worldコンテナはいらないので削除
sudo docker rm (コンテナID)
sudo docker rmi hello-world

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo docker ps -a
[sudo] password for masauser:
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
9a5a5fa1c7f0   hello-world   "/hello"   21 minutes ago   Exited (0) 21 minutes ago             eager_robinson
masauser@xxx:/mnt/c/Users/xxx$ sudo docker rm 9a5a5fa1c7f0
9a5a5fa1c7f0
masauser@xxx:/mnt/c/Users/xxx$ sudo docker rmi hello-world
Untagged: hello-world:latest
Deleted: sha256:f9078146db2e05e794366b1bfe584a14ea6317f44027d10ef7dad65279026885
    


WSL, Docker

Docker:一般ユーザーでDockerコマンドを使う

更新:2026.05.01(金) 22:11
公開:2026.03.12 15:19

WSL上のDocker環境でのDockerコマンドを一般ユーザーで使えるようにする

WSLでDockerコマンドを使う際、毎回 "sudo docker…" とするのが面倒なので、
一般ユーザーでもDockerコマンドが実行できるように設定する。

dockerグループに操作者ユーザーを追加すればよい。
sudo usermod -aG docker $USER

実行結果:
masauser@xxx:/mnt/c/Users/xxx$ sudo usermod -aG docker $USER    
  

いちどログインし直さないとダメみたいなので "logout"コマンド実行してからUbuntuコマンドを起動すればOK


Docker, WSL

その他の記事