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”.

I’ve created a nodejs project – https://github.com/modisanketp/nodejs-helloworld – you may use the same project to dockerise. You can also create your own project (nodejs is preferred).

nodejs-helloworld

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 !

One thought on “Containerisation – Create your first Docker Image

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