I have a directory ‘hello-docker’ as our project.

And I’ll create a file ‘app.js’ in it with following cods.

console.log("Hello Docker!");

Now, let’s write our Dockerfile, which contains the instructions for packaging our application.

Typically, we start from a base image. This base image has a bunch of files. We are going to take those files and add additional files to it. This is kind of like inheritance in programming.

We can start from a linux image and then install node on top of it. Or, we can start a node image. This image is already built on top of linux.

How do I know these names? These images are officially published on dockerhub. Go to dockerhub and search on it. You can see the official node image here.

Now back to our dockerfile. If you look at docker hub, you will see that there are multiple node images. These node images are built on top of different distributions of linux.

Now, we can specify a tag using a column to specify which linux distribution we want to use. For this project, we choose alpine, which is a very small distribution.

FROM node:alpine

Then we need to copy our application or program files. We will copy all files in the current directory into the app directory into that image.

COPY . /app

So that image has a file system and in that system we are going to create a directory called app.

Finally, we are going to use the command instruction to execute a command.

WORKDIR /app
CMD node app.js

These instructions clearly document our deployment process.

Our dockerfile looks like this:

Now, we go to the terminal to tell docker to package up our application.

docker build -t hello-docker .

After a few seconds, you can see it’s finished. But you cannot find the image in your local directory.

To see all the docker images on this computer, we type these codes in the terminal:

docker images

So, have a look. Here it is.

Now we can run this image on any computer running docker. On my machine, I can type”

docker run hello-docker

Here is the result:

We can go ahead and publish this image to docker hub so that it can be used on any machine.

docker pull yourDockerHubID/repositoryName


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *