You are on page 1of 15

Department of Information Technology

COURSE CODE: DITL803 DATE: 23-02-2022


COURSE NAME: DevOps Laboratory CLASS: BEIT

Sap Id: 60003180008


EXPERIMENT NO. 5

LO: LO4. To understand Docker to build, ship and run containerized images

AIM / OBJECTIVE: To understand Docker file and Images.

THEORY:

What is a Docker image?


A Docker image is a file used to execute code in a Docker container. Docker images act as a set of
instructions to build a Docker container, like a template. Docker images also act as the starting point
when using Docker. An image is comparable to a snapshot in virtual machine (VM) environments.

Docker is used to create, run and deploy applications in containers. A Docker image contains
application code, libraries, tools, dependencies and other files needed to make an application run.
When a user runs an image, it can become one or many instances of a container.

Docker images have multiple layers, each one originates from the previous layer but is different
from it. The layers speed up Docker builds while increasing reusability and decreasing disk use.
Image layers are also read-only files. Once a container is created, a writable layer is added on top of
the unchangeable images, allowing a user to make changes.

References to disk space in Docker images and containers can be confusing. It's important to
distinguish between size and virtual size. Size refers to the disk space that the writable layer of a
container uses, while virtual size is the disk space used for the container and the writeable layer. The
read-only layers of an image can be shared between any container started from the same image.

Docker image use cases


A Docker image has everything needed to run a containerized application, including code, config
files, environment variables, libraries and runtimes. When the image is deployed to a Docker
environment, it can be executed as a Docker container. The docker run command creates a container
from a specific image.

Anatomy of a Docker image


A Docker image has many layers, and each image includes everything needed to configure a
container environment -- system libraries, tools, dependencies and other files. Some of the parts of
an image include:

Base image. The user can build this first layer entirely from scratch with the build command.
Parent image. As an alternative to a base image, a parent image can be the first layer in a Docker
image. It is a reused image that serves as a foundation for all other layers.
Layers. Layers are added to the base image, using code that will enable it to run in a container. Each
layer of a Docker image is viewable under /var/lib/docker/aufs/diff, or via the Docker history
Department of Information Technology
command in the command-line interface (CLI). Docker's default status is to show all top-layer
images, including repository, tags and file sizes. Intermediate layers are cached, making top layers
easier to view. Docker has storage drives that handle the management of image layer contents.
Container layer. A Docker image not only creates a new container, but also a writable or container
layer. This layer hosts changes made to the running container and stores newly written and deleted
files, as well as changes to existing files. This layer is also used to customize containers.
Docker manifest. This part of the Docker image is an additional file. It uses JSON format to describe
the image, using information such as image tags and digital signature.
An example of a docker image.

How a Docker image might look to run an Apache HTTPD Server.


Docker image repositories
Docker images get stored in private or public repositories, such as those in the Docker Hub cloud
registry service, from which users can deploy containers and test and share images. Docker Hub's
Docker Trusted Registry also provides image management and access control capabilities.

Official images are ones Docker produces, while community images are images Docker users create.
CoScale agent is an official Docker image that monitors Docker applications. Datadog/docker-dd-
agent, a Docker container for agents in the Datadog log management program, is an example of a
community Docker image.

Users can also create new images from existing ones and use the docker push command to upload
custom images to the Docker Hub. To ensure the quality of community images, Docker provides
feedback to authors prior to publishing. Once the image is published, the author is responsible for
updates. Authors must be cautious when sourcing an image from another party because attackers can
gain access to a system through copycat images designed to trick a user into thinking they are from a
trusted source.
Department of Information Technology
The concept of a latest image may also cause confusion. Docker images tagged with ":latest" are not
necessarily the latest in an ordinary sense. The latest tag does not refer to the most recently pushed
version of an image; it is simply a default tag.

How to create a Docker image


Docker images can be created through either an interactive or Dockerfile method.

Interactive method
With this method, users run a container from an existing Docker image and manually make any
needed changes to the environment before saving the image. The interactive method is the easiest
way to create docker images. The first step is to launch Docker and open a terminal session. Then
use the Docker run command image_name:tag_name. This starts a shell session with the container
that was launched from the image. If the tag name is omitted, Docker uses the most recent version of
the image. After this, the image should appear listed in results.

