Docker Lab: What Is Virtualization?
Docker Lab: What Is Virtualization?
Docker Lab: What Is Virtualization?
What is Virtualization?
Virtualization refers to importing a guest operating system on our host operating system,
allowing developers to run multiple OS on different VMs while all of them run on the
same host, thereby eliminating the need to provide extra hardware resources.
Advantages of Virtualization:
VMs are being used in the industry in the following ways:
However, running multiple VMs over the same host leads to degradation in
performance. As guest operating systems have their own kernel, libraries, and many
dependencies running on a single host OS, it takes up a large occupation of resources
such as the processor, hard disk and, especially, its RAM. Also, when we use VMs in
virtualization, the bootup process takes a long time that would affect efficiency in the
case of real-time applications.
Disadvantages of Virtualization:
● Running multiple VMs leads to unstable performance
● Hypervisors are not as efficient as the host operating system
● Boot up process is long and takes time
What is Containerization?
Containerization is a technique where the virtualization is brought to an operating
system level. In containerization, we virtualize the operating system resources. It is
more efficient as there is no guest operating system consuming the host resources;
instead, containers utilize only the host operating system and share relevant libraries
and resources only when they are required. The required binaries and libraries of
containers run on the host kernel leading to faster processing and execution.
In the case of containerization, all containers share the same host operating system.
Multiple containers get created for every type of application making them faster but
without wasting the resources, unlike virtualization where a kernel is required for every
OS and lots of resources from the host OS are utilized.
Advantages of Containers:
● Containers are small and lightweight as they share the same OS kernel.
● They do not take much time to boot-up (only seconds).
● They exhibit high performance with lower resource utilization.
Containerization vs Virtualization
Containerization Virtualization
Lightweight Heavyweight
Docker
Docker is a containerization platform that packages your application and all its
dependencies together in the form of Containers to ensure that your application works
seamlessly in any environment.
Each application will run on a separate container and will have its own set of libraries
and dependencies. This also ensures that there is process level isolation, meaning each
application is independent of other applications, giving developers surety that they can
build applications that will not interfere with one another.
As a developer, one can build a container which has different applications installed on it
and give it to my QA team who will only need to run the container to replicate the
developer environment.
Benefits of Docker:
With docker, the QA team need not install all the dependent software and applications
to test the code and this helps them save lots of time and energy. This also ensures that
the working environment is consistent across all the individuals involved in the process,
starting from development to deployment. The number of systems can be scaled up
easily and the code can be deployed on them effortlessly.
Docker Architecture
Docker uses a client–server architecture. The Docker client consists of Docker build,
Docker pull, and Docker run. The client approaches the Docker daemon that further
helps in building, running, and distributing Docker containers. Docker client and Docker
daemon can be operated on the same system; otherwise, we can connect the Docker
client to the remote Docker daemon. Both communicate with each other using the
REST API, over UNIX sockets or a network.
Docker Client
● It is the primary way for many Docker users to interact with Docker.
● It uses command-line utility or other tools that use Docker API to communicate
with the Docker daemon.
● A Docker client can communicate with more than one Docker daemon.
Docker Host
In Docker host, we have Docker daemon and Docker objects such as containers and
images. First, let’s understand the objects on the Docker host, then we will proceed
toward the functioning of the Docker daemon.
● Docker Image: A Docker image is a Docker object. It is a type of recipe/template
that can be used for creating Docker containers. It includes steps for creating the
necessary software.
● Docker Container: A Docker container is also a Docker object. A type of virtual
machine created from the instructions found within the Docker image. It is a
running instance of a Docker image that consists of the entire package required
to run an application.
● Docker Daemon:
○ Docker daemon helps in listening requests for the Docker API and in
managing Docker objects such as images, containers, volumes, etc.
Daemon issues to build an image based on a user’s input and then saves
it in the registry.
○ In case we don’t want to create an image, then we can simply pull an
image from the Docker hub (which might be built by some other user). In
case we want to create a running instance of our Docker image, then we
need to issue a run command that would create a Docker container.
○ A Docker daemon can communicate with other daemons to manage
Docker services.
Docker Hub is like GitHub for Docker Images. It is basically a cloud registry where you
can find Docker Images uploaded by different communities, also you can develop your
own image and upload on Docker Hub, but first, you need to create an account on
DockerHub.
Dockerfile:
● You specify what to include in your Docker container via a special file which by
convention is called Dockerfile.
● The Dockerfile contains a set of Docker instructions which are executed by the
Docker command line tool. The result is a Docker image
Specifically,
1. Docker Image is created by the sequence of commands written in the Dockerfile.
2. When this Dockerfile is executed using a docker command it results into a
Docker Image with a name.
3. When this Image is executed by “docker run” command it will by itself start
whatever application or service it must start on its execution.
Docker Compose
● The Docker Compose feature enables you to "link" multiple Docker containers
into a single "composition", which can be installed / deployed and started up all at
once.
● Docker Compose is basically used to run multiple Docker Containers as a single
server. Suppose if I have an application which requires WordPress, Maria DB
and PHP MyAdmin. I can create one file which would start both the containers as
a service without the need to start each one separately. It is really useful
especially if you have a microservice architecture.
● For instance, an application in one Docker container, and a database in another
Docker container - in case both Docker containers are necessary for the
application to run.
Docker Installation
● Access docs.docker.com
● Click on Get Docker
● Select Docker for Linux
● Select Ubuntu from menu
To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:
● Ubuntu Eoan 19.10
● Ubuntu Bionic 18.04 (LTS)
● Ubuntu Xenial 16.04 (LTS)
cat /etc/*release*
to cross check dependencies
uninstall older versions: Older versions of Docker were called docker, docker.io, or
docker-engine. Uninstall them. New version is docker-ce
sudo apt-get remove docker docker-engine docker.io containerd runc
To uninstall previously installed docker-ce, use:
sudo apt-get purge docker-ce docker-ce-cli containerd.io
Images, containers, volumes, or customized configuration files on your host are not
automatically removed. To delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker
If you would like to use Docker as a non-root user, you should now consider adding
your user to the “docker” group with something like:
sudo usermod -aG docker your-user
After which you should log out and login for it to take effect
Docker Commands
docker ps
List all running containers (IDs etc.)
As you can see, none of the containers are running at the moment. All stopped after
running. I forced my nginx containers to stop through CTRL+C
Every container has an ID (given by docker), its image name, the command used to run
it, time it was created, its current status (running or exited), assigned ports (if any by the
user) and a name (given by docker)
docker ps -a
Show all prev and current containers
Now, I ran docker run nginx in a separate terminal and checked the running containers:
docker rm name/id
get rid container permanently:
After stopping, the docker will still remain in the history list….
Remove it completely through the name:
docker images
List all the docker images downloaded from registry
docker rmi
You can remove the sensitive system files within the container and exit. It will make a
difference only to this container. You can simply delete this container and start a new
one
Lets deploy a web application with docker:
The --rm flag will automatically remove the container when it exits. Right now, the
container is up and running but we need to specify some more commands to see the
content on website (e.g., ports)
Here, -d will make the website run in background (so I can continue to type commands
at the terminal), -P will assign random ports, and --name will assign a name as we want.
The output confirms the execution
In this case, I want to map the network service port 80 of the container to port 8888 on
my host machine, i.e., whatever should be visible on port 80 inside the container will be
visible on port 8888 on my machine
Cross-check:
Dockerfile example
Create the following Dockerfile with nano (nano Dockerfile) and add the following:
Add the following:
#This is a sample Image
FROM ubuntu
MAINTAINER bdaclass2020
RUN apt-get update
CMD [“echo”,”Image created”]
● http://tutorials.jenkov.com/docker/dockerfile.html
● https://intellipaat.com/blog/tutorial/devops-tutorial/docker-tutorial/
● https://stackify.com/docker-tutorial/
Exercise 1:
Start 3 containers from image that does not automatically exit, such as nginx, detached.
Stop 2 of the containers leaving 1 up.
Submitting the output for docker ps -a is enough to prove this exercise has been done.
Exercise 2:
Exercise 3:
Start image devopsdockeruh/pull_exercise with flags -it like so: docker run -it
devopsdockeruh/pull_exercise. It will wait for your input. Navigate through docker hub to
find the docs and Dockerfile that was used to create the image.
Read the Dockerfile and/or docs to learn what input will get the application to answer a
“secret message”.
Submit the secret message and command(s) given to get it as your answer.
Exercise 4:
Exercise 5:
Start a ubuntu image with the process sh -c 'echo "Input website:"; read website; echo
"Searching.."; sleep 1; curl http://$website;'
You will notice that a few things required for proper execution are missing. Be sure to
remind yourself which flags to use so that the read actually waits for input.
Note also that curl is NOT installed in the container yet. You will have to install it from
inside of the container.
Test inputting h
elsinki.fi into the application. It should respond with something like
<html>
<head>
<title>301 Moved Permanently</title>
</head>
<body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.helsinki.fi/">here</a>.</p>
</body>
</html>
This time return the command you used to start the process and the command(s) you
used to fix the ensuing problems.
This exercise has multiple solutions, if the curl for helsinki.fi works then it’s
done. Can you figure out other (smart) solutions?
Exercise 6:
The developer has poorly documented how the application works. Passing flags will
open different functionalities, but we’d like to create a simplified version of it.
Add a CMD line to the Dockerfile and tag it as “docker-clock” so that docker run
docker-clock starts the application and the clock output.
Return both Dockerfile(s) and the command you used to run the container(s)
Exercise 7:
Make a script file for echo "Input website:"; read website; echo "Searching.."; sleep 1; curl
http://$website; and run it inside the container using CMD. Build the image with tag
“curler”.
Run command docker run [options] curler (with correct flags again, as in 1.5) and input
helsinki.fi into it. Output should match the 1.5 one.
Return both Dockerfile(s) and the command you used to run the container(s)
Exercise 8:
Exercise 9:
Create Dockerfile for an application in any of your own repositories and publish it to
Docker Hub. This can be any project except clones / forks of backend-example or
frontend-example.
For this exercise to be complete you have to provide the link to the project in docker
hub, make sure you at least have a basic description and instructions for how to run the
application in a README that’s available through your submission.
Exercise 10:
Create an image that contains your favorite programming environment in it’s entirety.