Develop Python 3 applications.
Metadata | Value |
---|---|
Categories | Core, Languages |
Image type | Dockerfile |
Published image | mcr.microsoft.com/devcontainers/python |
Available image variants | 3 / 3-bullseye, 3.7 / 3.7-bullseye, 3.8 / 3.8-bullseye, 3.9 / 3.9-bullseye, 3.10 / 3.10-bullseye, 3.11-bullseye, 3.11, 3-buster, 3.7-buster, 3.8-buster, 3.9-buster, 3.10-buster, 3.11-buster (full list) |
Published image architecture(s) | x86-64, arm64/aarch64 for bullseye 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.7
(or3.7-bullseye
,3.7-buster
to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.8
(or3.8-bullseye
,3.8-buster
to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.9
(or3.9-bullseye
,3.9-buster
to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.10
(or3.10-bullseye
,3.10-buster
to pin to an OS version)mcr.microsoft.com/devcontainers/python:3.11
(or3.11-bullseye
,3.11-buster
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:0-3.9
(or0-3.9-bullseye
,0-3.9-buster
)mcr.microsoft.com/devcontainers/python:0.203-3.9
(or0.203-3.9-bullseye
,0.203-3.9-buster
)mcr.microsoft.com/devcontainers/python:0.202.5-3.9
(or0.202.5-3.9-bullseye
,0.202.5-3.9-buster
)
However, we only do security patching on the latest non-breaking, in support versions of images (e.g. 0-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 Dockerfile
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:bionic
ARG PYTHON_PACKAGES="python3.5 python3.6 python3.7 python3.8 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
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. See LICENSE