neowenshun
neowenshun

Reputation: 960

Docker can't connect to localhost

Im facing a problem of not being able to access my docker container from my browser at localhost:8000. There is no error message. Here is what the browser is saying: This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE

This is my docker-compose file:

version: "3.7"

services:
  postgres:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=postgres

  fastapi:
    build: ./backend
    ports:
      - "8000:8000"
    volumes:
      - ./backend/:/usr/src/backend/
    depends_on: 
      - postgres

volumes:  
  postgres_data:

and this is my dockerfile:

# pull official base image
FROM python:3.8.3-slim-buster
# set work directory
WORKDIR /usr/src/backend

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# copy project
COPY . .

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
#Installing dependencies, remove those that are not needed after the installation
RUN pip install -r requirements.txt

CMD uvicorn main:app --reload

Here is my CLI:

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

INFO: Started reloader process [6] using statreload

INFO: Started server process [8]

INFO: Waiting for application startup.

INFO: Application startup complete.

Upvotes: 1

Views: 1573

Answers (1)

muzak
muzak

Reputation: 83

If anyone has this problem with FastAPI,

Try adding --host 0.0.0.0 in your startup script.

Example: uvicorn main:app --reload --host 0.0.0.0 --port 8000

Upvotes: 2

Related Questions