How to dockerize nodejs app
Step 1: Install Docker
To begin with, it’s required to have Docker on your computer. You can download Docker Desktop from the below link and once it’s downloaded and installed successfully, launch the Docker Desktop on your computer.
Step 2: Create a containerised application
The next step is to containerise your application using Docker. To containerise your application you need to have a Dockerfile that is written in YAML. Let’s create a Dockerfile using node as the base image.
I created a simple Node.js application and my working directory contains the below files.
hello-world
- app.js
- package-lock.json
- package.json
Then I need to create a Dockerfile inside this directory.
FROM node:10.23-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "app.js"]
After you add the Dockerfile, you can create the docker image for your application. Use the below command for that [make sure you’re in the same directory where your Dockerfile resides].
$ docker build . -t <image-name>
Eg:
$ docker build . -t node-app
Step 3: Push your image to a Container Registry
For this demonstration, I am using Docker Hub as the container registry. But you can choose any other container registry as you prefer.
To start with, go to Docker Hub and create a new account. Then sign in and go to Repositories > Create Repository. Add a Name to your repository [you can also add a description to your repository but it’s optional] and you can choose whether it keeps as a public or a private repository [if you’re using a free plan, you can create only one private repository in your account].
Then you need to log in to your Docker Hub account using your terminal. For that, use the below command and provide your credentials where necessary.
$ docker login <container-registry-name> -u <username> -p <password>
This is the general command which you can use to log into your account. But the drawback of using this command is, you have to enter your password in plain text. Therefore, use one of the following commands so that your password will not be visible even on the terminal.
$ docker login <container-registry-name>
// then hit enter & provide your username and password
- OR -
$ docker login <container-registry-name> -u <username>
// then hit enter and provide your password
Since I’m using Docker Hub as the container registry you can use docker login command on the terminal and provide credentials without specifying the container registry name.
Eg: type docker login and hit enter
$ docker login
Username: <username>
Password: <password>
// provide credentials and hit enter
After you get Login Succeeded as the output on your terminal, you can start docker tagging and docker pushing.
docker tag command help you to give a tag to your docker image while docker push command pushes it into the Docker Hub. Let’s see how to use these commands on the terminal.
$ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
SOURCE_IMAGE[:TAG] - you can keep this as your local image
TARGET_IMAGE[:TAG] - <username>/<image-name>:<tag>
Eg:
$ docker tag node-app mikej/node-app:v1.0
The above command will tag your image and next you can use the docker push command as below and it will push your image or a repository to a registry.
$ docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image signing (default true)
Eg:
$ docker push mikej/node-app:v1.0
Top comments (0)