python:3.9-alpine development container setup with dockerfile for use on Visual Studio.
Report Bug
·
Request Feature
This project serves as an example of how to setup a development container. A Development Container (or Dev Container for short) allows you to use a container as a full-featured development environment. It can be used to run an application, to separate tools, libraries, or runtimes needed for working with a codebase, and to aid in continuous integration and testing. Dev containers can be run locally or remotely, in a private or public cloud, in a variety of supporting tools and editors.
This devcontainer, is built IAW the dev containers specification and tailored for a build environment that runs on alpine linux and python with the cowsay library as an additional dependency for demonstration purposes. Additionally, the devcontainer has git for version control and several extensions installed for Visual Studio Code as development utilities.
To start, you need to have Docker Engine and Docker Compose on your machine. You can either:
- Install Docker Desktop which includes both Docker Engine and Docker Compose
- Install Docker Engine and Docker Compose as standalone binaries
Additionally, this devcontainer is meant to work on Visual Studio Code. You should have it installed along with the remote development pack to enable the IDE's devcontainers functionality.
Create a config.sh
file to host your git configuration settings. This file is run as a postCreateCommand
as specified in .devcontainer.json
. The format of this file should looks as such:
#!/bin/ash
echo "" # add white space for formatting
echo "Configuring Git..."
echo "-----------------------------------------"
# config username and print for confirmation
git config --global user.name "YOUR NAME"
echo -n "User: "
git config --global user.name
# config email and print for confirmation
git config --global user.email "YOUR EMAIL"
echo -n "Email: "
git config --global user.email
echo "" # add white space for formatting
echo "Displaying OS Info..."
echo "-----------------------------------------"
cat /etc/os-release
echo "" # add white space for formatting
All dependencies within your requirements.txt
file will be handled during the build process with Docker.
This project is setup to work with Docker on Visual Studio Code. Once the remote development pack extension is installed, start the development container by running Dev Containers: Rebuild and Reopen In Container
in the command palette. It can be accessed with the keyboard shortcut ctrl + shift + P
on your keyboard. The command shows as follows:
Doing so will start the build process for the devcontainer. Visual Studio will establish a remote connection to the development container with several common python extensions installed in the IDE. Of note when a successful connection is established to the container, the user name and email provided for git configuration is displayed along with information for the alpine-linux OS as shown below:
The src
folder includes a python file, main.py
, that can be run to validate that the cowsay
dependency included in our requirements.txt
file was added correctly.
To validate that requirements were installed correctly, run:
python src/main.py
A little cow from the cowsay library should display with a very special greeting in your console.
Distributed under the MIT License. See LICENSE
for more information.