How to Integrate and Use Docker in VS Code

Integrate Docker Vscode

Integrating Docker into Visual Studio Code makes development easier and more efficient. You can quickly create Docker files, explore containers and images in a visual panel, and switch to logs or terminals with a single click. In this article, we will explore how to set up Docker in VS Code with the Docker extension.

Installing the Docker Extension in VS Code

Before installing the Docker extension, ensure that Docker and Visual Studio Code are already set up on your system. Open the Extensions view in Visual Studio Code, search for Docker, and install the official Docker extension by Microsoft.

Install Docker Extension

After the installation completes, you’ll see a Docker/Containers logo appear in the lower-left corner of the editor, which serves as the Docker Explorer.

Docker Explorer

Connecting Docker Desktop to VS Code

When Docker Desktop is running, VS Code automatically connects through the Docker extension. You don’t need extra setup unless Docker requires special configuration (like remote hosts or WSL integration).

To confirm the connection, click the Docker/Containers icon to open the Docker Explorer. From here, you can check your running and stopped containers, as well as view images, registries, volumes, and networks.

Verify Docker Integration

Managing Containers, Images, and Volumes from VS Code

Once Docker is properly integrated, you can manage most container tasks directly from VS Code. For instance, you can view running and stopped containers in the CONTAINERS section.

Manage Containers

Moreover, you can right-click on a specific container to start, stop, restart, or remove it.

Start Stop Remove Container

You can also pull images, build new ones, or clean up unused images. For example, to remove an unused image, I can simply right-click on it and select Remove to delete it immediately.

Remove Image

Apart from that, you can right-click on a specific volume linked to your containers to inspect or manage it.

Manage Volume

You can also explore and manage container networks, registries, Docker contexts, and even access the help and feedback section to simplify your workflow.

Build and Run a Containerized App in VS Code

Let’s build a simple Node.js Express app to try out the extension. To do this, first, create a new folder in VS Code for your project, then a file named “index.js”, and add the following code in that file:

const express = require("express");
const server = express();
const PORT = 4000;

server.get("/", (req, res) => {
  res.send("Welcome to our Express app!");
});

server.listen(PORT, () => {
  console.log(`App running on port ${PORT}`);
});

This code creates a small Express app that runs on port 4000. When you open the root URL (/), it shows the message “Welcome to our Express app!“. It’s just a basic setup for Dockerfile testing.

Next, you need to add a Docker file. Normally, adding Docker means creating a Dockerfile, writing the instructions, building an image from the terminal, and then running it manually. With the Docker extension, VS Code can generate these files for you. To do this, open the Command Palette (⇧⌘P on Mac or Ctrl + Shift + P on Windows) and type the command Add Docker files to Workspace.

Add Docker Files To Workspace

Select Node.js as your application platform.

Select Node Js

Select the “package.json” file located in your project root (the same folder as your index.js file). This is the main configuration file for your app, and the Docker extension will use it to generate the necessary Dockerfile and other related files.

Select Package Json File

Specify the port your app runs on for Docker to expose it.

Select Port Number

Select Yes to include the Docker Compose file.

Docker Compose File

The Docker extension will generate a Dockerfile, a “.dockerignore” file, and optionally a “compose.yaml” file if you choose it.

Generate Required Fliles

Finally, to build a Docker image, right-click the Dockerfile and select Build Image or run Docker Images: Build Image in the Command Palette.

Build Docker Image
Verify Built Image

Next, go to the Images section in Docker Explorer to see your newly added project:

Newly Added Project

Now, open the Command Palette, execute the command container images: run or right-click on your image and select Run.

Run Image

From the list of images, choose your latest image.

Choose Image

And finally, you can confirm it’s running by checking the Containers section in Docker Explorer.

Confirm Running Container

To see your app in action, right-click the running container in Docker Explorer and choose Open in Browser.

Open In Browser

From the output, you can see that the container is running and our app is accessible through the specified port.

Output

Wrapping Up

Integrating Docker into VS Code streamlines your development by enabling you to build, run, and manage containers directly within your editor. Once set up, you can easily create Docker files, explore containers and images, and access logs or terminals with a single click. It simplifies running and testing applications and keeps your workflow organized and efficient.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Anees Asghar Avatar

Read next

The Roman aqueduct at Segovia, built around the first century AD without mortar, still carried water into the 1970s, its 167 granite arches held together by nothing but the precise weight distribution of stones cut to fit each other within fractions of a millimeter.
In 1843, Ada Lovelace described a brass-and-punched-card engine that could act on symbols as well as numbers, even composing music if harmony could be reduced to rules, inside seven translator’s notes three times longer than the paper itself
ARPANET sent its first message on 29 October 1969 from a lab at UCLA to a machine at Stanford, and the message was supposed to read ‘LOGIN’ — but the system crashed after the L and the O, meaning the first word ever transmitted over the network that became the internet was, by accident, ‘LO’.
In 1995, Microsoft shipped a cartoon-house interface called Bob, led by Melinda French, who married Bill Gates while it was in development — it demanded twice the memory of a typical home PC, sold roughly 30,000 copies, and was dead within a year, leaving behind the font Comic Sans and the animated assistant that became Clippy.
Apple’s original 1984 Macintosh keyboard had no arrow keys, no function keys, and no numeric pad because Steve Jobs wanted users to reach for the mouse first. Then Apple quietly sold the missing keys as an accessory.
In 1965, Mary Allen Wilkes wrote LAP6 for the LINC computer from her parents’ Baltimore home, testing an interactive operating system on a 250-pound machine in the living room and becoming the first known person to use a personal computer at home, twelve years before the Apple II reached buyers
In 1969, László Bélády and two IBM colleagues published a paging-machine anomaly showing FIFO could make four memory frames suffer ten page faults after three frames suffered nine, leaving generations of operating-systems students staring at the moment more memory became the wrong answer
In 1964, IBM risked its entire corporate empire on the System/360, a chaotic gamble to make all of its future machines compatible with the same software — and the architecture proved so robust that modern enterprise mainframes today are still running sections of binary code written more than sixty years ago