pythondockerdocker-composeprefect

prefect) Can't connect to Server API at http://0.0.0.0:4200/api in docker container


I tried to build docker container to use prefect server. But there is a problem that I can't connect the server in container from local environment.

my prefect version : 2.10.4(latest)

As I search this issue, there is no exact solution for this issue. Pleas let me know if you need more information to solve this issue. Thanks.

My Steps --

  1. docker-compose.yml (ref: https://medium.com/the-prefect-blog/how-to-self-host-prefect-orion-with-postgres-using-docker-compose-631c41ab8a9f)
version: "3.9"
services:
  ### PostgreSQL Database
  database:
    image: postgres:14.1-alpine
    restart: always
    environment:
      - POSTGRES_USER=prefect
      - POSTGRES_PASSWORD=prefect
      - POSTGRES_DB=prefect
    expose:
      - 5432
    volumes: 
      - postgres:/var/lib/postgresql/data
    profiles: ["server"]

  ### Prefect Orion API
  orion:
    image: prefecthq/prefect:2-python3.11
    restart: always
    volumes:
      - prefect:/.prefect
    entrypoint: ["prefect", "server", "start"]
    environment:
      - PREFECT_SERVER_API_HOST=0.0.0.0
      - PREFECT_API_DATABASE_CONNECTION_URL=postgresql+asyncpg://prefect:prefect@database:5432/prefect
    ports:
      - 4200:4200
    depends_on:
      - database
    profiles: ["server"]
      
volumes:
  prefect:
  postgres:
  1. Enter "http://localhost:4200/api"
  2. Result (figure) enter image description here

Here's my tries. I changed PREFECT_SERVER_API_HOST to 127.0.0.1, public IP, 0.0.0.0 but not works. I changed PREFECT_SERVER_API_HOST not to 4200 but not works I entered the 127.0.0.1:4200, localhost:4200, 0.0.0.0:4200 just build dockerfile (not docker compose)


Solution

  • Sounds like PREFECT_SERVER_API_HOST is used to set IP address to listen at server side (inside container). But outside of container you have to request different IP address.

    So if you set PREFECT_SERVER_API_HOST=localhost then it will be correct for connections outside container (because http://localhost:4200/api UI is working correct). But! in this case server will listen localhost inside of container and it will be not accessible outside.

    So you have to set 0.0.0.0 for listening interface and localhost as URL for requests sent via UI. And here is PREFECT_UI_API_URL parameter created especially for this case:

    ...
        environment:
          - PREFECT_SERVER_API_HOST=0.0.0.0
          - PREFECT_UI_API_URL=http://localhost:4200/api
    ...