Step 1: Download Node.js Code
First, download the Node.js application code by clicking the button below:
Download Node.js CodeStep 2: Install Docker Extensions in VS Code
Download the Docker extension in VS Code from Microsoft. This will install two extensions:
- Docker - For building, managing, and deploying containerized applications
- Container Tools - For working with containers in VS Code
After installing the extensions, open the Node.js program in VS Code.
Step 3: Add Docker Files to Your Project
Follow these steps to add Docker files to your Node.js project:
Open Command Palette
Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac) to open the command palette.
Search for Docker Add
Type "Docker add" and select the option "Docker: Add Docker Files to Workspace..."
Select Application Platform
Choose Node.js since your application is written in Node.js.
Select Package.json Location
Select the root package.json file.
Enter Port Number
Enter 3000 as the port your application runs
on.
Skip Docker Compose
When asked if you want to add a Docker Compose file, select "No".
The extension will generate two Docker
files: .dockerignore and Dockerfile.
Step 4: Build a Docker Image
Now you can build an image using the command palette:
Search for Docker Build
In the command palette, search for "docker build" and select the option "Docker Images: Build Image..."
# You can check the image using: docker images
Step 5: Run Your Docker Image
Run the Image via Command Palette
In the command palette, type "docker run" and select "Docker Images: Run".
Select Image
Select your Docker image named "myexpressapp".
Alternative Method
If the command palette method doesn't work, you can run the image using the Docker extension:
- Click on the Docker extension icon in VS Code
- Go to the "Images" section
- Find your image named "myexpressapp"
- Right-click on the image
- Select "Run" from the context menu
Managing Docker Containers
When you click on the Docker extension icon, you'll see two main sections:
Containers
- Shows running containers
- Shows stopped containers
- Images are listed below the containers
Images
- Shows every image in your local PC
- You can manage images from here
Container Management
Stopping Containers
To stop a running container:
- Go to the Containers section
- Right-click on your container
- Select "Stop" or "Remove"
Starting Containers
To start a container:
- Go to the Images section
- Right-click on the container name
- Select "Start" or "Remove"
Docker Command Quick Reference
| Command | Description |
|---|---|
| docker images | List all Docker images on your system |
| docker ps | List running containers |
| docker ps -a | List all containers (running and stopped) |
| docker stop <container_id> | Stop a running container |
| docker rm <container_id> | Remove a container |
| docker rmi <image_id> | Remove an image |