502 Bad Gateway - nginx/1.14.0 (Ubuntu): ローカルにイメージが見つからない + デーモンからのエラー応答

最近、Dell サーバーを別の場所に移設しました。メインの OS/VM 管理ツールは Proxmox VE 5.3 です。いくつかの他の VM のリバースプロキシとして機能する Nginx VM を持っており、その中に Discourse VM も含まれています。

新しいルーターを構成した後、いくつかの VM がインターネットに接続できるようになりました(SSL 証明書の更新さえ不要でした)。しかし、Discourse にアクセスしようとすると、ブラウザで 502 Bad Gateway - nginx/1.14.0 (Ubuntu) というエラーが表示されます。

以前もこのエラーを受け取ったことがありますが、通常は以下の方法のいずれかで解決していました。

Discourse VM の再起動

ブラウザでは依然として 502 Bad Gateway - nginx/1.14.0 (Ubuntu) が表示されます。

ブラウザのクッキーをクリアして別のブラウザを試す

もし私の側で何か問題があるなら… いいえ。さまざまなブラウザでも同じ 502 Bad Gateway - nginx/1.14.0 (Ubuntu) エラーが発生します。

Discourse のクリーンアップとディスク容量の確認

当初、クリーンアップ時に約 4 GB のデータが削除されました。非常に驚きました。もしかすると、それが問題の原因だったのでしょうか?とにかく、今 Discourse をクリーンアップしようとすると:

> root@forum:/var/discourse# ./launcher cleanup
> WARNING! This will remove all stopped containers.
> Are you sure you want to continue? [y/N] y
> Total reclaimed space: 0B
> WARNING! This will remove all images without at least one container associated to them.
> Are you sure you want to continue? [y/N] y
> Total reclaimed space: 0B

また、ディスク容量が不足しているわけではないことを繰り返しておきます:

Git Pull

最新の状態です。

> root@forum:/var/discourse# git pull
> Already up to date.

Discourse の再起動

ここで、私が受け取っている 2 つのエラーを発見しました。

> root@forum:/var/discourse# ./launcher restart app
> 
> WARNING: We are about to start downloading the Discourse base image
> This process may take anywhere between a few minutes to an hour, depending on your network speed
> 
> Please be patient
> 
> Unable to find image 'discourse/base:2.0.20191013-2320' locally
> /usr/bin/docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
> See '/usr/bin/docker run --help'.
> Your Docker installation is not working correctly
> 
> See: `https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam`

提案されたリンクを確認しました。Git PullDiscourse の再構築(後で確認できます)を推奨しています。また、Docker の「Hello World」コマンドを実行することも提案されています。動作しますが(いくつかの問題があるように見えます):

> root@forum:/var/discourse# docker run -it --rm hello-world
> Unable to find image 'hello-world:latest' locally
> latest: Pulling from library/hello-world
> 1b930d010525: Pull complete
> Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
> 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/
> 
> failed to resize tty, using default size

次に、Ubuntu Bash を試しましたが、エラーが発生しました。

> root@forum:/var/discourse# docker run -it ubuntu bash
> Unable to find image 'ubuntu:latest' locally
> docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/ubuntu/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fubuntu%3Apull&service=registry.docker.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
> See 'docker run --help'.

他の既知の方法に進みましょう。

Discourse の停止と開始

再起動と同じエラーが表示されます。

> root@forum:/var/discourse# ./launcher stop app
> 
> WARNING: We are about to start downloading the Discourse base image
> This process may take anywhere between a few minutes to an hour, depending on your network speed
> 
> Please be patient
> 
> Unable to find image 'discourse/base:2.0.20191013-2320' locally
> /usr/bin/docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
> See '/usr/bin/docker run --help'.
> Your Docker installation is not working correctly
> 
> See: `https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam`
> 
> root@forum:/var/discourse# ./launcher start app
> 
> WARNING: We are about to start downloading the Discourse base image
> This process may take anywhere between a few minutes to an hour, depending on your network speed
> 
> Please be patient
> 
> Unable to find image 'discourse/base:2.0.20191013-2320' locally
> /usr/bin/docker: Error response from daemon: Get https://registry-1.docker.io/v2/discourse/base/manifests/2.0.20191013-2320: Get https://auth.docker.io/token?scope=repository%3Adiscourse%2Fbase%3Apull&service=registry.docker.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
> See '/usr/bin/docker run --help'.
> Your Docker installation is not working correctly
> 
> See: `https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam`

Discourse の再構築

同じ 2 つのエラーが 再び 発生します。

