Docker in VSCode

Docker in VSCode#

VSCode delivers a great support to develop inside of a running docker container.

Prerequisites#

  • VSCode and Docker installed and running

  • Remote - Containers extensions installed within VSCode

Example using Python#

Open the working directory in VSCode. This example starts with a simple python script and a requirements.txt file. You can download this project here

../../_images/docker-vscode-1.png

Create a new folder .devcontainer and a new file within this folder: devcontainer.json

Add the following to devcontainer.json:

{
    "name": "python-dev-env",
    "extensions": [
        "mhutchie.git-graph",
        "donjayamanne.githistory",
        "streetsidesoftware.code-spell-checker",
        "ms-python.python"
    ],

    "settings": [],

    "image": "python:3.10.7-alpine3.15",

    "postCreateCommand": "pip install -r requirements.txt && apk add git"
}

Click: CTRL + SHIFT + P and type: Remote-Containers: Open Folder in Container

../../_images/docker-vscode-2.png

Afterwards the docker container is installed on your computer and all necessary packages are downloaded. The libraries defined in requirements.txt are installed as well. The first time this process it may take a little longer.

../../_images/docker-vscode-3.png

When everything works fine you can see Dev Container: python-dev-env in a green box in the bottom left corner.

As a final step we can check in the terminal if python is installed correctly

python --version

and if we can execute our python example.

python main.py
../../_images/docker-vscode-4.png

Congrats you made your first steps working with Docker in VSCode!