An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
Setting up Docker with Pipenv
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-pythonFROM python:3.9-slimENV VAR1=10# Keeps Python from generating .pyc files in the containerENV PYTHONDONTWRITEBYTECODE=1# Turns off buffering for easier container loggingENV PYTHONUNBUFFERED=1# Install & use pipenvCOPY Pipfile Pipfile.lock ./RUN python -m pip install --upgrade pipRUN pip install pipenv && pipenv install --dev --system --deployWORKDIR /appCOPY . /app# Creates a non-root user and adds permission to access the /app folderRUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /appUSER appuser# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debugCMD ["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 pipRUN 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, its 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. Its 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 youll 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.
Original Link: https://dev.to/mrpbennett/setting-up-docker-with-pipenv-3h1o
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To