> root@forum:/var/discourse# ./launcher rebuild app
> 
> WARNING: We are about to start downloading the Discourse base image
> This process may take anywhere between a few minutes to an hour, depending on your network speed
> 
> Please be patient
> 
> Unable to find image 'discourse/base:2.0.20191013-2320' locally
> /usr/bin/docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
> See '/usr/bin/docker run --help'.
> Your Docker installation is not working correctly
> 
> See: https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam

以前のバックアップからの復元

サーバーを移設し、クリーンアップ(約 4 GB のファイルが削除されたことについて前述)を行う前のスナップショットがあります。上記のすべてを再度試しましたが、同じ結果になりました(クリーンアップを除く)。また、ブラウザでは 502 Bad Gateway - nginx/1.14.0 (Ubuntu) が表示されます。したがって、これはクリーンアップとは無関係かもしれません。

Discourse を使用している間、私はこれら 2 つのエラーに遭遇したことがありません。これらのエラーを解決し、Discourse をブラウザで動作させるためにどのような対策を取ればよいでしょうか。

サーバーが Docker Hub に接続できず、大きなイメージのダウンロードに失敗しました。小さな hello world イメージのみが正常にダウンロードされました。

では、これをどう解決すればよいのでしょうか?Dockerの経験があまりないため、どこから手をつければよいかわかりません。

私が知っているDocker関連のことは、Discourseのインストールに含まれるコマンド(もちろん、Discourseをサードパーティのクラウドではなく、自分のサーバー上のVMにインストールしているため、一部変更を加えています)だけです。

実行中の Docker バージョンを教えてください(docker info コマンドで確認できます)。また、https://registry-1.docker.io/v2/ への接続を確立する際にネットワークスタックに問題がないか確認してください。

ここに Docker の情報があります:

root@forum:/var/discourse# docker info
コンテナ: 1
実行中: 1
一時停止: 0
停止: 0
イメージ: 3
サーバーバージョン: 18.09.5
ストレージドライバ: overlay2
バックアップファイルシステム: extfs
d_type サポート: true
ネイティブオーバーレイ差分: true
ログドライバ: json-file
Cgroup ドライバ: cgroupfs
プラグイン:
ボリューム: local
ネットワーク: bridge host macvlan null overlay
ログ: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: 非アクティブ
ランタイム: runc
デフォルトランタイム: runc
初期化バイナリ: docker-init
containerd バージョン: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc バージョン: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
初期化バージョン: fec3683
セキュリティオプション:
apparmor
seccomp
プロファイル: default
カーネルバージョン: 4.15.0-69-generic
オペレーティングシステム: Ubuntu 18.04.2 LTS
OSType: linux
アーキテクチャ: x86_64
CPU: 2
総メモリ: 6.805GiB
名前: forum
ID: 2RRX:ZQIT:R5AK:WNPR:VJ6Z:2EBY:PFOL:W5RD:GL3X:RUQM:YLJ4:2L2X
Docker ルートディレクトリ: /var/lib/docker
デバッグモード (クライアント): false
デバッグモード (サーバー): false
レジストリ: https://index.docker.io/v1/
ラベル:
実験的機能: false
安全でないレジストリ:
127.0.0.0/8
リバースストア有効化: false
製品ライセンス: Community Engine

警告: スワップ制限サポートなし

ご質問について:

これをどのように行えばよいですか?

@Falco 問題のさらなるトラブルシューティングを試みていますが、今はさらに混乱しています。良いニュースと悪いニュースがあります:

良いニュース

いくつかの Discourse リビルド./launcher rebuild app)を実行した後、Unable to find image または Error response from daemon のエラーは不再現します!Discourse を開始または再起動しても、エラーは表示されません:

> root@forum:/var/discourse# ./launcher start app
> 
> .+ /usr/bin/docker run --shm-size=512m -d --restart=always -e LANG=en_US.UTF-8 -e RAILS_ENV=production -e UNICORN_WORKERS=4 -e UNICORN_SIDEKIQS=1 -e RUBY_GLOBAL_METHOD_CACHE_SIZE=131072 -e RUBY_GC_HEAP_GROWTH_MAX_SLOTS=40000 -e RUBY_GC_HEAP_INIT_SLOTS=400000 -e RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=1.5 -e DISCOURSE_DB_SOCKET=/var/run/postgresql -e DISCOURSE_DB_HOST= -e DISCOURSE_DB_PORT= -e DISCOURSE_HOSTNAME=discourse.example.com -e DISCOURSE_DEVELOPER_EMAILS=admin@example.com,postmaster@example.com -e DISCOURSE_SMTP_ADDRESS=smtp.sparkpostmail.com -e DISCOURSE_SMTP_PORT=587 -e DISCOURSE_SMTP_USER_NAME=SMTP_Injection -e DISCOURSE_SMTP_PASSWORD=<HIDING-FOR-PRICACY> -e LETSENCRYPT_ACCOUNT_EMAIL=admin@example.com -h forum-app -e DOCKER_HOST_IP=100.17.0.1 --name app -t -p 8080:80 -p 8443:443 -p 2222:22 -v /var/discourse/shared/standalone:/shared -v /var/discourse/shared/standalone/log/var-log:/var/log --mac-address 02:96:f3:e6:e7:14 local_discourse/app /sbin/boot
> cebe89493bc79dab2c1716599629adfe3dc571c8659367e6ffa0d39b0e6d47af
> root@forum:/var/discourse# ./launcher restart app
> .+ /usr/bin/docker stop -t 10 app
> app
> 
> starting up existing container
> .+ /usr/bin/docker start app
> app

