I have always struggled with Docker as I mainly build front end web apps, where I have used services like Vercel to host my applications. Sometimes for work, I am required to build some scripts that need to run in a container.
And this where sh*t fits the fan generally. But no more!! I think I have finally cracked it, hello docker-compose
I have always seen these files but never really used them.
I'll give you a rundown of how I am using my Dockerfile
and docker-compose
together to get python running with pipenv.
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.9-slim
ENV VAR1=10
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install & use pipenv
COPY Pipfile Pipfile.lock ./
RUN python -m pip install --upgrade pip
RUN pip install pipenv && pipenv install --dev --system --deploy
WORKDIR /app
COPY . /app
# Creates a non-root user and adds permission to access the /app folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "main.py"]
This is my Docker file for running Python with Pipenv works well enough. This is generally the boilerplate from VSC when you add a Docker file to the workspace. I have only really replaced the requirements.txt
and pip
stuff with the below
COPY Pipfile Pipfile.lock ./
RUN python -m pip install --upgrade pip
RUN pip install pipenv && pipenv install --dev --system --deploy
Which in turns copies my Pipfile
& Pipfile.lock
and then installs my dependencies.
If you have a file named .env in your project, it’s only used to put values into the docker-compose.yml file which is in the same folder. Those are used with Docker Compose and Docker Stack. It has nothing to do with ENV, ARG, or anything Docker-specific.
The key-value pairs, in your .env
file are used to substitute dollar-notation variables in the docker-compose.yml file. It’s kind of a pre-processing step, and the resulting temporary file is used. This is a nice way to avoid hard-coding values. You can also use this to set the values for environment variables, by substituting the string, but that does not happen automatically.
Here is an example docker-compose.yml file, relying on values provided from a .env file:
version: '3'
services:
plex:
image: linuxserver/plex
environment:
- env_var_name=${VARIABLE_NAME} # here it is
Hint: When working with an .env file, you can debug your docker-compose.yml files quite easily. Just type
docker-compose config
. This way you’ll see how the docker-compose.yml file content looks after the substitution step has been performed without running anything else.
Once you're done just run docker-compose -f docker-compose.yaml up
this will run a container with your image and all the specified env variables. Like anything it's easy when you understand it.
Top comments (0)