devcontainers/python
A dev container spec-supported image for working with Python.
Develop Python 3 applications.
| Metadata | Value |
|---|---|
| Categories | Core, Languages |
| Image type | Dockerfile |
| Published image | mcr.microsoft.com/devcontainers/python |
| Available image variants | 3 / 3-trixie, 3.14 / 3.14-trixie, 3.13 / 3.13-trixie, 3.12 / 3.12-trixie, 3.11 / 3.11-trixie, 3.10 / 3.10-trixie, 3.14-bookworm, 3.13-bookworm, 3.12-bookworm, 3.11-bookworm, 3.10-bookworm (full list) |
| Published image architecture(s) | x86-64, arm64/aarch64 for trixie and bookworm variants |
| Container Host OS Support | Linux, macOS, Windows |
| Container OS | Debian |
| Languages, platforms | Python |
See history for information on the contents of published images.
You can directly reference pre-built versions of Dockerfile by using the image property in .devcontainer/devcontainer.json or updating the FROM statement in your own Dockerfile with one of the following:
mcr.microsoft.com/devcontainers/python:3 (latest)mcr.microsoft.com/devcontainers/python:3.10 (or 3.10-trixie, 3.10-bookworm to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.11 (or 3.11-trixie, 3.11-bookworm to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.12 (or 3.12-trixie, 3.12-bookworm to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.13 (or 3.13-trixie, 3.13-bookworm to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.14 (or 3.14-trixie, 3.14-bookworm to pin to an OS version)
Refer to this guide for more details.You can decide how often you want updates by referencing a semantic version of each image. For example:
mcr.microsoft.com/devcontainers/python:3-3.14 (or 3-3.14-trixie)mcr.microsoft.com/devcontainers/python:3.2-3.14 (or 3.2-3.14-trixie)mcr.microsoft.com/devcontainers/python:3.2.2-3.14 (or 3.2.2-3.14-trixie)However, we only do security patching on the latest non-breaking, in support versions of images (e.g. 3-3.14). You may want to run apt-get update && apt-get upgrade in your Dockerfile if you lock to a more specific version to at least pick up OS security updates.
See history for information on the contents of each version and here for a complete list of available tags.
Alternatively, you can use the contents of .devcontainer to fully customize the your container's contents or build for a container architecture the image does not support.
Beyond Python and git, this image / Dockerfile includes a number of Python tools, zsh, Oh My Zsh!, a non-root vscode user with sudo access, and a set of common dependencies for development.
Given JavaScript front-end web client code written for use in conjunction with a Python back-end often requires the use of Node.js-based utilities to build, this container also includes nvm so that you can easily install Node.js.
Also, you can use a Node feature to install any version of Node by adding the following to devcontainer.json:
{
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "latest"
}
}
}
This container installs all Python development utilities using pipx to avoid impacting the global Python environment. You can use this same utility add additional utilities in an isolated environment. For example:
pipx install prospector
See the pipx documentation for additional information.
By default, frameworks like Flask only listens to localhost inside the container. As a result, we recommend using the forwardPorts property (available in v0.98.0+) to make these ports available locally.
"forwardPorts": [5000]
The appPort property publishes rather than forwards the port, so applications need to listen to * or 0.0.0.0 for the application to be accessible externally. This conflicts with the defaults of some Python frameworks, but fortunately the forwardPorts property does not have this limitation.
If you've already opened your folder in a container, rebuild the container using the Remote-Containers: Rebuild Container command from the Command Palette (F1) so the settings take effect.
If your requirements rarely change, you can include the contents of requirements.txt in the container by adding the following to your Dockerfile:
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
Since requirements.txt is likely in the folder you opened be sure to include "context": ".." to devcontainer.json. This allows the Dockerfile to access everything in the opened folder.
You can opt into using the vscode non-root user in the container by adding "remoteUser": "vscode" to devcontainer.json. However, by default, this you will need to use sudo to perform global pip installs.
sudo pip install <your-package-here>
Or stick with user installs:
pip install --user <your-package-here>
If you prefer, you can add the following to your Dockerfile to cause global installs to go into a different folder that the vscode user can write to.
ENV PIP_TARGET=/usr/local/pip-global
ENV PYTHONPATH=${PIP_TARGET}:${PYTHONPATH}
ENV PATH=${PIP_TARGET}/bin:${PATH}
RUN if ! cat /etc/group | grep -e "^pip-global:" > /dev/null 2>&1; then groupadd -r pip-global; fi \
&& usermod -a -G pip-global vscode \
&& umask 0002 && mkdir -p ${PIP_TARGET} \
&& chown :pip-global ${PIP_TARGET} \
&& ( [ ! -f "/etc/profile.d/00-restore-env.sh" ] || sed -i -e "s/export PATH=/export PATH=\/usr\/local\/pip-global:/" /etc/profile.d/00-restore-env.sh )
If you would prefer to have multiple Python versions in your container, use Dockerfile and update FROM statement:
FROM ubuntu:jammy
ARG PYTHON_PACKAGES="python3.8 python3.9 python3 python3-pip python3-venv"
RUN apt-get update && apt-get install --no-install-recommends -yq software-properties-common \
&& add-apt-repository ppa:deadsnakes/ppa && apt-get update \
&& apt-get install -yq --no-install-recommends ${PYTHON_PACKAGES} \
&& pip3 install --no-cache-dir --upgrade pip setuptools wheel
The dev container spec images are maintained in the devcontainers/images repo. You can explore each image and open issues or feature requests.
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. See LICENSE
No tags have been pushed to this repository yet.