Docker も実行中です:
> root@forum:/var/discourse# systemctl status docker.service
> ● docker.service - Docker Application Container Engine
>    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
>    Active: active (running) since Thu 2019-11-14 03:00:54 UTC; 17h ago
>      Docs: https://docs.docker.com
>  Main PID: 18721 (dockerd)
>     Tasks: 31
>    CGroup: /system.slice/docker.service
>            ├─ 1375 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8443 -container-ip 172.17.0.2 -container-port 443
>            ├─ 1387 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.2 -container-port 80
>            ├─ 1399 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 2222 -container-ip 172.17.0.2 -container-port 22
>            └─18721 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
> 
> Nov 14 20:13:26 forum dockerd[18721]: time="2019-11-14T20:13:26.430856242Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:28 forum dockerd[18721]: time="2019-11-14T20:13:28.597999379Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:30 forum dockerd[18721]: time="2019-11-14T20:13:30.862158413Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:32 forum dockerd[18721]: time="2019-11-14T20:13:32.978285148Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:35 forum dockerd[18721]: time="2019-11-14T20:13:35.105130149Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:37 forum dockerd[18721]: time="2019-11-14T20:13:37.151466214Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:13:39 forum dockerd[18721]: time="2019-11-14T20:13:39.024948159Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:14:05 forum dockerd[18721]: time="2019-11-14T20:14:05.179759938Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
> Nov 14 20:14:16 forum dockerd[18721]: time="2019-11-14T20:14:16.078334393Z" level=info msg="Container cebe89493bc79dab2c1716599629adfe3dc571c8659367e6ffa0d39b0e6d47af failed to exit within
> Nov 14 20:14:16 forum dockerd[18721]: time="2019-11-14T20:14:16.281731176Z" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"

悪いニュース

それでも、サイトを開こうとすると、さまざまなブラウザで 502 Bad Gateway - nginx/1.14.0 (Ubuntu) のエラーが表示されます。念のためキャッシュをクリアし、複数のブラウザを試しましたが、解決しません。

一つ気づいたのは、Discourse のクリーンアップを実行すると、いくつかのコンテナが削除されることです:

> root@forum:/var/discourse# ./launcher cleanup
> WARNING! This will remove all stopped containers.
> Are you sure you want to continue? [y/N] y
> Total reclaimed space: 0B
> WARNING! This will remove all images without at least one container associated to them.
> Are you sure you want to continue? [y/N] y
> Deleted Images:
> untagged: hello-world:latest
> untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
> deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
> deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
> untagged: discourse/base:2.0.20191013-2320
> untagged: discourse/base@sha256:77e010342aa5111c8c3b81d80de7d4bdb229793d595bbe373992cdb8f86ef41f
> deleted: sha256:53b44681b65ee5e9a9cadc6bd34c6aa6f6bcbbbe6270e61669c50bcd655c6898
> deleted: sha256:939a3ac6d5627270ae02a9f9ea05c580589cec0afa019b7f296fdd43157dd3a0
> 
> Total reclaimed space: 452.2MB

今では、開始/再起動を試みると、エラーが再び発生します!Unable to find image または Error response from daemon のエラーメッセージが表示されなくなった時点までロールバックしました。明らかにコンテナは再構築されましたが、イメージコンテナをクリアすると問題が発生します。

Docker が破損している場合(つまり、docker run ubuntu が失敗する場合)、Discourse は確実に動作しません。

Docker 固有のサポートについては、https://forums.docker.com/ に投稿してみてください。

@Falco はい、以前と同じように、そのコマンドを実行すると「Unable to find image locally」と「Error response from daemon」というエラーが発生しています。

そこで、あなたの提案に従って、Docker フォーラムでトピックを作成しました。残念ながら、まだ返信はありません。

さて、さらにいくつかのニュースがあります:

良い知らせ

もう /usr/bin/docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers). というエラーは表示されなくなりました!

