# Contents

Creating a development container involves setting up a standardized and reproducible development environment that can run consistently across different workstations or CI/CD environments. This short tutorial will guide you through the process of creating a basic development container using commonly used tools like Docker and Visual Studio Code (VS Code).

Prerequisites

  • Docker installed and running on your machine

  • Visual Studio Code with the Remote - Containers extension installed

  • Basic familiarity with Docker and VS Code

Step 1: Create a Dockerfile

A Dockerfile specifies the instructions to create the image for your development environment.

Create a new directory for your project and navigate into it:

1mkdir my-dev-container && cd my-dev-container

Inside the directory, create a file named Dockerfile with the following content:

1FROM ubuntu:20.04
2
3# Avoid warnings by switching to noninteractive
4ENV DEBIAN_FRONTEND=noninteractive
5
6# Use the default user 'developer'
7ARG USERNAME=developer
8ARG USER_UID=1001
9ARG USER_GID=$USER_UID
10
11# Create the user 'developer' with sudo access
12RUN groupadd --gid $USER_GID $USERNAME \
13 && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
14 && apt-get update \
15 && apt-get install -y sudo \
16 && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
17 && chmod 0440 /etc/sudoers.d/$USERNAME
18
19# Install development tools
20RUN apt-get install -y git build-essential
21
22# Switch back to the dialog frontend for any additional apt-get installations
23ENV DEBIAN_FRONTEND=dialog

This Dockerfile creates an Ubuntu 20.04 based image, adds a user developer, and installs common development tools like git and build-essential.

Step 2: Create a devcontainer.json File

The devcontainer.json file describes how VS Code should interact with the development container.

In the same directory, create a file named devcontainer.json with the following content:

1{
2 "name": "My Development Container",
3 "build": {
4 "dockerfile": "Dockerfile",
5 "args": {
6 "USER_UID": "1001",
7 "USER_GID": "1001"
8 }
9 },
10 "remoteUser": "developer"
11}

This file tells VS Code to build the development container image using the Dockerfile in the current directory and set the remote user to developer.

Step 3: Open the Project in VS Code

Open the directory you created in VS Code. Press F1 to open the command palette and select Remote-Containers: Reopen in Container. This will build the Docker image if it's not already built and start a container with your development environment, attaching VS Code to it.

Step 4: Develop Inside the Container

Now you are inside a containerized development environment. You can open a terminal in VS Code, and you'll be interacting with the shell inside the container. Install your project's dependencies, develop your code, and run your applications all within the container.

You can add more tools and dependencies you need for your project by modifying the Dockerfile and rebuilding the container.

Step 5: Maintaining the Dev Container

As you iterate over your dev container, you may find you need to add additional tools or modify settings. You can update the Dockerfile and devcontainer.json as needed, and rebuild your container to apply changes using the Remote-Containers: Rebuild Container command in VS Code.

Remember when you commit your project, include the .devcontainer folder with the devcontainer.json and the Dockerfile. This ensures that anyone who clones the repository can get started with the same environment.

Tags::
  • devcontainer
  • docker
  • code