I'm a little bit confused when building a docker c...
# prefect-getting-started
r
I'm a little bit confused when building a docker composer file to run a Prefect server (2.0) , Postgres and Prefect UI. What I have now is this;
Copy code
version: "3"

services:
  server:
    image: prefecthq/prefect:2-python3.11
    container_name: prefect-server
    restart: always
    command: ["prefect", "server", "start"]
    entrypoint: "/opt/prefect/entrypoint.sh"
    ports:
      - "4200:4200"
    depends_on:
      - postgres
    environment:
      PREFECT_DEBUG_MODE: "true"
      PREFECT_API_DATABASE_CONNECTION_URL: '<postgresql+asyncpg://rbs:123@postgres/prefect>'
      PREFECT_API_DATABASE_ECHO: "false"
      PREFECT_API_DATABASE_MIGRATE_ON_START: "true"
      

  postgres:
    image: postgres:latest
    container_name: prefect-db
    restart: always
    environment:
      POSTGRES_PASSWORD: 123
      POSTGRES_USER: rbs
      POSTGRES_DB: prefect
    ports:
      - "5432:5432"
    expose:
      - 5432
    volumes:
      - db_data:/var/lib/postgresql/data
  ui:
    image: prefecthq/ui:latest
    container_name: prefect-ui
    restart: always
    depends_on:
      - server
    ports:
      - "8080:8080"
    environment:
      PREFECT_API_DATABASE_CONNECTION_URL: '<postgresql+asyncpg://rbs:123@postgres/prefect>'
      PREFECT_API_DATABASE_ECHO: "false"
      PREFECT_API_DATABASE_MIGRATE_ON_START: "true"
      PREFECT_SERVER__APOLLO_URL: "/graphql"
      PREFECT_API_URL: "/api"
      PREFECT_SERVER__BASE_URL: "<http://localhost:4200>"
volumes:
  db_data: {}
my main questions are: 1. why the server is not being accessible on my browser through localhost or 127.0.0.1 ... I'm using macos without any firewalls or anything like that. Prefect UI is being accessible through localhost 8080 though 2. I'm also confused where certain env variables goes and the right values for them. thanks a lot
b
Not addressing directly your questions, but here the docker-compose.yaml that I always use and is quite simple:
Copy code
services:
  ### PostgreSQL Database
  database:
    image: postgres:16
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=prefect
    expose:
      - 5432
    volumes:
      - postgres:/var/lib/postgresql/data

  ### Prefect server API
  prefect-server:
    image: prefecthq/prefect:2-python3.11
    restart: always
    volumes:
      - prefect:/home/ubuntu/.prefect
    entrypoint: ["prefect", "server", "start"]
    environment:
      - PREFECT_SERVER_API_HOST=0.0.0.0
      - PREFECT_API_DATABASE_CONNECTION_URL=<postgresql+asyncpg://postgres:postgres@database:5432/prefect>
    ports:
      - 4200:4200
    depends_on:
      - database

volumes:
  prefect:
  postgres:
The UI is then accessible at http://localhost:4200/ and api is at http://localhost:4200/api
r
@Baker hey man, thanks for your help , it works, but why the /api returns 404?
b
I don't know, but you are not supposed to use /api in your browser anyway. What you might want is http://localhost:4200/docs
http://localhost:4200/api goes in
~/.prefect/profiles.toml
like:
Copy code
active = "local"
[profiles.local]
PREFECT_API_URL = "<http://127.0.0.1:4200/api>"
PREFECT_TASK_INTROSPECTION_WARN_THRESHOLD = "0"
you will see that http://localhost:4200/api/health should work, for example.
r
yeah, it returns true ... that should be enough for us to put that on ECS and try other things ... thanks a lot!
one last question, do we need those toml files or we can use env vars instead?
b
You can use env var. Since you mentioned ECS, this is how a worker looks like on ECS, with the API url as env var:
Copy code
"containerDefinitions": [
        {
            "name": "prefect-worker",
            "image": "prefecthq/prefect:2-latest",
            "cpu": 512,
            "memory": 1024,
            "portMappings": [],
            "essential": true,
            "command": [
                "/bin/sh",
                "-c",
                "pip install prefect-aws && prefect worker start --pool cluster-dev --type ecs"
            ],
            "environment": [
                {
                    "name": "PREFECT_API_URL",
                    "value": "<http://10.0.2.35:4200/api>"
                }
            ],
            "mountPoints": [],
            "volumesFrom": []
        }
    ],
r
great! Although now we're transitioning from celery to Prefect and we're still dependent on celery workers ... so for now we'll be using those workers with Prefect agents installed