Skip to content

[BUG] pre_start no longer inherits its parent environment #14287

Closed

Description

@JoaoLockem

Description

In docker compose, I have a pre_start clause to run an alembic command. before docker compose v5.6.0 it worked fine, once I upgraded my version I started having issues

  File "//alembic/env.py", line 104, in get_database_uri
    raise RuntimeError(f"{DATABASE_URI_VARIABLE} is not set in the environment.")
RuntimeError: DATABASE_URI is not set in the environment.

I ran docker compose config and got

(...)
  src:
    restart: always
    pre_start:
      - command:
          - alembic
          - upgrade
          - head
        env_file:
          - path: {CORRECT_PATH_TO_ENV_FILE}
        extra_hosts:
          - host.docker.internal=host-gateway
        networks:
          default: null
    environment:
      DATABASE_URI: (...)

But the environment values are not recognized inside the pre_start container. This is the result of env in the pre_start container:

HOSTNAME=7df048caff72
HOME=/root
PYTHONUNBUFFERED=1
GPG_KEY=7169605F62C751356D054A26A821E680E5FA6305
PYTHON_SHA256=f4b1bfb3c79b5bb11b8d228a12504163b4c0dab4d679828d8f5f26b6cb6ab35d
PYTHONDONTWRITEBYTECODE=1
PATH=/opt/venv/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PYTHON_VERSION=3.13.16
PWD=/

Steps To Reproduce

  1. with docker version 29.8.2, Docker Compose version v5.6.0
  2. docker compose config
services:
  database:
    image: postgres:15-alpine
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}" ]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    volumes:
      - database:/var/lib/postgresql/data
    env_file:
      - .env
    restart: always

  src:
    build:
      context: .
      secrets:
        - url_lib_common_py
    volumes:
      - ./logs:/src/logs
      - ./alembic:/alembic
      - config:/root/.config
    extra_hosts:
      - "host.docker.internal:host-gateway"
    env_file:
      - .env
    depends_on:
      database:
        condition: service_healthy
        restart: true
    pre_start:
      - command: alembic upgrade head
    restart: always

secrets:
  url_lib_common_py:
    environment: URL_LIB_COMMON_PY


volumes:
  database:
  credentials:
  config:
  1. Run docker compose up
  2. Get:
(...)
  File "//alembic/env.py", line 104, in get_database_uri
    raise RuntimeError(f"{DATABASE_URI_VARIABLE} is not set in the environment.")
RuntimeError: DATABASE_URI is not set in the environment.

Compose Version

v5.6.0

Docker Environment

29.8.2

Anything else?

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions