Use Docker containers like a pro

Using Docker containers, you can simplify your development process. With the Docker Visual Studio Code extension, you can make it even easier!

Simplify your dev toolchain in Docker containers

Docker containers offer you a great opportunity to package your toolchain and its dependencies and easily distribute it among everyone in your team. With a preconfigured Docker image, you can cut your onboarding process from hours full of frustration to having people install Docker while drinking a cup of coffee and running a single command. And it’s not just about the distribution of the toolchain.

Using Docker images: is it really that easy?

Using Docker can be intimidating. While you don’t need to understand the whole underlying Docker infrastructure to just use an image, you need to specify the right command to start the right image, expose correct ports, map hostname, mount volumes, etc.

docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material:3.1.0

How to use Docker images like a pro with the Docker Visual Studio Code extension

The Docker Visual Studio Code extension is meant to help you build and manage Docker images run Docker containers. It abstracts away fiddling with the command-line and arguments and allows you to focus on your work instead.

Option 1: Define a task for your container

One way to instruct the Docker extension how to run your container is by defining a task in the .vscode/tasks.json file in your project. The Docker extension introduces a new type of task named docker-run that you can use for running Docker containers.

docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material:3.1.0
{
"version": "2.0.0",
"tasks": [
{
"label": "Run docs container",
"type": "docker-run",
"dockerRun": {
"image": "squidfunk/mkdocs-material:3.1.0",
"ports": [{
"containerPort": 8000,
"hostPort": 8000
}],
"volumes": [{
"localPath": "${workspaceFolder}/docs",
"containerPath": "/docs"
}],
"remove": true
}
}
]
}

Option 2: Customize the Docker run command

Another way to run your Docker container is by selecting the image from the list of images, and from the context menu choosing Run or Run interactive.

{
"docker.commands.runInteractive": [
{
"label": "Default run command",
"template": "docker run --rm -it ${exposedPorts} ${tag}"
},
{
"label": "Run docs container",
"template": "docker run --rm -it ${exposedPorts} -v ${workspaceFolder}/docs:/docs ${tag}",
"match": "squidfunk"
}
]
}

Summary

The Docker extension for Visual Studio Code significantly simplifies working with Docker images and containers in your project. Not only it offers a convenient way of pulling images and running containers but it also does an awesome job in helping you build images for your team. Abstracting away the different commands and arguments, it allows you to focus on your job, saving you precious time.

Cloud Developer Advocate for Microsoft 365 @ Microsoft | https://blog.mastykarz.nl