In this repository we will set up a reproducible and isolated environment for programming using Docker and VSCode. If you need a thorough dive into Docker, I recommend the Docker documentation page. For more details about VSCode, have a look at the VSCode documentation page.
Steps to build the Docker Images:
# For the cpu docker image
docker build -f docker/Dockerfile.cpu --build-arg UBUNTU_VERSION=18.04 --build-arg PYTHON_VERSION=3.7 -t docker_playground_cpu .
# For the gpu docker image
docker build -f docker/Dockerfile.gpu --build-arg CUDA_VERSION=11.2.1 --build-arg UBUNTU_VERSION=18.04 --build-arg PYTHON_VERSION=3.7 -t docker_playground_gpu .
Once you have built a Docker Image, you can run a Docker Container, that uses the Docker Image to create your isolated environment:
# Simple cpu image example
docker run -it \
--mount type=bind,source=$HOME/code,target=/code \
docker_playground_cpu /bin/zsh
# Simple gpu image example. Note that nvidia-docker needs to be installed!
docker run -it \
--mount type=bind,source=$HOME/code,target=/code \
--gpus all \
docker_playground_gpu /bin/zsh
The .devcontainer
folder specifies the arguments needed for VSCode to launch an isolated environment. In particular, the .devcontainer/devcontainer.json
file specifies build arguments for building the Docker Image, the path to the Dockerfile recipe, the run arguments when running a container, and many more. For details you can visit the devcontainer reference page.
The steps to run the container in VSCode are the following:
- Open the VSCode app.
- Install the Remote - Containers VSCode extension (see details here)
- Open the folder containing the .devcontainer folder (
File->Open Folder
). In our case this is${HOME}/code/vscode_docker_playground
. - VSCode should prompt you that the folder contains a devcontainer file and you could run your code inside a Docker Container. Select
Build Image
. In case the prompt does not appear, go toView->Command Palette
and typeRemote-Containers: Open Folder in Container
, pressing Enter to the command. Select the folder that contains the .devcontainer file, and the docker container will now be created for you!