Estimate project
← All posts

Using dev containers for Machine Learning

How to configure and work with the VSCode dev container extension during development.

3 min read
Machine LearningMl dockerGPU dev container

These days, Docker enjoys enormous popularity. It is lightweight, portable, and self-contained, which makes it an excellent fit for a microservices architecture. Docker containers simplify building software and isolating the environment, and they can run across many environments with different operating systems and hardware platforms. We could go on about how well containers serve deployment, but let’s step back and look at how they can help with development instead.

Picture a scenario in which you have to build a program on a specific version of the operating system, or you want to test your application across several environments. You could build a docker container using the OS system you need as the base image and run it without detach mode (-d):

docker run -it <IMAGE>

, or attach to a container that is already running with:

docker exec -it [container-id] bash

After that, you write and test your code in a terminal window. Ehhh…, that does not sound appealing. Developing software in a terminal is no longer the kind of development environment we expect in 2024. Can we do better?

Luckily…. Yes, we can 🙂

VSCode Dev container

Microsoft’s VSCode extension Dev Containers proves extremely useful here. With the Dev Containers extension, a Docker container can serve as a fully featured development environment.

Among its other advantages, it lets you:

  • Work with a consistent, reproducible set of tools on the same OS as your deployment environment.
  • Move easily between separate development environments and update them safely without touching your local system.
  • Give new team members or contributors a simple, consistent development environment, so that getting started is easier for them.
  • Try out new technologies or make a copy of a codebase without disturbing your local setup.

You get all of this in the VSCode editor you already know, with the same features and user experience as when working locally, except that the workspace is now isolated inside a container.

Dev container definition

Create a Dockerfile.dev containing your environment setup. Below you can see an example docker file.

FROM nvidia/cuda:11.1.1-cudnn8-runtime-ubuntu20.04

# Install python3.10
RUN : \
   && apt-get update \
   && apt-get install -y git \
   && apt-get install -y --no-install-recommends software-properties-common \
   && add-apt-repository -y ppa:deadsnakes \
   && apt-get install -y --no-install-recommends python3.10-venv \
   && apt-get install libpython3.10-dev -y \
   && apt-get clean \
   && :

# Add env to PATH
RUN python3.10 -m venv /venv
ENV PATH=/venv/bin:$PATH

# Ensures that Python output to stdout/stderr is not buffered: prevents missing information when terminating
ENV PYTHONUNBUFFERED 1

# Update pip
RUN /venv/bin/python3.10 -m pip install pip --upgrade

WORKDIR /usr/src/app

ENTRYPOINT ["tail", "-f", "/dev/null"]

In the root directory of your project, create a .devcontainer folder and put a dev container configuration file called devcontainer.json in it. You can also configure it interactively via ctrl + shift + p -> Dev Containers: Configure Container Features or ctrl + shift + p -> Dev Containers: New Dev Container.

Then fill the devcontainer.json file with a basic setup.

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
   "name": "Existing Dockerfile",
   "build": {
       // Sets the run context to one level up instead of the .devcontainer folder.
       "context": "..",
       // Update the 'dockerfile' property if you aren't using the standard 'Dockerfile' filename.
       "dockerfile": "../Dockerfile.dev"
   },
}

Dev container for Machine Learning

Machine learning projects frequently require access to a GPU. This, too, can be set up in devcontainer.json. Here is a sample configuration of a dev container with an Nvidia GPU, Jupiter notebooks, and Python extensions.

{
   "name": "Existing Dockerfile",
   "build": {
       "context": "..",
       "dockerfile": "../Dockerfile.dev"
   },
   "runArgs": ["--gpus","all"],
   "remoteEnv": {
       "PATH": "${containerEnv:PATH}:/usr/local/cuda/bin",
       "LD_LIBRARY_PATH": "$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64",
       "XLA_FLAGS": "--xla_gpu_cuda_data_dir=/usr/local/cuda"
   },
   // Configure tool-specific properties.
   "customizations": {
       "vscode": {
           "extensions": [
               "ms-python.python",
               "ms-toolsai.jupyter",
               "ms-toolsai.vscode-jupyter-cell-tags",
               "ms-toolsai.jupyter-keymap",
               "ms-toolsai.jupyter-renderers",
               "ms-toolsai.vscode-jupyter-slideshow",
               "ms-python.vscode-pylance"
           ]
       }
   }
}

The extensions listed under “customizations” get installed automatically, so there is no need to have them pre-installed or to install them by hand. Every installed extension behaves exactly as it would running locally on your computer, offering the same functionality and the same user experience.

Running a dev container

To start a dev container, choose ctrl + shift + p -> Dev Containers: Reopen in Container and enjoy working in the VSCode you know well.

Summary

This article showed how to configure and use the VSCode dev container extension for development. The extension gives you the same development experience as working locally on your machine, while also offering all the benefits of isolating an environment in containers. It also performs well in remote environments.