How to Use Docker for Visual Studio Code

An open platform for creating, distributing, and executing applications is called Docker. It enables you to release software more rapidly by separating your applications from your infrastructure. A robust free code editor that works with Windows, macOS, and Linux, Visual Studio Code is accessible online. Docker and Visual Studio Code work together to provide a robust development environment and containerization for standardized and repeatable processes.

The procedures for creating, managing, and deploying containerized apps using Docker with Visual Studio Code will be covered in this post.

Getting Started

Let’s examine the fundamental meaning and salient characteristics of Visual Studio Code and Docker:

Docker

Docker speeds up and streamlines your workflow. You can package your program and its dependencies into a single container by using containerization technology. Containers operate reliably in a variety of settings and are small and lightweight.

  1. Among the fundamental ideas of Docker are images, which are immutable templates that specify the environment and the application.
  2. Docker image instances that are executing programs.
  3. A script with instructions for creating Docker images is called a Dockerfile.

Visual Studio Code

Numerous programming languages and frameworks are supported by the flexible code editor Visual Studio Code. Important characteristics include:

  1. Integrated Terminal: Use the editor to execute commands and scripts in the command line.
  2. Extensions: A wide range of plugins and extensions are available to improve functionality.
  3. Debugging: Effective tools for debugging software.
  4. Version Control: Source control integration with Git.

Setting Up Docker and Visual Studio Code

Before you can use Docker with VS Code, you need to install Docker on your Windows machine. After that open a command prompt and run the command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker --version
docker --version
docker --version

If you don’t already have Visual Studio Code installed refer to our guide on how to install Visual Code on Windows operating system. After that, open Visual Studio Code, you should see the welcome screen.

Install Docker Extension for Visual Studio Code

To integrate Docker with Visual Studio Code, you’ll need to install the Docker extension:

1. In VS Code, click on the Extensions icon in the Activity Bar on the side or press Ctrl+Shift+X.

2. Type “Docker” in the search bar.

docker extension

3. Click the “Install” button next to the Docker extension by Microsoft.

4. After installation, reload VS Code to activate the extension.

Create Dockerized Application

Create a new folder for your project on your local machine. Go to “File” then “Open Folder” and select the newly created folder.

In the project folder, create a new file named Dockerfile with no extension. Add the following content to the Dockerfile. This example uses a Node.js application:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "app.js"]
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
dockerfile

Ensure you save the file after adding the content.

Now, open the integrated terminal in VS Code using “View” and then “Terminal”, make sure you are in the project directory. Run the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker build -t my-node-app .
docker build -t my-node-app .
docker build -t my-node-app .

Run docker images to see your newly created image.

Running and Debugging Dockerized Applications

Use the following command to run the container from the image:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker run -p 3000:3000 my-node-app
docker run -p 3000:3000 my-node-app
docker run -p 3000:3000 my-node-app

Open your web browser and go to http://localhost:3000 to see your running application.

In VS Code, go to “Run” then “Add Configuration” and select Node.js: Attach from the dropdown.

Add the following configuration to .vscode/launch.json:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
]
}
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Docker: Attach to Node", "port": 9229, "address": "localhost", "localRoot": "${workspaceFolder}", "remoteRoot": "/app" } ] }
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Docker: Attach to Node",
            "port": 9229,
            "address": "localhost",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/app"
        }
    ]
}
launch json file

Modify your Dockerfile to enable debugging:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000 9229
CMD ["node", "--inspect=0.0.0.0:9229", "app.js"]
FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 9229 CMD ["node", "--inspect=0.0.0.0:9229", "app.js"]
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000 9229
CMD ["node", "--inspect=0.0.0.0:9229", "app.js"]
dockerfile debug

Rebuild the Docker image with the debugging capabilities:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker build -t my-node-app .
docker build -t my-node-app .
docker build -t my-node-app .

Run the container with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker run -p 3000:3000 -p 9229:9229 my-node-app
docker run -p 3000:3000 -p 9229:9229 my-node-app
docker run -p 3000:3000 -p 9229:9229 my-node-app

Start the debugger in VS Code by going to “Run” then “Start Debugging” or pressing F5.

Conclusion

Using Docker with Visual Studio Code streamlines the development process, ensures consistency across environments, and enhances productivity. By following the steps outlined in this guide, you can create, run, and manage Dockerized applications efficiently. Whether you’re developing a small project or a large-scale application, the combination of Docker and VS Code provides a powerful and flexible development environment.

Take your virtualization journey to the next level with Ultahost’s VM hosting increasing the performance, flexibility, and security of Kernel-based Virtual Machine technology for your business. Host your VMs quickly and with VPS Server offering Windows and Linux virtual machines.

FAQ

What is Docker in Visual Studio Code?
How do I install Docker in VS Code?
Can I debug Docker containers in VS Code?
Do I need Docker installed on my system to use it in VS Code?
How do I start a Docker container in VS Code?
Can I create a Dockerfile in Visual Studio Code?
Is Docker in VS Code free to use?

Related Post

How to List, Start and Stop Docker Container

Docker is a platform that allows you to build, deploy, ...

How to Share Data Between Docker Containers

Docker is a containerization platform that enables us t...

How To Remove Docker Images, Volumes and Cont

Docker is a popular containerization platform that allo...

How to Set Docker Environment Variables

Docker has revolutionized how we build, deploy, and run...

How to Set Up and Use Private Docker Registry

Setting up a private Docker registry can significantly ...

How to Install Visual Studio Code on Linux

Visual Studio Code (VS Code) is a widely used code edit...

Leave a Comment