docker run ubuntu でもエラーは報告されなくなりました。@Falco さん、表示されるべき何かの出力はありますか?

root@forum:/var/discourse# docker run ubuntu
root@forum:/var/discourse#

では、このエラーを解消するために私が何をしたのかというと、この VM で使用されているネームサーバー(DNS)を Google Public DNS サービスに置き換えました:
echo "nameserver 8.8.8.8" > /etc/resolv.conf

ただし、VM を再起動するたびに上記のコマンドを実行する必要があります(つまり、一時的な解決策のようです)。そうしないと、./launcher コマンドを実行しようとした際に 2 つのエラーが発生します。その後、エラーは 1 つ(Unable to find image 'discourse/base:2.0.20191013-2320' locally)に減りますが、コマンドは正常に完了しているようです:

root@forum:/var/discourse# ./launcher start app

WARNING: We are about to start downloading the Discourse base image
This process may take anywhere between a few minutes to an hour, depending on your network speed

Please be patient

Unable to find image 'discourse/base:2.0.20191013-2320' locally
2.0.20191013-2320: Pulling from discourse/base
Digest: sha256:77e010342aa5111c8c3b81d80de7d4bdb229793d595bbe373992cdb8f86ef41f
Status: Downloaded newer image for discourse/base:2.0.20191013-2320

starting up existing container
+ /usr/bin/docker start app
app

また、./launcher rebuild app もエラーなしで実行できるようになりました。以下は、そのコマンドの出力の最後の部分です(URL と MAC アドレスは伏せています):

+ /usr/bin/docker run --shm-size=512m -d --restart=always -e LANG=en_US.UTF-8 -e RAILS_ENV=production -e UNICORN_WORKERS=4 -e UNICORN_SIDEKIQS=1 -e RUBY_GLOBAL_METHOD_CACHE_SIZE=131072 -e RUBY_GC_HEAP_GROWTH_MAX_SLOTS=40000 -e RUBY_GC_HEAP_INIT_SLOTS=400000 -e RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=1.5 -e DISCOURSE_DB_SOCKET=/var/run/postgresql -e DISCOURSE_DB_HOST= -e DISCOURSE_DB_PORT= -e DISCOURSE_HOSTNAME=discourse.domain.com -e DISCOURSE_DEVELOPER_EMAILS=admin@domain.com,postmaster@domain.com -e DISCOURSE_SMTP_ADDRESS=smtp.sparkpostmail.com -e DISCOURSE_SMTP_PORT=587 -e DISCOURSE_SMTP_USER_NAME=SMTP_Injection -e DISCOURSE_SMTP_PASSWORD=0d431cd177ce3d35833aa823d498eb57c7c4e99c -e LETSENCRYPT_ACCOUNT_EMAIL=admin@domain.com -h forum-app -e DOCKER_HOST_IP=172.17.0.1 --name app -t -p 8080:80 -p 8443:443 -p 2222:22 -v /var/discourse/shared/standalone:/shared -v /var/discourse/shared/standalone/log/var-log:/var/log --mac-address 00:00:00:00:00:01 local_discourse/app /sbin/boot
abb788d4a6fd301d88f129189a07a19c4a6bfc8554d43c555d3e3cd126374736

悪い知らせ

ページにアクセスしようとした際、まだ 502 Bad Gateway - nginx/1.14.0 (Ubuntu) エラーが表示されます。

他に考えられる問題はありますか?ご提案があればお願いします。

問題が解決しました!ブラウザで Discourse フォーラムが表示されるようになりました!

より正確に言うと、Nginx フォーラムの Francis Day さんが私の問題を解決してくれました! 結局、問題は Nginx にありました。以下に私が行った手順を示します:

  1. Nginx VM にログインしました。
  2. Discourse の設定ファイルを VIM で編集しました:vim /etc/nginx/sites-available/discourse.conf
  3. 私の Discourse 設定ファイルは以下のようになっていました(もちろん、通常使用するドメイン名は使用していません。ここで 192.168.0.101 = Nginx VM192.168.0.104 = Discourse VM です)。
  4. 変更したのは 1 箇所だけです:proxy_pass http://discourse.domainame.com:8080/;proxy_pass 192.168.0.104:8080/; に変更しました。つまり、proxy_pass をホスト名ではなく、Discourse VM のローカル IP に設定しました。
  5. 設定ファイルを保存し、Nginx をリロード(systemctl reload nginx.service)して再起動(systemctl restart nginx.service)しました。
  6. できました!Discourse の URL を更新すると、表示されました!Discourse の再起動や再構築は不要でした!

Nginx を通じた Discourse のリバースプロキシ設定は非常に厄介な場合があるため、この情報が誰かの役に立てば幸いです。