You are on page 1of 6

Experiment -2.

1
Student Name: Sudhir Yadav UID: 21CDO1041
Branch: AIT – CSE (DevOps) Section/Group: 21BCD – 1(A)
Semester: 5th Date of Performance: 4 Sep, 2023
Subject Name: Docker and Kubernetes Subject Code: 21CSH - 349

1. Aim/Overview of the practical:


To run Node.js application using Docker and manage the Docker volume.

2. Task to be done :
 Use JavaScript for designing dynamic web pages and client side validation.
 Make Docker Account, deploy an application on Docker and install Virtual Docker on
local machine and access files from Docker account

3. Theory

What is a Docker File ?

A script called a Dockerfile gives directions for creating a customized Docker image. The final
image is made up of all the layers stacked on top of one another, and each instruction in a
Dockerfile adds a new layer to the image.
It contains instructions for establishing environment variables, transferring files, installing
dependencies, and customizing the container.

Any text editor can be used to generate and amend Dockerfile because of its straightforward,
understandable syntax. A Dockerfile can be used to construct an image by using the docker
build command after it has been prepared. Using the docker run command, the generated image
can then be used as a container.

Benefits of using Docker File


Applications may be deployed more quickly and effectively thanks to Dockerfiles. A Docker
image can be readily deployed to any environment that accepts Docker after it has been created.

To create and manage test environments for various apps and services, Dockerfiles can be used
in automation testing. Without having to manually set up and configure the environment on
each test run, you may use a Dockerfile to produce an image of a particular test environment
that can be reliably and simply repeated.

A continuous integration and continuous deployment (CI/CD) pipeline, which enables you to
automatically build, test, and deploy your application with every code change, may be simply
integrated with your Dockerfile. This lowers the possibility of errors and guarantees that your
application is always current.

4. Steps for the Experiment:

How to create a dockerfile:


1)Open your preferred IDE (such as VS Code) and navigate to your project directory. Create a
new file in your project directory (at the root level) and name it Dockerfile (no file extension).

2) FROM

The FROM instruction specifies the base image that the container will be built on top of. This
instruction is typically the first one in a Dockerfile and is used to set the base image for the
container. The format of the instruction is:

FROM <image>

3) WORKDIR

In a Dockerfile, the WORKDIR instruction sets the working directory for any command that
follows it in the Dockerfile. This means that any commands that are run in the container will be
executed relative to the specified directory. Below is the format of the instruction :

WORKDIR <directory>
4) COPY

Use the COPY instruction to copy local files from the host machine to the current working
directory. For example, to copy a file named package.json from the host machine’s current
directory to the image’s /app directory, you would use the following command:

COPY package.json /app/


If you want to copy all the files from the host’s current directory to the container’s current
directory, you can use the below command:

COPY . .

5) RUN

Use the “RUN” instruction to execute commands that will run during the image build process.
The format of instruction is :

RUN <command_name>
6) CMD

In a Dockerfile, the CMD instruction sets the command that will be executed when a container
is run from the image. The format of the instruction is:

CMD ["executable","param1","param2",...]

Running a Dockerfile:

After creating a Dockerfile, now let’s explore the process of building a Dockerfile, pushing it to
a registry, and then running it.

Pre-Requisite for push: You need to login ( use command: docker login)

Build Docker Image:


Once you have finished building your Dockerfile, you can build the image by running the below
command from the Terminal :

docker build -t <image_name> .


Once, it is successfully built, you would be able to see FINISHED message just like shown
below in the screenshot:

Verify Docker Image:

Once the image is built, you can check if it got created by using the below command:

docker images
The above command will show you all the docker images.

Push Docker Image:

Once, the docker image is built and now you can push your docker image. You can tag the
image first and then push it or you can push it directly as well. Sometimes, there is an access
denied issue even though you are logged in. In that scenario, you can tag the image first and
then push it.

Make sure you are logged in ( use command: docker login)

Tag the image using the below command:

docker tag imagename:tag username/imagename:tag


Push the image:

Once you tag the image, now you can push the image using the below commands:

docker push username/imagename:tag


OR you can use the below command to push without providing the username.

Syntax:

docker push imagename:tag

Run Docker Image:


In the end, you can run the docker image just to be sure that the image built is working fine. Use
the below command:

docker run <image_name>:<tag_name>

Learning outcomes (What I have learnt):

1. I now know how to create a docker volume


2. I am now familiar with inspecting docker volume.
3. I now know how to how to host a directory while creating a volume.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Maximum Marks


1.
2.
3.

You might also like