Running an express server as a docker container

Introduction

Pawan soni
4 min readApr 19, 2021

My goal here in this article is to show how we can have a docker container for our express server, for development purposes, and not for production deployments. I am assuming that you have installed docker in your system and have a little understanding of NodeJs.

This article is divided into two parts, In the first part we are going to create a simple HTTP server, and in the second part, we will be focussing on how we can take our HTTP server in a docker container.

Creating an express server

Create a directory in which you want to reside the project. Open a terminal in that directory and hit the below command.

npm init

You will get some simple questions like the name of the project and description etc, answer them accordingly, then hit the below command.

npm install express --save

It will pull express in the project directory.

Create a file named index.js, it will contain the code to run a simple webserver which we will deploy later as a docker container. Add below lines of code in this file index.js.

const express = require('express');const app = express();const HOST = 3000;app.get('/', (req, res) => {    res.status(200).send({body: 'Hi From Docker', statusCode: 200});});app.listen(HOST, (err) => {    if (!err) console.log(`listening on ${HOST}`);    else console.log('Error', JSON.stringify(err));});

Now hit the below command in the terminal to start the server.

node index.js

After that, you should be seeing the below output in the terminal.

listening on 3000

And if you make a get request to http://localhost:3000, you should see the below output returned.

Making a get request in Postman, A browser can also be used to make this request.

So far now, We have successfully created a web server, cheers.

Creating the Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession. Docker can build images automatically by reading the instructions from a Dockerfile.

We will use this Dockerfile to tell the docker how we want our image to be built, If you want to explore more about docker, Feel free to refer to their lovely documentation here.

Create a file named Dockerfile in the same directory and feed the below lines in it.

FROM node:10#Specifying Work Directory
WORKDIR /usr/src/app
#Installing dependencies
COPY package*.json ./
RUN npm install
#Bundling App Source
COPY . .
#Exposing the port to be mapped
EXPOSE 3000
CMD [ "node", "index.js" ]

Creating the image

Hit the below command in the terminal to create an image for our server.

command: sudo docker build -t <userName>/<application-name> .example: sudo docker build -t pwnsoni/docker-hello-world .

You should see something similar output in the terminal if all goes well.

sudo docker build -t pwnsoni/docker-hello-world .

Running the container

Now, Hit the below command in the terminal to run the container and map a public port of the local system with the private port of the container.

command : sudo docker run -p 49160:3000 -d <userName>/<application-name>example: sudo docker run -p 49160:3000 -d pwnsoni/docker-hello-world

The above command will map the local system’s port 49160 port with exposed port 3000 of the container, and we will be able to make requests to the server and the container will respond to us. You should see something similar to the below output in your terminal.

sudo docker run -p 49160:3000 -d pwnsoni/docker-hello-world

If you hit the below command to list the running docker containers.

sudo docker ps

You should see something similar to the below output.

And if you make a get request to http://localhost:49160, you should see the below output.

Making a get request in Postman, A browser can also be used to make this request.

So at last, we have created a docker container for our simple express server.

Conclusion

This whole code can be found at this GitHub repo (https://github.com/pwnsoni/dockerize-node-app). In this article, I have tried my best to present things as accurately as possible to the best of my knowledge. However, if you have any questions or suggestions, I’ll be more than happy to take them.

--

--

Pawan soni
0 Followers

Swift learner || Developer || JavaScript || NodeJS || Cloud Services || AWS || Lambda || Cloud Solutions