I am receiving a KeyError when running my docker container stating that a .env variable is presumably missing. When I run the project locally, everything works fine. Therefore, I assume loading the python-dotenv’s .env file is not working correctly. After running the run command (part of build.sh file), the keyerror is thrown and container exits. I have the following project structure:
cryptoapp
-app (child directory)
--.env
--main.py
-docker (child directory)
--Dockerfile
--generate_plist_launchd.py
--requirements.txt
--build.sh
The dockerfile is as follows:
# Use an official Python runtime as a parent image
FROM python:3.11
# Set the working directory in the container
WORKDIR /cryptoapp
# Set the PYTHONPATH environment variable
ENV PYTHONPATH=/cryptoapp
# Copy requirements.txt and install dependencies
COPY docker/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy app directory into the container at /cryptoapp/app
COPY app /cryptoapp/app
# Copy docker directory into the container at /cryptoapp/docker
COPY docker /cryptoapp/docker
# Run the Python script
CMD ["python", "app/main.py"]
The build.sh file is as follows:
#!/bin/bash
# Define the image name
IMAGE_NAME="cryptoapp-image"
# Initialize plist file
PYTHON_SCRIPT="generate_plist_launchd.py"
python "$PYTHON_SCRIPT"
# Navigate to the root directory of the project
cd "$(dirname "$0")"/..
# Build Docker image if no image exists yet
docker build -t "$IMAGE_NAME" -f docker/Dockerfile .
# Run Docker container with environment variables from .env file
docker run --name cryptoapp --env-file app/.env "$IMAGE_NAME"
# Load plist file into launchd
LAUNCHD_DIR="$HOME/Library/LaunchAgents"
PLIST_FILE="$LAUNCHD_DIR/launchd_job.plist"
if [ -f "$PLIST_FILE" ]; then
# Extracting service ID from the plist file
SERVICE_ID=$(basename "$PLIST_FILE" .plist)
# Constructing the service target
SERVICE_TARGET="gui/$(id -u)/$SERVICE_ID"
# Enable the launchd job
launchctl enable "$SERVICE_TARGET"
echo "Launchd job enabled successfully."
# Kickstart the launchd job
launchctl kickstart "$SERVICE_TARGET"
echo "Launchd job started successfully."
else
echo "Error: Launchd plist file '$PLIST_FILE' not found."
exit 1
fi
I have also accounted for possibly an outdated python-dotenv dependency by including the following in the requirements.txt file:
python-dotenv>=0.15.0
Does anyone have a suggestion how I could figure out the issue? The .env file is referenced in the run command. Strangely, whenever I run the build command, it takes 0.0s to perform all COPY statements specified in the dockerfile. Yet, the CMD statement is executed as expected.
Any help is much appreciated 🙂
I tried testing the issue locally and in the docker container. Locally no errors occured and the application works as expected. In the container a keyerror is returned and the container exits immediatly. See info in the description.
Lawrence Mulders is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3