Cron job in one container cannot access database in another

Hiw fellows,

I have a repository [1] with a cron job defined with a database. I want to host it using docker-compose and populate rows using the database service name. I learned, I can use the service name as host in this use case. Python ORM library does not recognize this, though. The definition of host occurs in src/setup/config.py. I can access the containarized database with command psql, but not the other container.

Any help is very appreciated.

[1] GitHub - brunolnetto/db-cron-docker: A dockerized cron task with database

Share your docker-compose.yml.

services:
db-cron-task:
container_name: db-cron-task
hostname: postgres
image: postgres:14
restart: always
env_file: .env
expose :
- “$POSTGRES_PORT”
ports:
- “$POSTGRES_PORT:$POSTGRES_PORT”
volumes:
- ./data:/var/lib/postgresql/data

cron-task:
image: cron-task
container_name: cron-task
depends_on:
- db-cron-task
build:
context: .
restart: unless-stopped
volumes:
- .:/app

volumes:
postgres_data:

  1. List item

Use 3 backticks before and after code/config to make it more readable and keep the spacing, which is especially important in yaml.

Without having looked at the compose file (due to the lack of readability), I can highly recommend https://github.com/aptible/supercronic, which works with crontab files, but does neither require the container to start as root, nor any special twist to make it work.