[Python] Python cannot use bind mounted files in Docker

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    There is my simplified code that causes problems

    env_path = "resources/.env"
    if os.path.exists(env_path):
    load_dotenv(env_path)
    else:
    raise ResourcesMissing('.env missing')

    tg_token_env = "resources/tg_token.env"
    if os.path.exists(tg_token_env):
    load_dotenv(tg_token_env)
    else:
    raise ResourcesMissing('tg_token.env missing')


    This code raises exception in docker but works fine on my win pc

    docker-compose.yml

    services:
    bot:
    build: .
    volumes:
    - ./media:/app/media:ro
    - ./resources:/app/resources:rw


    Dockerfile

    FROM python:3.13.5-slim
    WORKDIR /usr/src/app
    ENV CONTAINER=docker

    RUN mkdir -p /app/media
    RUN mkdir -p /app/resources

    RUN apt-get update
    RUN pip install --upgrade pip
    COPY . .

    [Installing dependencies]

    CMD ["python", "bot.py"]


    also i have .dockerignore file

    media/*
    resources/*


    I checked the Files tab on the Docker desktop and the container has mounted the files, but Python can't read them. files tab

    Im tried different formations of docker-compose.yml. Tried to pack .env file in image and it works but i need it in mount bind. Tried to define env_file in docker-compose.yml. I tried many things but always getting exception

    Continue reading...

Compartilhe esta Página