In a previous blog, I wrote on what is docker and the basic commands of docker. If you have not read then please read my previous blog [1]. This blog is basically on Dockerfile Tutorial and Dockerfile Example . In this blog, you will learn how to build a custom docker image using a Dockerfile and we will even run a simple node application with custom docker image. There are two different ways to build a docker image. If you are using a custom docker image then it is going to be a little complicated which is going to involve Dockerfile.

But if you are using a normal docker image or an image that is already existing then it’s going to be pretty simple. You just need to use the pull command to pull the docker image from the docker hub and then you will just execute a particular docker image and then you will have a container running.
If you want to build your own image then we need to write the first Dockerfile and in Dockerfile we will specify the base image and you need to specify what libraries and what packages you have to install in that particular docker image. So they will make a stack of images build on top of each other and that will be one final docker image.
Dockerfile Tutorial
How to build and use Docker images?
Dockerfile is a blueprint for building the images. It consists of series commands which overall makes the script and helps in create docker image. We basically pull the base image and on top of the base image, we write series of commands and arguments in order to create a new one.
Dockerfile Syntax
- Comment :
# Print "Welcome to InfoHubBlog!"
- Commands + Arguement :
RUN echo "WELCOME TO INFOHUBBLOG"
COMMANDS OF Dockerfile
FROM
The FROM keyword is used to define the base image, on which we will be building. The image on which you want to make changes.
SYNTAX : FROM <Base Image>
FROM ubuntu
ADD
The ADD Keyword is used to add files to the container being built.
SYNTAX : ADD <source> <destination in container>
ADD /var/www/html
RUN
The RUN command is used to add layers to the base image, by installing components. Each RUN statement, adds a new layer to the docker image.
SYNTAX : RUN <command>
RUN apt-get update RUN apt-get -y install apache2
CMD
The CMD keyword is used to run commands at the start of the container. These commands run only when there is no argument specified while running the container.
CMD apachectl -D FOREGROUND
ENTRYPOINT
The EntryPoint Keyword is used to strictly run commands the moment the container initializes. The difference between CMD and ENTRYPOINT is, ENTRYPOINT will run irrespective of the fact whether the argument is specified or not.
ENTRYPOINT apachectl -D FOREGROUND
ENV
The ENV Keyword is used to define environment variables in the container run time.
SYNTAX : ENV <name> <value>
ENV name InfoHubBlog
WORKDIR
The WORKDIR directive is used to set where the command defined with CMD to be executed.
SYNTAX: WORKDIR /path WORKDIR ~/
EXPOSE
The Expose Command is used to associate a specified port to enable networking between the running process inside the container and the outside world(i.e. the host)
SYNTAX : EXPOST <PORT>
MAINTAINER
This non-executing command declares the author, hence setting the author field of the images. It should come nonetheless after FROM.
SYNTAX: MAINTAINER <author_name>
Dockerfile EXAMPLE
Let’s build a sample application in the node app and try to run on docker by creating custom Docker images.
1.Create a package.json file using the text below.
{ "name": "docker_node_application", "version": "1.0.0", "description": "Node.js on Docker", "author": "Durgesh Kashyap <[email protected]>", "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "express": "^4.16.1" } }
2. In order to ignore node_modules or any other file, you can simple specify those file in .dockerignore
node_modules npm-debug.log
3. Let’s build a sample server.js file using the below text which simple print “Welcome to InfoHubBlog”
'use strict'; const express = require('express'); const PORT = 8080; const HOST = '0.0.0.0'; const app = express(); app.get('/', (req, res) => { res.send('Welcome to InfoHubBlog.com'); }); app.listen(PORT, HOST); console.log(`Running on http://${HOST}:${PORT}`);4.Create DockerFile with below text
4. Create Dockerfile with below text
FROM node:14 WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD ["node","server.js"]
5. Now Inorder to build the node application you can run the below command
SYNTAX : docker build . -t <your username>/nodewebapp

5. After Build is successful you can see images using the command docker images

5. In order to run docker image you can run the below command
docker run -p 49160:8080 -d kashyapdurgesh338/nodeapp

Now you will be able to see docker images running using docker ps

Inorder to check node application you can simply hit curl request and then you will get response back.


If you simply want to push to docker hub then you can do using docker push
docker push kashyapdurgesh338/nodewebapp:1.0.1
Hope you Like this. Please subscribe to Blog for getting an upcoming blog. This is all in this Blog. If you want to read about Kubernetes the Please visit [2][3][4].
Reference :
Leave a comment