Professional Documents
Culture Documents
LO: LO4. To understand Docker to build, ship and run containerized images
THEORY:
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.
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.
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.
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 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:
docker images
9
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
9. Run an instance of the image webapp-color and publish port 8080 on the container to 8282 on
the host.
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.
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.
Conclusion:-
Thus we have successfully understood docker files and images.