Using a virtual environment to isolate project dependencies is great, but what’s even greater is containerization. In other words, using Docker. All you need is a file called devcontainer.json.
Dev containers are docker images running on your local machine as a customized, fully featured development environment. It allows developers to work on multiple projects which require different frameworks, dependencies or versions of a language.
They isolate each project’s environment so that dependencies don’t overlap or disrupt your local setup or other projects. Furthermore, they allow you to standardize the development environment and are very easy to set up.
No more “it works on my computer” issues!
Prerequisites:
There are 3 ways to create a container:
From a predefined container — by using a base image from the registry
From an existing “Dockerfile”
From an existing “docker-compose.yml” file
In this tutorial, we’ll cover options 1 and 2. For the third option, you can find relevant tutorials located at the bottom.
Using a predefined base image
All container configuration is stored in a standard devcontainer.json file in the .devcontainer folder. This packages instructions for your environment.
You can start with any base image of a specific language, generate a new development environment, and start developing with it.
Let’s create a new development container for a Python project from a base image.
You can create devcontainer.json either from scratch or by using the *Dev Containers: Add Dev Container Configuration Files… in VS Code.
(to open command palette: (Ctrl+Shift+P)
With this command, you can pick a pre-defined container configuration from a list.
Next step: choosing the base image. You can type Python to search for Python base images. I select the first one showing up.
Next step: you can choose the Python version.
Next step: Additional features to install, I skip this, just click ok.
These are to add specific tools or containers into your dev container configuration. More info: https://containers.dev/features
It creates devcontainer.json file under .devcontainer folder.
#devcontainer.json
{
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:1-3.9-bullseye"
}
Next step: Click on Reopen in Container to run this configuration:
Or you can type Dev Container *Dev Containers: Rebuild Container… in VS Code command palette.
It will pull the base docker image: “mcr.microsoft.com/devcontainers/python:1–3.9-bullseye”, then create an SSH server in it. This a pre-defined container configuration from devcontainer registry provided by Microsoft.
If you open a terminal in VS Code, you can easily SSH into the container environment and execute your Python files.
I am using the code from movie-recommender as an example, you can find the repo here: https://github.com/marvelousmlops/movie-recommender
As you can see, my entire project folder is available in the container.
Next step: I run the following command to install my dependencies in dev environment:
$ pip install requirements.txt
Next Step: Execute main.py
Alternatively, you can add the following command as a post-creation command in your json file:
#devcontainer.json
{
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:1–3.10-bullseye",
// Use 'postCreateclCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install -r requirements.txt"
}
In this way, dependencies will be installed automatically right after container creation.
Another alternative, you can run a bash file:
#script.sh
#!/bin/sh
pip3 install -r requirements.txt
#devcontainer.json
{
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:1–3.10-bullseye",
// Use 'postCreateclCommand' to run commands after the container is created.
"postCreateCommand": "sh .devcontainer/script.sh"
}
Using an existing Dockerfile
We’ve seen how to create a new devcontainer froms scratch. In an ongoing project, it’s likely that the repository already has a Dockerfile for development. If you are already given a Dockerfile, the repo structure should look like this:
- .devcontainer/
- devcontainer.json
- Dockerfile
- requirements.txt
- topn/
- #python modules
- tests/
- #test scripts
- main.py
In this case, we will refer to the Dockerfile from devcontainer.json, instead of giving a base image.
#devcontainer.json
{
"build": {
// Path is relative to the devcontainer.json file.
"dockerfile": "Dockerfile"
}
Example Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster
# Upgrade pip
RUN python -m pip install --upgrade pip
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
&& rm -rf /tmp/pip-tmp
You can still use a pre-defined container configuration from devcontainer registry provided by Microsoft and additional steps in Dockerfile.
FROM mcr.microsoft.com/devcontainers/python:1-3.10-bullseye
# Upgrade pip
RUN python -m pip install --upgrade pip
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
&& rm -rf /tmp/pip-tmp
Conclusion
Dev containers create a consistent development environment that can be shared by everyone, it also makes it easy to onboard a new person to the project. They keep dependencies, requirements organized for different projects and allow developers to focus on implementation of softwares instead of dealing with setup troubles.
Tutorial for using docker-compose.yml file to create devcontainer:
https://github.com/luabud/petgram/tree/main
Other references:
https://code.visualstudio.com/docs/devcontainers/containers
https://www.josephguadagno.net/2022/12/10/getting-started-with-developer-containers
https://www.freecodecamp.org/news/standardize-development-environment-with-devcontainers/
Hi,
where can i find the requirements.txt for movie-recommender ?