Get Start with Docker with Node.js

2022/03/183 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Requirements
  3. Create Node.js App
  4. Create Dockerfile
  5. Build and Run Docker
  6. Test Result
  7. Conclusion


This blog demo a quick start for if you want to try the docker with node.js.


Before, we get start, you'll need to download the Docker and Node.js, so later we can run docker or npm or node from the local environment.

  • Install the Docker
  • Install the Node.js

Create Node.js App

Here, we'll create a very simple node.js web app, at first, let's run below command.

npm init -y
npm install --save express
nano server.js

These command will first initialize npm repo at local directory, and install the express as web server and try to create a new file server.js.

So in side the server.js we'll put below content.

const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('hello world!')

app.listen(4455, () => {
    console.log('server is up on 4455')

This code will start web server as port 4455 and will return message hello world!.

If you type http://localhost:4455 at your browser, you should see the message hello world!.

Create Dockerfile

Docker can build image and later you can run load and run the image, so Dockerfile is the one has instruction how to build the image.

Create a file Dockerfile then put below content, this will use the Alpine Docker image, then copy all current directory content, then run the npm install and expose the port 4455.

FROM node:8-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD ["node", "server.js"]

Build and Run Docker

The code Node.js is ready and Dockerfile is ready too, now we can use Dockerfile to build image and run it on our local environment.

Run below command at the same directory of the node.js, this'll create a docker image.

docker build -t my-first-docker-app .

if You this, you should be able to see the image you just created.

docker images

Now, run this command then will execute the docker image in a new container with port 4455. the -p is the --publish which publish the container's port to the host.

docker run -p 4455:4455 {the image id}


Run this command on your local environment, we should seee hello world! from the node.ja app.

curl http://localhost:4455

Also, few other command might be useful to use with docker. List all docker images from local.

docker images

List all container.

docker container ls

Stop the container, if you have a website running by the docker container, once stop the container you won't be able to access to it.

docker stop {container ID}

Find all the containers include the one is stop

docker container ls --all
Delete the container
docker container rm {container id}
Delete all the container
docker rm $(docker ps -aq)
Delete all the images
docker rmi $(docker images -q)


Above is an simple steps to create node.js app, include create docker image base on the node.js image, also run the node.js docker image in a container, access from outside of the environment with the port opened, hopefully you like this blog and feel free to reach out if you want to see more of this kind of blogs.