Home > other >  docker-compose up shows both services running but why does the page not respond?
docker-compose up shows both services running but why does the page not respond?

Time:12-21

I'm trying to run a fastapi web service and postgres db on docker. here's the docker-compose.yml

version: '3.4'
services:
  db:
    image: postgres:13.1-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./app/.env
    ports:
      - 5432:5432

  web:
    build:
      context: ./app
      dockerfile: Dockerfile
    volumes:
      - ./app/:/app/
    command: uvicorn main:app --reload --workers 1 --port 8000
    env_file:
      - ./app/.env
    ports:
      - 8000:8000
    depends_on:
      - db

volumes:
    postgres_data:

dockerfile

FROM python:3.10.8

WORKDIR /app

COPY requirements.txt ./

RUN pip3 install --no-cache-dir --upgrade -r requirements.txt

COPY . .

EXPOSE 8000

here's my .env

POSTGRES_USER=myname
POSTGRES_PASSWORD=
POSTGRES_SERVER=db
POSTGRES_PORT=5432
POSTGRES_DB=postgres

in the terminal, both services seem to be up and running with no errors or warnings: pic of my terminal

what else am I missing here? when I visit: http://127.0.0.1:8000 I just see a page is not working. Could it be the way the repo directory is set up?

// directory
Project
  - app
    - config.py
    - database.py
    - Dockerfile
    - main.py
    - requirements.txt
  - docker-compose.yml

CodePudding user response:

the problem is that you didn't specify a host, update your docker-compose file as follows:

version: '3.4'
services:
  db:
    image: postgres:13.1-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./app/.env
    ports:
      - 5432:5432

  web:
    build:
      context: ./app
      dockerfile: Dockerfile
    volumes:
      - ./app/:/app/
    command: uvicorn main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    env_file:
      - ./app/.env
    ports:
      - 8000:8000
    depends_on:
      - db

volumes:
    postgres_data:
  • Related