software engineer, consultant, conference speaker, #tech4good, #stacktivism

Dev Containers for Python and Django (VIDEO)

DevContainers_cover.png

Dev Containers is an open specification that allows you to isolate and share your development environment with Docker. Now that you can Dockerize your dev, your devs no longer shrug "It works on my machine."

Why Dev Containers?

Dev Containers let you use a docker container as a full featured development environment.

Docker with process chart development, test, preproduction, production. Dev Container surrounding the first three parts of the process and Production around the last.
We're all familiar with the problem of an application failing to run correctly when moved from one environment to another. The "it works on my machine" is a menace to collaborative development.

Containers allows developers to deploy applications seamlessly across environments. Just like using virtual environments to separate dependencies, we’re leveraging features of docker to simplify and accelerate your workflow. This allows a level of flexibility pinning of discrete versions between projects that non-Pythonistas are not used to. To get this level of granularity and control over package versions – we usually use virtual environments.

Dev Containers can replace the need for a virtual environment but doesn't necessarily have to. Depending on how you develop and what you're using this development environment for, you're free to use virtual environments as a "turducken" of dependency separation.

Uses cases “Sharing dependencies…”

  • Teaching
  • New project or language playground
  • Onboarding new developers to a team
  • Reviewing PRs

Today we're talking about Dev Containers, but this is same technology in codespaces, that will provide cloud computing as well.

Some Docker fundamentals

container: A self-contained unit where application code is packaged with its libraries and dependencies, runtime and system tools. container image: Container images become containers at runtime. This is the container as an executable. Container registry: Repository or collection of repositories used to store and access container images.

To learn more about docker, check out the Docker overview.

Your workspace files are mounted from your localfile system or cloned into the container.

Dev Containers proces and an arrow to the first part of the process and it has an arrow to the local file. Your file system is mounted then cloned into your development container.

Because your extension is installed inside the container it has full access to the tools, platform and filesystem. The experience I’m showing is assuming you’re creating a development environment, but you can also attach this experience to an already running container.

It also forwards your ports 8000, 8080, 5432. Expose ports using `forwardPorts` in `devcontainers.json`

Expose posed specified in your dev containers along with other configurations in your devcontainers.json.

Getting started

Two screen caps from the extensions on the market place. There is Dev Containers or remote development.

If you are using WSL 2 on Windows, to ensure the WSL 2 back-end is enabled: Right-click on the Docker taskbar item and select Settings. Check Use the WSL 2 based engine and verify your distribution is enabled under Resources > WSL Integration.

When not using the WSL 2 back-end, right-click on the Docker task bar item, select Settings and update Resources > File Sharing with any locations your source code is kept. See tips and tricks for troubleshooting.

Copy or Create

Depending on your workflow you can start with a dev container, you can open up an existing directory in a container or if you want an isolated copy of a repo (for example to investigate a PR without impacting your local work) you can do that.

You can use an image as a starting point for your devcontainer.json.

devcontainer_dot_json.png

The dev container configuration is either located under .devcontainer/devcontainer.json or stored as .devcontainer.json in the root of your project.

📹 Create

📹 Reopen in Container

From the VS Code GUI, you’ll end up with a template file that looks something like this. Depending on the settings you select, it may automatically set you up with a DockerFile or docker-compose.yml. Using the Python3 and Postgres Default container will automatically have the docker-file.yml and DockerFile pointed to and give you a .env file *inside* your .devcontainer

Create a new devcontainer.json empty file

Resources for starting your Dev Container

📹 Adding Extensions to Dev Containers

settings.json

The devcontainer.json directory will specify declarative

devcontainer.json
    "dockerComposeFile": [
        "docker-compose.yml" // development docker-compose
        // "../docker-compose.yml" // production docker-compose
    ],
Separate development and production docker-compose.yml
devcontainer.json
    // Uncomment the next line if you want to keep your containers running after VS Code shuts down.
    "shutdownAction": "stopCompose",
shutdownAction will stop your container leaving VS Code

docker-compose.yml and DockerFile

docker-compose.yml

docker_file_yaml.png

DockerFile

DockerFile_pic.png
devcontainers.json
    "portsAttributes": {
        "8000": {"label": "Django port", "onAutoForward": "notify"},
        "5432": {"label": "PostgreSQL port", "onAutoForward": "silent"}
    },
Specify how noisey your ports are with portsAttributes
devcontainers.json
    // Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip install -r indymeet/requirements/requirements-dev.txt",
After the container is created…

Extensions Show & Tell

Python Extension Pack with Python, Jinja, Python Indent, IntelliCode, Django, AutoDocstring, Python Environment Manager
.
.
Ruff, Data Wrangler, SQLTools, SQLTools PostreSQL/Cockroach Driver, Jupyter

Sharing your Dev Container + other settings

Dev Containers remove the “it works on my machine”

  • Commit your .devcontainer.json or .devcontainer directory
  • Commit your .vscode directory because it contains your workspace settings
  • Open in CodeSpaces, which uses Dev Containers, but has added portability because it does not need Docker Desktop
  • You can share running demo of your code with a temporary URL with CodeSpaces
  • Use the LiveShare extension to pair in real time
  • Export your profile and it will be a special vs code file or export it to github to open with vscode.dev

📹 Opening in Codespaces

Sharing your Dev Container + other settings

Preview running servers with share links on the ports tab:

codespaces_ports.png
DevContainer_ports.png

VS Code Profiles

Summary

  • A few clicks and you can switch from docker to local for your dev environment
  • Sharing settings and work in progress has never been easier
  • Customize your extensions and save them User settings, Workspace Settings, Profile settings or your Dev Container.
  • A lot of the extensions we didn’t have a chance to talk about today are feature rich, turning your text editor to an IDE.

The Python Pulse

every 2nd Friday 11am PT / 7pm UTC

python_pulse.png
a monthly Microsoft livestream opening up the stream with the latest and greatest in Python at Microsoft news, then I introduce guests from across the tech ecosystem to chat cloud and Python news, share demos, answer questions and share insights.

The VS Code Python team has created an extension template.

https://aka.ms/python-pulse-live