Containerisation – Create your first Docker Image

Hello Everyone,

Hope you’re doing good. During previous article we installed docker and executed hello-world using different commands. In this article, we’ll dockerise our first project which prints “hello-world”. For other related articles please refer index page.

I’ve created a nodejs project – – you may use the same project to dockerise. You can also create your own project (nodejs is preferred).


This is simple web project created using express framework, which prints hello-world. I’ve created a get service on root path which accepts the request and responds “hello-world” string. In order to execute this code, please checkout the project in your machine and execute npm install and node app.js commands. Once execution starts you can access it using http://localhost:3000.

Dockerise nodejs-helloworld project

Now let’s dockerise this project. First of all let’s create a Dockerfile in root directory i.e. nodejs-helloworld directory.

touch Dockerfile

now let’s modify this docker file and add below instructions.

FROM node:12
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD [“node”, “app.js”]

Too many new instructions !! no problem, let’s understand each line.

  1. FROM – This is a base image for my docker file. Base image is also a docker image which can be reused to build your project on top of it. Image name has two sections node:12
    • Image name – in our case it is node, this is unique name of image.
    • Version – version is separated by colon, in our case it is 12
  2. WORKDIR – This instruction informs docker engine that upcoming commands needs to be executed at given location.
  3. COPY – As name suggests, it copies given file / folder to our image. In our case, I’m copying all files / folders from current directory.
  4. RUN – This instruction executes the given command just like our terminal.
  5. CMD – This is very important instruction as it is a trigger point of our docker image. This command will be executed by docker engine when we execute this image.

Now, let’s build this docker project and create our image from this Dockerfile. Execute below command in the same directory.

docker build -t nodejs-helloworld . / docker build -t nodejs-helloworld:v1 .

This command will create a image using Dockerfile from your current directory and will name / tag it as nodejs-helloworld. if you do not specify version it will be by default latest. So if you want to give some specific version to it then you need to append the same to image name. Output of the command will be something like below,

Execution of command for the first time.
Subsequent execution of the command

As explained in previous article, first time execution will download the base image and then execute instructions but subsequent execution will use the downloaded image. You see docker logging few set of details after each step, we’ll understand it in detail in another article. Once execution completes, you can list the images in local,

docker images

Docker images in local.
Docker images in local

Our image size is 919 MB !! let’s ignore this as of now, by the end of this learning, we’ll understand to create small size images. Now let’s execute our container,

docker run -p 3000:3000 nodejs-helloworld /
docker run –publish 3000:3000 -d nodejs-helloworld:v1

Docker run command executes the given image and -p / –publish binds local port to container’s port hence all request coming to localhost:3000 will be forwarded to docker container’s 3000 port. Let’s quickly access http://localhost:3000,

Execution of project from container..
Execution of project from Container.

Yey ! It worked !! Let’s quickly summarise our learning.

  • Downloaded / created a node project which prints helloworld.
  • Created our Dockerfile which had set of instructions.
  • Executed docker build command to create image.
  • Executed docker run command to start our container.

While following any step if you face any difficulty feel free to either get in touch with me or comment. Stay Safe and keep learning ! For other related articles please refer index page.

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

4 Responses

  1. February 3, 2021

    […] you’re doing well. In previous article we converted our node project to dockerise one. We saw that while creating docker image, there were […]

  2. February 6, 2021

    […] well and utilising this important time of your life in learning new things. In one of the previous article we learned how to create a Docker file, now let’s deep dive into each available instructions […]

  3. February 10, 2021

    […] Create your first Docker Image […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: