s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.79k stars 161 forks source link

Project starts with Docker error. #174

Open shpilevskiyevgeniy opened 1 year ago

shpilevskiyevgeniy commented 1 year ago

When starting the project docker-compose -f deploy/docker-compose.yml --project-directory . up --build, an error occurs:

Status: Downloaded newer image for postgres:13.8-bullseye Pulling migrator (hrm:latest)... ERROR: The image for the service you're trying to recreate has been removed. If you continue, volume data could be lost. Consider backing up your data before continuing.

Continue with the new image? [yN]y Pulling migrator (hrm:latest)... ERROR: pull access denied for hrm, repository does not exist or may require 'docker login': denied: requested access to the resource is denied (hrm-py3.10) lab42@lab42-Linux:~/Рабочий стол/dev/hrm$

s3rius commented 1 year ago

Can you, please, show generated docker-compose?

shpilevskiyevgeniy commented 1 year ago

version: '3.9'

services: api: &main_app build: context: . dockerfile: ./deploy/Dockerfile target: prod image: hrm:${HRM_VERSION:-latest} restart: always env_file:

volumes: hrm-db-data: name: hrm-db-data

networks:

Network for traefik.

traefik-shared: name: traefik-shared

shpilevskiyevgeniy commented 1 year ago

It also throws the following error on startup: (hrm-py3.10) lab42@lab42-Linux:~/Рабочий стол/dev/hrm$ sudo docker-compose -f deploy/docker-compose.dev.yml --project-directory . up --build ERROR: The Compose file is invalid because: Service api has neither an image nor a build context specified. At least one must be provided.

version: '3.9'

services: api: ports:

Exposes application port.

- "8000:8000"
build:
  target: dev
volumes:
  # Adds current directory as volume.
- .:/app/src/
environment:
  # Enables autoreload.
  HRM_RELOAD: "True"

taskiq-worker: volumes:

Adds current directory as volume.

- .:/app/src/
command:
- taskiq
- worker
- hrm.tkq:broker
- --reload
s3rius commented 1 year ago

Can you wrap your code into blocks?

 
```yaml
{your-code here}
```
shpilevskiyevgeniy commented 1 year ago
version: '3.9'

services:
  api: &main_app
    build:
      context: .
      dockerfile: ./deploy/Dockerfile
      target: prod
    image: hrm:${HRM_VERSION:-latest}
    restart: always
    env_file:
    - .env
    labels:
      # Enables traefik for this container.
    - traefik.enable=true
    - traefik.http.routers.hrm.rule=Host(`${HRM_TRAEFIK_HOST:-hrm.localhost}`)
    - traefik.http.routers.hrm.entrypoints=http
    - traefik.http.routers.hrm.service=hrm
    - traefik.http.services.hrm.loadbalancer.server.port=${HRM_PORT:-8000}
    networks:
    - default
    - traefik-shared
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      kafka:
        condition: service_healthy
    environment:
      HRM_HOST: 0.0.0.0
      HRM_DB_HOST: hrm-db
      HRM_DB_PORT: 5432
      HRM_DB_USER: hrm
      HRM_DB_PASS: hrm
      HRM_DB_BASE: hrm
      HRM_REDIS_HOST: hrm-redis
      TESTKAFKA_KAFKA_BOOTSTRAP_SERVERS: '["hrm-kafka:9092"]'

  taskiq-worker:
    <<: *main_app
    labels: []
    command:
    - taskiq
    - worker
    - hrm.tkq:broker

  db:
    image: postgres:13.8-bullseye
    hostname: hrm-db
    environment:
      POSTGRES_PASSWORD: "hrm"
      POSTGRES_USER: "hrm"
      POSTGRES_DB: "hrm"
    volumes:
    - hrm-db-data:/var/lib/postgresql/data
    restart: always
    healthcheck:
      test: pg_isready -U hrm
      interval: 2s
      timeout: 3s
      retries: 40

  migrator:
    image: hrm:${HRM_VERSION:-latest}
    restart: "no"
    command: alembic upgrade head
    environment:
      HRM_DB_HOST: hrm-db
      HRM_DB_PORT: 5432
      HRM_DB_USER: hrm
      HRM_DB_PASS: hrm
      HRM_DB_BASE: hrm
    depends_on:
      db:
        condition: service_healthy

  redis:
    image: bitnami/redis:6.2.5
    hostname: "hrm-redis"
    restart: always
    environment:
      ALLOW_EMPTY_PASSWORD: "yes"
    healthcheck:
      test: redis-cli ping
      interval: 1s
      timeout: 3s
      retries: 50

  zookeeper:
    image: "bitnami/zookeeper:3.7.1"
    hostname: "hrm-zookeeper"
    environment:
      ALLOW_ANONYMOUS_LOGIN: "yes"
      ZOO_LOG_LEVEL: "ERROR"
    healthcheck:
      test: zkServer.sh status
      interval: 1s
      timeout: 3s
      retries: 30

  kafka:
    image: "bitnami/kafka:3.2.0"
    hostname: "hrm-kafka"
    environment:
      KAFKA_BROKER_ID: "1"
      ALLOW_PLAINTEXT_LISTENER: "yes"
      KAFKA_CFG_LISTENERS: "PLAINTEXT://0.0.0.0:9092"
      KAFKA_CFG_ADVERTISED_LISTENERS: "PLAINTEXT://hrm-kafka:9092"
      KAFKA_CFG_ZOOKEEPER_CONNECT: "hrm-zookeeper:2181"
    healthcheck:
      test: kafka-topics.sh --list --bootstrap-server localhost:9092
      interval: 1s
      timeout: 3s
      retries: 30
    depends_on:
      zookeeper:
        condition: service_healthy