Dockerfile method
This approach requires making a plain text Dockerfile. The Dockerfile makes the specifications for
creating an image. This process is more difficult and time-consuming, but it does well in continuous
delivery environments. The method includes creating the Dockerfile and adding the commands
needed for the image. Once the Dockerfile is started, the user sets up a .dockerignore file to exclude
any files not needed for the final build. The .dockerignore file is in the root directory. Next, the
Docker build command is used to create a Docker image and an image name and tag are set. Lastly,
the Docker images command is used to see the created image.

Docker image commands


According to Docker, there are sets of primary Docker image commands, categorized as child
commands; some include the following:

docker image build. Builds an image from a Dockerfile.


docker image inspect. Displays information on one or more images.
docker image load. Loads an image from a tar archive or streams for receiving or reading input
(STDIN).
docker image prune. Removes unused images.
docker image pull. Pulls an image or a repository from a registry.
docker image push. Pushes an image or a repository to a registry.
docker image rm. Removes one or more images.
docker image save. Saves one or more images to a tar archive (streamed to STDOUT by default).
docker image tag. Creates a tag TARGET_IMAGE that refers to SOURCE_IMAGE.
The Docker CLI provides commands that are used to customize Docker images. Examples of
Docker image commands include the following:

docker image history. Shows the history of an image, including changes made to it and its layers.
docker update. Enables a user to update the configuration of containers.
docker tag. Creates a tag, such as TARGET_IMAGE, which enables users to group and organize
container images.
docker search. Looks in Docker Hub for whatever the user needs.
docker save. Enables a user to save images to an archive.
docker compose. Used to handle an environment variable.
Department of Information Technology
IMPLEMENTATION AND RESULTS:

1. How many images are available on this host?

docker images
9

2. What is the size of the ubuntu image?

docker image
72.7MB

3. We just pulled a new image. What is the tag on the newly pulled NGINX image?

docker image
1.14-alpine
Department of Information Technology
4. We just downloaded the code of an application. What is the base image used in the
Dockerfile? Inspect the Dockerfile in the webapp-color directory.

vi /root/webapp-color/Dockerfile
python:3.6
Department of Information Technology
5. To what location within the container is the application code copied to during a Docker build?
Inspect the Dockerfile in the webapp-color directory.

vi /root/webapp-color/Dockerfile
/opt
Department of Information Technology
6. When a container is created using the image built with this Dockerfile, what is the command
used to RUN the application inside it. Inspect the Dockerfile in the webapp-color directory.

vi /root/webapp-color/Dockerfile
python app.py
Department of Information Technology
7. What port is the web application run within the container? Inspect the Dockerfile in the
webapp-color directory.

vi /root/webapp-color/Dockerfile
8080
Department of Information Technology
8. Build a docker image using the Dockerfile and name it webapp-color. No tag to be specified.

Move to the directory first by using the cd command and verify the path of the working
directory from pwd command :-

$ cd /root/webapp-color/
$ pwd
/root/webapp-color

Now, run the docker build command within that directory :-

$ docker build -t webapp-color .


Department of Information Technology

9. Run an instance of the image webapp-color and publish port 8080 on the container to 8282 on
the host.

docker run -p 8282:8080 webapp-color


Department of Information Technology

10. What is the base Operating System used by the python:3.6 image? If required, run an instance
of the image to figure it out.
docker run python:3.6 cat /etc/*release*
debian
Department of Information Technology
11. What is the approximate size of the webapp-color image?

docker image
913MB

12. Build a new smaller docker image by modifying the same Dockerfile and name it webapp-
color and tag it lite.

docker build -t webapp-color:lite .


Department of Information Technology
13.

14. Build a new smaller docker image by modifying the same Docker and name it webapp-color
and tag it lite.
Department of Information Technology

15. Run an instance of the new image webapp-color:lite and publish port 8080 on the container to
8383 on the host.

docker run -p 8383:8080 webapp-color:lite


Department of Information Technology

Conclusion:-
Thus we have successfully understood docker files and images.

You might also like