このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

よくあるエラーと解決策②

よくあるエラーと解決策②について解説していきます。

ポート番号の重複

 Apacheコンテナと、nginxコンテナを作成します。使用するポート番号を重複させます。ポート番号が重複した場合、エラーメッセージから、エラーを修正する解決策について解説していきます。

不要なDockerオブジェクトの削除

まず、不要なDockerオブジェクトをすべて削除しておきます。

以下のコマンドを実行します。

・「docker system prune -a」コマンドを実行します。

Are you sure you want to continue? [y/N] の質問には「y」と入力します。

PS C:\Users\joeac> docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
(略)

・「docker system prune -a」コマンドで削除されなかったDockerオブジェクトは、個別に削除しておきます。

環境によって削除されずに残っているDockerオブジェクトは異なります。

ディレクトリの移動と作成

ディレクトリを移動します。

・「cd desktop/docker」コマンドを実行します。

PS C:\Users\joeac> cd desktop/docker
PS C:\Users\joeac\Desktop\docker>

 「compose.yaml」ファイルを保存する「error2」ディレクトリを作成して、作成したディレクトリに移動します。次のコマンドを実行します。

  • 「mkdir error2」コマンド
  • 「cd error2」コマンド
PS C:\Users\joeac\Desktop\docker> mkdir error2

    Directory: C:\Users\joeac\Desktop\docker

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024/04/29    17:41                error2

PS C:\Users\joeac\Desktop\docker> cd error2
PS C:\Users\joeac\Desktop\docker\error2>

Apacheコンテナとnginxコンテナの作成

Apacheコンテナとnginxコンテナを作成します。

・compose.yamlの作成

VSCodeで「compose.yaml」ファイルを作成します。

・「code compose.yaml」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\error2> code compose.yaml

VSCodeが起動します。

・「compose.yaml」ファイルの編集

・「compose.yaml」ファイルを以下のように編集して保存します。

10行目にnginxコンテナのポート番号とApacheコンテナとのポート番号に重複の間違いがあります。

services:
  apache:
    image: httpd:latest
    ports:
      - "80:80"

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
コンテナの作成と実行

・「docker compose up -d」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\error2> docker compose up -d
[+] Running 14/14
 ✔ nginx Pulled                                                                                                    6.4s
   ✔ b0a0cf830b12 Pull complete                                                                                    1.6s
   ✔ 8ddb1e6cdf34 Pull complete                                                                                    1.9s
   ✔ 5252b206aac2 Pull complete                                                                                    0.7s
   ✔ 988b92d96970 Pull complete                                                                                    1.4s
   ✔ 7102627a7a6e Pull complete                                                                                    2.1s
   ✔ 93295add984d Pull complete                                                                                    2.3s
   ✔ ebde0aa1d1aa Pull complete                                                                                    2.5s
 ✔ apache Pulled                                                                                                   7.5s
   ✔ 851c52adaa9b Pull complete                                                                                    2.6s
   ✔ 4f4fb700ef54 Pull complete                                                                                    2.9s
   ✔ 39d9f60535a6 Pull complete                                                                                    3.1s
   ✔ 943a2b3cf551 Pull complete                                                                                    3.7s
   ✔ ea83e81966d6 Pull complete                                                                                    3.6s
[+] Running 3/3
 ✔ Network error2_default     Created                                                                              0.0s
 ✔ Container error2-nginx-1   Created                                                                              0.1s
 ✔ Container error2-apache-1  Started                                                                              0.1s
Error response from daemon: driver failed programming external connectivity on endpoint error2-nginx-1 (47abbc045cd35a6dff1fbd062113cd6814e213766768118bb869a6af389a4aa3): Bind for 0.0.0.0:80 failed: port is already allocated

nginxコンテナが「Sterted」になっていません。

以下のエラーメッセージが表示されます。

Error response from daemon: driver failed programming external connectivity on endpoint error2-nginx-1 (47abbc045cd35a6dff1fbd062113cd6814e213766768118bb869a6af389a4aa3): Bind for 0.0.0.0:80 failed: port is already allocated

デーモンからのエラー応答: ドライバーはエンドポイント error2-nginx-1 での外部接続のプログラミングに失敗しました (47abbc045cd35a6dff1fbd062113cd6814e213766768118bb869a6af389a4aa3): 0.0.0.0:80 のバインドに失敗しました: ポートはすでに割り当てられています

ポート番号が重複していることが分かります。

実行中のコンテナの確認

・「docker container ls」コマンドを実行します。

このコマンドは、実況中のコンテナを一覧表示します。

PS C:\Users\joeac\Desktop\docker\error2> docker container ls
CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS         PORTS                NAMES
19ba7baa225a   httpd:latest   "httpd-foreground"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp   error2-apache-1

Apacheコンテナは実行中ですが、nginxコンテナがありません。

実行中のコンテナの停止

実行中のコンテナを停止させます。

・「docker compose stop」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\error2> docker compose stop
[+] Stopping 2/2
 ✔ Container error2-apache-1  Stopped                                                                              1.3s
 ✔ Container error2-nginx-1   Stopped                                                                              0.0s
Dockerオブジェクトの削除

Dockerオブジェクトをすべて削除しておきます。

・「docker system prune -a」コマンドを実行します。

Are you sure you want to continue? [y/N] の質問には「y」と入力します。

PS C:\Users\joeac> docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
(略)

エラーを修正してコンテナを起動させる

エラーを修正してnginxコンテナを起動させます。

・以下のように「compose.yaml」ファイルを修正します。

10行目を「”8080:80″」にします。

services:
  apache:
    image: httpd:latest
    ports:
      - "80:80"

services:
  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
コンテナの作成と実行

・「docker compose up -d」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\error2> docker compose up -d
[+] Running 14/14
 ✔ apache Pulled                                                                                                   6.0s
   ✔ b0a0cf830b12 Pull complete                                                                                    1.3s
   ✔ 851c52adaa9b Pull complete                                                                                    0.7s
   ✔ 4f4fb700ef54 Pull complete                                                                                    0.7s
   ✔ 39d9f60535a6 Pull complete                                                                                    1.5s
   ✔ 943a2b3cf551 Pull complete                                                                                    2.0s
   ✔ ea83e81966d6 Pull complete                                                                                    2.2s
 ✔ nginx Pulled                                                                                                    7.0s
   ✔ 8ddb1e6cdf34 Pull complete                                                                                    2.9s
   ✔ 5252b206aac2 Pull complete                                                                                    2.5s
   ✔ 988b92d96970 Pull complete                                                                                    2.7s
   ✔ 7102627a7a6e Pull complete                                                                                    3.6s
   ✔ 93295add984d Pull complete                                                                                    3.4s
   ✔ ebde0aa1d1aa Pull complete                                                                                    3.6s
[+] Running 3/3
 ✔ Network error2_default     Created                                                                              0.0s
 ✔ Container error2-nginx-1   Started                                                                              0.1s
 ✔ Container error2-apache-1  Started

今度は、Apacheコンテナとnginxコンテナ共に「Started」になっています。

コンテナへの接続

・Apacheコンテナへの接続

・WebブラウザのURLに「localhost」と入力します。

ApacheコンテナのTOPページが表示されます。

・nginxコンテナへの接続

・WebブラウザのURLに「localhost:8080」と入力します。

nginxコンテナのTOPページが表示されます。

関連コンテンツ