0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerのコンテナ内でlocalhostにリクエストするとエラーになる

Posted at

事象

  • Dockerのコンテナ内で、curl http://localhost:8010/を実行するとcurl: (7) Failed to connect ~ が返ってきてしまう。
  • Dockerのコンテナ外で、curl http://localhost:8010/を実行すると正常にレスポンスが返ってくる。

結論

ホスト側とコンテナ側でポートが異なる設定にしていたため。

ホスト側のポートとコンテナ内部のポートのマッピングを確認する

例えば、docker-compose.yml が下記のようになっていた場合、

services:
  php81:
    image: php:8.1-apache
    ports:
      - "8010:80"

ホスト側の 8010 がコンテナの 80 にマッピング される。
そのため、コンテナの中からは 8010 ではなく 80 にアクセスする必要がある。

検証

  • Dockerのコンテナ内で、curl http://localhost:8010/を実行するとcurl: (7) Failed to connect ~ が返ってくる
  • curl http://localhost:80/を実行すると、正常にレスポンスが返ってきた。

まとめ

  • ホスト側のポートとコンテナ内部のポートのマッピングの設定により、ポートが異なることがある。
  • コンテナ内部でも同じポート番号でアクセスしたい場合は、docker-compose.yml で ports: - "8010:8010"に変更する。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?