d6c4972024 | ||
---|---|---|
.. | ||
.devcontainer | ||
.vscode | ||
test-project | ||
.npmignore | ||
README.md |
README.md
Python 3 & PostgreSQL
Summary
Develop applications with Python 3 and PostgreSQL. Includes a Python application container and PostgreSQL server.
Metadata | Value |
---|---|
Contributors | The VS Code Python extension team |
Categories | Core, Languages |
Definition type | Docker Compose |
Works in Codespaces | Yes |
Available image variants | See Python 3 definition. |
Supported architecture(s) | x86-64, arm64/aarch64 for bullseye based images |
Container host OS support | Linux, macOS, Windows |
Container OS | Debian |
Languages, platforms | Python |
Using this definition
This definition creates two containers, one for Python and one for PostgreSQL. VS Code will attach to the Python container, and from within that container the PostgreSQL container will be available on localhost
port 5432. The default database is named postgres
with a user of postgres
whose password is postgres
, and if desired this may be changed in docker-compose.yml
. Data is stored in a volume named postgres-data
.
While the definition itself works unmodified, it uses the mcr.microsoft.com/vscode/devcontainers/python
image which includes git
, eslint
, zsh
, Oh My Zsh!, a non-root vscode
user with sudo
access, and a set of common dependencies and Python tools for development. You can pick a different version of this image by updating the VARIANT
arg in .devcontainer/docker-compose.yml
to pick a Python version.
build:
context: ..
dockerfile: .devcontainer/Dockerfile
args:
# Update 'VARIANT' to pick a version of Python: 3, 3.10, 3.9, 3.8, 3.7, 3.6
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: 3.7-bullseye
You also can connect to PostgreSQL from an external tool when using VS Code by updating .devcontainer/devcontainer.json
as follows:
"forwardPorts": [ "5432" ]
Adding another service
You can add other services to your docker-compose.yml
file as described in Docker's documentation. However, if you want anything running in this service to be available in the container on localhost, or want to forward the service locally, be sure to add this line to the service config:
# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db
Installing Node.js
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. You can change the version of Node.js installed or disable its installation by updating the args
property in .devcontainer/docker-compose.yml
.
args:
VARIANT: 3.7
NODE_VERSION: "14" # Set to "none" to skip Node.js installation
Installing or updating Python utilities
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.
Debug Configuration
Note that only the integrated terminal is supported by the Remote - Containers extension. You may need to modify launch.json
configurations to include the following value if an external console is used.
"console": "integratedTerminal"
Using the forwardPorts property
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 ports
property in docker-compose.yml
publishes rather than forwards the port, this will not work in a Codespace and 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.
[Optional] Building your requirements into the container image
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 rather than the .devcontainer
folder, be sure to include context: ..
under build
in docker-compose.yml
. This allows the Dockerfile to access everything in the opened folder instead of just the contents of the .devcontainer
folder.
[Optional] Allowing the non-root vscode user to pip install globally without sudo
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 )
Adding the definition to your folder
-
If this is your first time using a development container, please see getting started information on setting up Remote-Containers or creating a codespace using GitHub Codespaces.
-
Start VS Code and open your project folder or connect to a codespace.
-
Press F1 select and Add Development Container Configuration Files... command for Remote-Containers or Codespaces.
Note: If needed, you can drag-and-drop the
.devcontainer
folder from this sub-folder in a locally cloned copy of this repository into the VS Code file explorer instead of using the command. -
Select this definition. You may also need to select Show All Definitions... for it to appear.
-
Finally, press F1 and run Remote-Containers: Reopen Folder in Container or Codespaces: Rebuild Container to start using the definition.
Testing the definition
This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps:
- If this is your first time using a development container, please follow the getting started steps to set up your machine.
- Clone this repository.
- Start VS Code, press F1, and select Remote-Containers: Open Folder in Container...
- Select the
containers/python-3-postgres
folder. - After the folder has opened in the container, use ctrl+shift+` to open a terminal and run the following commands to initialize the database and create a super user:
cd test-project pip install --user -r requirements.txt python manage.py migrate python manage.py createsuperuser
- Next, press F5 to start the project.
- Once the project is running, press F1 and select Remote-Containers: Forward Port...
- Select port 5000 and click the "Open Browser" button in the notification that appears.
- You should see a page with a message indicating the install was successful. You can also go to
http://localhost:<port>/admin
and sign in. - From here, you can add breakpoints or edit the contents of the
test-project
folder to do further testing.
License
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. See LICENSE