3
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?

More than 5 years have passed since last update.

Dockerコンテナに127.0.0.1でアクセスできない場合の解消方法

Last updated at Posted at 2020-02-29

TL;DR

Dockerコンテナで起動したアプリケーションに対してブラウザから以下のアドレスでアクセスする。
http://127.0.0.1:8000

Issue

以下のように ERR_EMPTY_RESPONSE が返却されてしまい、
Dockerコンテナで起動しているアプリケーションにアクセスできない。
スクリーンショット 2020-02-29 09.50.52.png
ホストマシンからcurlを送信しても以下の通り、うまく繋がらない。

taichikanaya@kanayatBookpuro attakait_hp % curl http://127.0.0.1:8000
curl: (52) Empty reply from server
taichikanaya@kanayatBookpuro attakait_hp % 

Cause

Dockerコンテナ内でのアプリケーションを127.0.0.1で起動しているから。
(なので、Dockerコンテナ内からアプリケーションへのアクセスは可能だが、
 ホストマシンからDockerコンテナ内へのアクセスはできないといった現象が発生する)

Solution

Dockerコンテナ内でのアプリケーションを 0.0.0.0で起動する。
127.0.0.1だとDockerコンテナ内からのみアクセス可能になってしまうので、
 ホストマシンからアクセスできるように 0.0.0.0 へ変更する必要がある)

gunicornを起動する場合は以下のようにしてあげることでアクセスできるようになりました。

gunicorn attakait_hp.wsgi --bind 0.0.0.0:8000 --log-file -

以下は私が使用していた簡単なDockerファイル群です。

Dockerfile
FROM python:3.7
ADD . /var/www/html
WORKDIR /var/www/html
RUN pip install -r ./requirements.txt
docker-compose.yml
version: '3'
services:
  attakait_app:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: python:3.7
    volumes:
      - '.:/var/www/html'
    container_name: hp
    tty: true
    working_dir: '/var/www/html'
    ports:
      - 8000:8000
    command:
      gunicorn attakait_hp.wsgi --bind 0.0.0.0:8000 --log-file -

では、アクセス確認。
スクリーンショット 2020-02-29 10.08.25.png
以上、終わり。

3
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
3
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?