Dockerコンテナからlocalhostにアクセスできない

Dockerコンテナからlocalhostにアクセスできない

私はこのチュートリアルをステップごとに実行しました:https://www.youtube.com/watch?v=NZGu-9KQVsE

  • Dockerfile を作成しました:

ubuntu:jammyより

RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
  • Dockerイメージを構築しました:
docker build -t mycurl .
  • 私は画像を実行した
docker run --add-host host.docker.internal:host-gateway --rm -ti mycurl bash
  • 私はローカルサーバーを立ち上げました
python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
  • しかし、コンテナからアクセスしようとすると、次のタイムアウトが発生します。
root@da4d8b1f4ae4:/# curl host.docker.internal:8000
curl: (28) Failed to connect to host.docker.internal port 8000 after 133451 ms: Connection timed out

私の /etc/hosts は次のとおりです:

root@da4d8b1f4ae4:/# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1  host.docker.internal
172.17.0.2  da4d8b1f4ae4

理由が分かりますか?私はUbuntu 22.04.4 LTSを使用しています

答え1

これを実行して問題を解決しました:

sudo ufw allow 8000

関連情報