volumes:
  hrm-db-data:
    name: hrm-db-data

networks:
  # Network for traefik.
  traefik-shared:
    name: traefik-shared
s3rius commented 1 year ago

The docker-compose.dev.yaml should be used along with the main file. Like this:

docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up --build
shpilevskiyevgeniy commented 1 year ago
version: '3.9'

services:
  api:
    ports:
      # Exposes application port.
    - "8000:8000"
    build:
      target: dev
    volumes:
      # Adds current directory as volume.
    - .:/app/src/
    environment:
      # Enables autoreload.
      HRM_RELOAD: "True"

  taskiq-worker:
    volumes:
      # Adds current directory as volume.
    - .:/app/src/
    command:
    - taskiq
    - worker
    - hrm.tkq:broker
    - --reload

(hrm-py3.10) lab42@lab42-Linux:~/Рабочий стол/dev/hrm$ sudo docker-compose -f deploy/docker-compose.dev.yml --project-directory . up --build ERROR: The Compose file is invalid because: Service api has neither an image nor a build context specified. At least one must be provided.

s3rius commented 1 year ago

What docker version are you using?

I guess it has some problems finding your image. Please try building image before running it. Maybe it would help.

docker-compose -f deploy/docker-compose.yml --project-directory . build
docker-compose -f deploy/docker-compose.yml --project-directory . up
shpilevskiyevgeniy commented 1 year ago

Docker version 23.0.2, build 569dd73

s3rius commented 1 year ago

Consider upgrading it to 24+. Just to make sure. But still, it should work fine on 23+.

s3rius commented 1 year ago

This postgresql image works fine on my computer. Can you have access problems to hub.docker.com?

❯ docker run --rm -it postgres:13.8-bullseye
Unable to find image 'postgres:13.8-bullseye' locally
13.8-bullseye: Pulling from library/postgres
e9995326b091: Already exists 
a0cb03f17886: Already exists 
bb26f7e78134: Already exists 
c8e073b7ae91: Already exists 
99b5b1679915: Already exists 
55c520fc03c5: Pull complete 
d0ac84d6672c: Pull complete 
4effb95d5849: Pull complete 
97fd2548fc1e: Pull complete 
43e7f13e3769: Pull complete 
2898936d5b2e: Pull complete 
b4b731b0864d: Pull complete 
fbd79522dd4c: Pull complete 
Digest: sha256:2b31dc28ab2a687bb191e66e69c2534c9c74107ddb3192ff22a04de386425905
Status: Downloaded newer image for postgres:13.8-bullseye
Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html
shpilevskiyevgeniy commented 1 year ago

When I run the command: docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up --build it runs containers and throws an error:

taskiq-worker_1 | ValueError: To use '--reload' flag, please install 'taskiq[reload]'.

and if I run only dev, then it gives an error: (hrm-py3.10) lab42@lab42-Linux:~/Desktop/dev/hrm$ sudo docker-compose -f deploy/docker-compose.dev.yml --project-directory . up --build ERROR: Compose file is invalid because: Service api has neither an image nor a build context specified. At least one must be provided.

s3rius commented 1 year ago

Taskiq moved some dependencies to extras.

Can you replace your taskiq entry from pyproject toml with taskiq = {version = "^0", extras = ["reload"]}

After that, update your poetry.lock by running poetry update.

It should fix it.

shpilevskiyevgeniy commented 1 year ago

So how do I run dev? sudo docker-compose -f deploy/docker-compose.dev.yml --project-directory . up --build

ERROR: The Compose file is invalid because: Service api has neither an image nor a build context specified. At least one must be provided.

s3rius commented 1 year ago

As I showed you above. https://github.com/s3rius/FastAPI-template/issues/174#issuecomment-1602292485

shpilevskiyevgeniy commented 1 year ago

Taskiq moved some dependencies to extras.

Can you replace your taskiq entry from pyproject toml with taskiq = {version = "^0", extras = ["reload"]}

After that, update your poetry.lock by running poetry update.

It should fix it.

In payproject I have exactly this version: taskiq = { version = "^0", extras = ["reload"] }

s3rius commented 1 year ago

Can you try building image with dev compose file for dev?

docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . build
shpilevskiyevgeniy commented 1 year ago

The migrator container does not start and gives an error: hrm_migrator_1 exited with code 0

what command should I run only dev and what prod?

s3rius commented 1 year ago

No, that's okay. 0 code means Everything worked fine and all migrations ran successfully.

s3rius commented 1 year ago

The migrator is a container that only runs migrations and shuts down. You should run it before rolling out new version of the application. It will apply all migrations and then you can start your application.

s3rius commented 1 year ago

However, in docker-compose configuration migrator runs automatically, when you start it.