Dockerize Node JS application
Learn how to build a Docker image for a simple NodeJS application and deploy the image (NodeJS application) in a Cocker container.
In this article we will learn:
- Setup a simple NodeJS application using Express
- Create Docker File
- Build Image of the NodeJS application
- Launch the NodeJS application in a Container
Prerequisites:
nodejs and npm:
To check whether node and npm are installed in the system or not, type below commands in the terminal:
node --version
npm --version
If node and npm are installed in the system, it will print their versions. Else, to install, follow the instructions mentioned in
Docker:
To install Docker, follow the instructions mentioned in:
To check if Docker is installed and running, type below command in terminal:
docker --version
This will print the docker version in the terminal.
Setup a simple NodeJS application using Express
Create package.json
Run below command to create package.json with default settings:
npm init --force
Run below command to download express to your package:
[sudo ]npm install express –save
Create index.js file and add below code:
Create Dockerfile
Create a new file in the root folder with name Dockerfile (without any file extension)
Add below code to the Dockerfile:
- FROM: is used to get a Docker Image from docker hub.
- WORKDIR: The workdir is sort of default directory for your application. All the commands will run from that directory.
- COPY: Copies files from current local directory to the WORKDIR
- EXPOSE: This exposes the port defined in index.js for mapping by the docker daemon
- RUN and CMD: These are used to run commands.
For a detailed explanation of comamnds and instructions regarding how to create a Docker file, please refer the official Docker :
Build Image of the NodeJS application
Run below command to create a Docker image of the node application:
docker build -t node-app .
By running the above command in terminal it will create a Docker image in local Docker engine with a name node-app
To see the list of images, run the below command in terminal:
docker images
This will print a list of docker images in terminal.
To remove an image, run:
docker image rm --force node-app
node-app is the Docker image name to be deleted
Run an instance of node-app image in a container
Run below command to launch the node-app image in a container which can be accessed from the browser:
docker run -p 8081:3100 -d node-app
In the above command 3100 port (mentioned in index.js file) is mapped to 8081 which can be used in browser to launch the application in browser window.
To see the list of currently running containers, run below command in terminal:
docker container ls
Now, if the application is launched in browser using http://localhost:8081, it should print Welcome!.
To stop or delete a container, run:
docker rm --force 3d18a675f6e
3d18a675f6e is the currently running container.
Docker Commands
To get a full list of commands, refer Docker documentations:
Source Code
Source code is available at following github location:
Thanks!