There is no doubt that Containerization has revolutionized development processes. Onboarding new collaborators has never been easier, and switching between Dev machines is almost unnoticeable with container development.

Visual Studio Code Remote-Containers

Visual Studio Code Remote Containers is an extension that you can add to VSCode which allows you to launch Docker containers with the tools you need for your local dev environment. And since it runs from within the IDE, you are not loosing your favorite VSCode features. You can read more in detail about how it works in the official VSCode website here.

Terraform Remote-Container

In order to setup our dev container, let us list what do we need in it:

  • Terraform (obviously)
  • Az-cli ( could be other cli s if you’re using other cloud providers )
  • Azure and Terraform extension in VSCode

We need to add a .devcontainer/ folder to our developpement directory, there we specify the configuration of the container and the IDE after the container starts.

In that folder we would find the devcontainer.json and a Dockerfile

devcontainer.json

{
	"name": "Azure CLI",
	"dockerFile": "Dockerfile",
	
	// Set *default* container specific settings.json values on container create.
	"settings": { 
		"terminal.integrated.shell.linux": "/bin/bash"
	},
	
	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"ms-vscode.azurecli",
		"4ops.terraform"
	],
	
	// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode",
	"build": {
		"args": {
			"UPGRADE_PACKAGES": "true"
		}
	}
}

Dockerfile

# You can pick any Debian/Ubuntu-based image. 😊
FROM mcr.microsoft.com/vscode/devcontainers/base:buster

# [Option] Install zsh
ARG INSTALL_ZSH="true"
# [Option] Upgrade OS packages to their latest versions
ARG UPGRADE_PACKAGES="false"

# Terraform Version
ARG TF_Version="0.14.0"

# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
COPY library-scripts/*.sh /tmp/library-scripts/
RUN bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
    # Install the Azure CLI
    && bash /tmp/library-scripts/azcli-debian.sh \
    # Clean up
    && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts

# Install Terraform
RUN apt-get update && apt-get install -y \
    wget \
    unzip \
  && rm -rf /var/lib/apt/lists/*
RUN wget --quiet https://releases.hashicorp.com/terraform/${TF_Version}/terraform_${TF_Version}_linux_amd64.zip \
  && unzip terraform_${TF_Version}_linux_amd64.zip \
  && mv terraform /usr/bin \
  && rm terraform_${TF_Version}_linux_amd64.zip

We’re all set, this will install the required az-cli and terraform, you can specify the terraform version using the TF_Version argument variable.

All we need to do now is to launch our dev environment. On the bottom left of Visual Studio Code Click on the green button

Then Reopen in Container

You should see the container loading, it is charging the necessary docker images.

Once that is done, congratulations, you have an operational Development Container, with Azure CLI and Terraform ready to go, no further installation of tools or extensions is needed. Container starts with the configuration we previously set in the devcontainer.json file.

If let’s say you have to change machines or use a different machine elsewhere, you save yourself the hustle of re-installing all the tools you need. You can literally have the same dev environment everywhere you go.

Here Terraform is a lightweight tool, but imagine all the possibilities …