Pulling docker image is not detecting the .env file variables

Hi, I am new to Docker. I created an image for a streamlit-based chatbot app, and pushed it to docker hub repository. Initially I had created a repository but due to name mismatch, it was uploaded to a new repository that Docker Hub created. I then deleted the local copy of the image and pulled the image from the repository.

Now when I was running the image locally in my device using docker compose up, it was working fine. I did not use docker build command as it was an app with several depencies, one of which was the .env file.

Now after pulling the image from the hub and trying to run it, it is unable to detect the API credentials from the .env file. i am not sure what to do, as I had written both the Dockerfile and the docker-compose.yml files correctly. I have pasted the codes here.

Dockerfile:

FROM python:3.13-slim

# Prevent Python from writing .pyc files and enable unbuffered logs
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

# Install system deps (minimal; enough for common Python wheels)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
  && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy app code
COPY . .

# Streamlit port
EXPOSE 8501

# Run Streamlit app (Change final.py if your module/object is different)
CMD ["streamlit", "run", "final.py", "--server.port=8501", "--server.address=0.0.0.0"]

docker-compose:

services:
  streamlit:
    image: forwardair-streamlit:latest
    build: .
    container_name: forwardair-streamlit
    ports:
      - "8501:8501"
    env_file:
      - .env
    volumes:
      # Persist FAISS index between runs (optional, but useful)
      - ./faiss_index:/app/faiss_index
    restart: unless-stopped

Please help me resolve this issue. Thanks!

EDIT: It is also happening when I use Azure Content Registry

If you have the right dot env file, the variables are set in the container. You can use docker container inspect to show the metadata of the container which also contains the environment variables.

docker container inspect CONTAINERNAME --format '{{ json .Config.Env }}'

What do you see there? Do you see the required variable? Please, show the output using a fake api key

The registry is not relevant hre. You most likely just did something else you don’t remember or was not even intentional, but when you pull a previously successfully pushed image, you will get the same image with the same features. And if the dot env file cannot be read correctly, it will not be caused by the image, but the Docker daemon.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.