# Contents

As someone with a background in development engineering, I appreciate the value of having the proper resources to efficiently accomplish the work. I have always hated unnecessary complexities of setting up environments. That's why I'm excited to bring you this guide on how to create your first devcontainer.json file.

Following the next steps you can easily and iteratively build the perfect devcontainer.json file for your project, tailored to your unique needs and preferences.

What is a Devcontainer.json File?

Before diving into the step-by-step process, let's define what a devcontainer.json file is and why you want to create one.

At its core, a devcontainer.json file specifies the environment for your VS Code Remote Development environment, including everything from the development language and tools to the runtime.

By creating a devcontainer.json file, you can ensure that your development environment remains consistent across all machines, making it easier to develop, test and debug your applications.

With Daytona's native support for Dev Container, creating a comprehensive development environment with all the necessary tools and libraries has never been easier.

If you're curious about the differences between Devfiles, you can dive deeper into the topic by reading our latest article: "Devfiles and Development Containers: Understanding the Differences".

Step 1: Start Simple

The first step in creating a devcontainer.json is to start with defining the simplest possible app.

To create a Dev Container spec, you need to create a new file named devcontainer.json, which will be located inside the .devcontainer folder.

This super simple Dev Container only requires three fields: name of application, docker-compose.yaml, and workspace folder. From there you can build and customize your own devcontainer.json file.

1{
2 "name": "Simplest complex app",
3 "dockerComposeFile": "docker-compose.yaml",
4 "service": "app",
5 "workspaceFolder": "/workspaces/simplexity"
6}

Once you have successfully created this basic devcontainer.json file, you can start to add in additional configurations and features that make your development process more efficient and effective.

Step 2: Configure Tool-Specific Properties

In addition to specifying the basic environment variables, you can also configure tool-specific properties within your devcontainer.json file. This includes things like which extensions you want to be installed when the container is created and any additional settings that are specific to your development environment.

1"customizations": {
2 "vscode": {
3 "extensions": [
4 "dbaeumer.vscode-eslint",
5 "esbenp.prettier-vscode",
6 "nrwl.angular-console",
7 "astro-build.astro-vscode",
8 "unifiedjs.vscode-mdx"
9 ]
10 }
11},

Note that the example snippet shown above installs several extensions once the container is created.

Step 3: Add in Additional Features

Now that you have the basic configurations and tool-specific properties of your devcontainer.json in place, you can start to add in additional features. There are a number of features that are pre-built, such as common-utils, docker-in-docker, go, and node. You can easily integrate these features into your own devcontainer.json file with a few configuration fields:

1"features": {
2 "ghcr.io/devcontainers/features/common-utils:1": {
3 "installZsh": "true",
4 "username": "codehero",
5 "uid": "1000",
6 "gid": "1000",
7 "upgradePackages": "false"
8 },
9 "ghcr.io/devcontainers/features/docker-in-docker:2.0.0": {
10 "version": "20.10.21",
11 "moby": true,
12 "dockerDashComposeVersion": "v2"
13 },
14 "ghcr.io/devcontainers/features/go:1.1.0": {
15 "version": "1.19.3"
16 },
17 "ghcr.io/devcontainers/features/node:1.1.1": {
18 "version": "18.12.1"
19 }
20},

In this example, the devcontainer.json file installs Common Utils, Docker-in-Docker, Go, and Node as additional features.

Step 4: Forward Your Ports

As you continue to build out your devcontainer.json file, you may find that you need to add more port forwarding. To do this, simply add new entries to the forwardPorts field in your devcontainer.json file, as shown below:

1"forwardPorts": [
2 8080,
3 "pgadmin:80",
4 "rabbitmq:15672",
5 "db:5432"
6]

This specifies that ports 8080, 80, 15672, and 5432 should be exposed within the container.

Step 5: Initialize, Post Create, and Post Start Commands

In addition to configuring the development environment, you can also specify initialization, post-create, and post-start commands that will be run automatically when the container is created or started.

Here's an example of how this looks in a devcontainer.json file:

1"initializeCommand": "test -f .env.local || touch .env.local",
2
3"postCreateCommand": {
4 "swag": "go install github.com/swaggo/swag/cmd/swag@v1.8.10"
5},
6
7"postStartCommand": "yarn",

The initializeCommand specifies a command to run before code runs. The postCreateCommand installs a Swag package for generating documentation, while the postStartCommand installs Yarn.

To take advantage of your updated Dev Container configuration, simply recreate your workspace in Daytona.

Conclusion

By following the steps outlined above, you can use create a fully customized, efficient, and effective devcontainer.json file that meets your unique development needs. Start simple and iterate as needed, and you'll find that you can create the perfect development environment for your projects.

Remember, a well-designed development environment can increase productivity, reduce errors, and make the development process more enjoyable. So don't be afraid to invest some time and effort into building the perfect devcontainer.json file for your projects today.

Bonus Tip: Use of ChatGPT to Clarify or Improve Your Devcontainer

Don't forget that you can use ChatGPT to clarify or improve your Dev Container! Whether you have questions about the syntax or want to explore additional features, ChatGPT can provide guidance and help you create the perfect Devcontainer for your project.

Tags::
  • devcontainer.json