Create your Node.js application image
In this step, you use the Docker extension in Visual Studio Code to add the necessary files to create an image for your app, build the image, and push it to a registry.
If you don't already have an app for this walkthrough, use the app from the Visual Studio Code Node.js tutorial.
Add Docker files
In Visual Studio Code, open the Command Palette (F1), type
add docker files to workspace, then select the Docker: Add Docker files to workspace command.
When prompted, select Node.js for the application type, answer No for Docker compose files, then select the port on which your application listens.
The command creates a
Dockerfilealong with some configuration files for Docker compose and a
VS Code has great support for Docker files. See the Working with Docker in the VS Code documentation to learn about rich language features like smart suggestions, completions, and error detection.
Build a Docker image
Dockerfile describe the environment for your app including the location of the source files and the command to start the app within a container.
Containers versus images: A container is an instance of an image.
Open the Command Palette (F1) and run Docker Images: Build Image to build the image. VS Code uses the Dockerfile in the current folder and gives the image the same name as the current folder.
Once completed, the Terminal panel of Visual Studio Code opens to run the
docker buildcommand. The output also shows each step, or layer, that makes up the app environment.
Once built, the image appears in the DOCKER explorer under Images.
Push the image to a registry
To push the image to a registry, you must first tag it with the registry name. In the DOCKER explorer, right-click the latest image.
In the prompt that follows, complete the tags and press Enter.
By convention, tagging uses the following format:
[registry or username]/[image name]:[tag]
If you're using the Azure Container Registry, your image name would be similar to the following:
If you are using Docker Hub, use your Docker Hub username. For example:
The newly-tagged image appears now in a node under Images that includes the registry name. Expand that node, right-click latest, and select Push.
The Terminal panel shows the
docker pushcommands used for this operation. The target registry is determined by the registry specified in the image name.
Once completed, expand the Registries node in the Docker extension explorer to see your image in the registry.