CENG301 DBMS Docker Session 4
CENG301 DBMS Docker Session 4
CENG301 DBMS Docker Session 4
Session-4
Asst. Prof. Mustafa YENIAD
myeniad@gmail.com
A Brief Information & What will you achieve?
• An introduction to the container world with Docker
(In simple words, Docker is a container runtime software,
and addtionally, Kubernetes is a container orchestration tool)
• By the end of this session, you will understand the core concepts of Docker, such as
Introduction
• You probably heard someone talking or read some article that cites Docker.
But what is it all about?
Why should I use Docker?
And why is it good for both dev and production systems.
Why you should learn Docker?
• With every organization going towards the Cloud, the container model is going to be
even more critical in the coming years, and Docker and Kubernetes will play a significant
role in deploying and running software from Cloud.
• That's why it is very important for both programmers and DevOps engineers to learn
Introduction
Docker and Kubernetes to do well on their current job and add an in-demand technical
skill.
See: https://www.crunchydata.com/blog/an-easy-recipe-for-creating-a-postgresql-cluster-with-docker-swarm
Docker Concepts
• Docker is a command-line program, a background daemon, and a set of remote
services that take a logistical approach to solving common software problems and
simplifying your experience installing, running, publishing, and removing software.
• Docker provides a uniformed wrapper around a software package:
"Build, Ship and Run Any App, Anywhere" [https://www.docker.com]
• Similar to shipping containers: The container is always the same, regardless of the contents and thus fits on
all trucks, cranes, ships, ...
Docker
• Docker Technology:
libvirt: Platform Virtualization
LXC (LinuX Containers):
Multiple isolated Linux systems
(containers) on a single host
Layered File System
[Source: https://docs.docker.com/terms/layer/]
Remember: Namespace
• In computing, a namespace is a set of symbols that are used to organize objects of various kinds, so that
these objects may be referred to by name. Prominent examples include:
File Systems are namespaces that assign names to files
Some Programming Languages organize their variables and subroutines in namespaces
Computer Networks and Distributed Systems assign names to resources, such as computers, printers,
websites, (remote) files, etc.
• As an analogy, consider a system of naming of people where each person has a proper name, as well as a
family name shared with their relatives. If the first names of family members are unique only within each
Docker
family, then each person can be uniquely identified by the combination of first name and family name.
In a similar way, hierarchical file systems organize files in directories. Each directory is a separate
namespace, so that the directories "letters" and "invoices" may both contain a file "to_jane".
In computer programming, namespaces are typically employed for the purpose of grouping symbols and
identifiers around a particular functionality and to avoid name collisions between multiple identifiers that
share the same name.
In networking, the Domain Name System organizes websites (and other resources) into hierarchical
namespaces.
Additional Information - Linux Namespaces
• Linux Namespaces are a feature of the Linux kernel that partitions kernel resources such that one set of
processes sees one set of resources while another set of processes sees a different set of resources.
• The feature works by having the same name space for these resources in the various sets of processes, but
those names referring to distinct resources. Examples of resource names that can exist in multiple spaces, so
that the named resources are partitioned, are process IDs, hostnames, user IDs, file names, and some
names associated with network access, and interprocess communication.
• Namespaces are fundamental aspect of containers on Linux.
Docker
• The term "namespace" is often used for a type of namespace (e.g. process ID) as well for a particular space
of names. A Linux system starts out with a single namespace of each type, used by all processes. Processes
can create additional namespaces and join different namespaces.
• Since kernel version 4.10, there are 7 kinds of namespaces: Mount (mnt), Process ID (pid), Network (net),
Interprocess Communication (ipc), UTS, User ID (user), and Control Group (cgroup)
• Namespace functionality is the same across all kinds: Each process is associated with a namespace and can
only see or use the resources associated with that namespace, and descendant namespaces where
applicable. This way each process (or group thereof) can have a unique view on the resource. Which
resource is isolated depends on the kind of namespace that has been created for a given process group.
• Filedescriptor to the namespace's (ns) file: /proc/<pid>/ns/<ns-kind>
Container technique
• Linux Kernel provides the “control groups” (cgroups) functionality (cgroup-tools):
Allows limitation and prioritization of resources (CPU, Memory, Block I/O, Network, etc.) without the need
for starting any VM
Introduced in 2008 (Kernel 2.6.24)
• Various container software use Linux namespaces in combination with cgroups to isolate their processes,
including Docker and LXC.
• “namespace isolation” functionality:
Docker
Resource limiting, Prioritization, Accounting, and Control (freezing groups of processes, restarting, etc.)
Allows: Complete isolation of an applications' view of the operating environment, including process trees,
networking, user IDs and mounted file systems.
Possible to have a new “init” process for each container!
• Containers running in the user space
• Each container has:
• Own process space; Own network interface; Own /sbin/init (coordinates the rest of the boot process
and configures the environment for the user)
• Run stuff as root
• Share kernel with the host
• No device emulation. The system software and devices are provided by the image.
Docker Concepts
• Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.
The use of Linux containers to deploy applications is called containerization.
Containers are not new, but their use for easily deploying applications is.
• Docker engine runs in the background.
• Containerization is increasingly popular because containers are:
Flexible: Even the most complex applications can be containerized.
Lightweight: Containers leverage and share the host kernel.
Interchangeable: You can deploy updates and upgrades on-the-fly.
Docker
Portable: You can build locally, deploy to the cloud, and run anywhere.
Scalable: You can increase and automatically distribute container replicas.
Stackable: You can stack services vertically and on-the-fly.
• An image is an executable package that includes everything needed to run an application--the code, a runtime,
libraries, environment variables, and configuration files.
• A container is a run-time instance of an image--what the image becomes in memory when executed (that is, an image
with state, or a user process). A container is launched by running an image. (Self-contained units of software that has
its own file system)
You can see a list of your running containers with the command, just as you would in Linux:
# docker ps; docker ps -l
• When you run a container the process you launch take the PID 1 and assume init power. So when that process is
terminated the daemon stop the container until a new process is launched (via docker start).
Docker Concepts
• Traditional deployment era: Early on, organizations ran applications on physical servers. There was no way to define resource boundaries for applications
in a physical server, and this caused resource allocation issues.
• Virtualized deployment era: As a solution, virtualization was introduced. It allows you to run multiple Virtual Machines (VMs) on a single physical server's
CPU. Virtualization allows applications to be isolated between VMs and each VM is a full machine running all the components, including its own operating
system, on top of the virtualized hardware.
• Container deployment era: Like VM, a container has its own filesystem, share of CPU, memory, process space, and more. As they are decoupled from the
underlying infrastructure, they are portable across clouds and OS distributions.
Docker
[Source: https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/]
Docker Concepts - LXC/Docker vs VM
• A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a
discrete process, taking no more memory than any other executable, making it lightweight.
• By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host
resources through a hypervisor. In general, VMs provide an environment with more resources than most
applications need.
• https://docs.docker.com/reference/
Docker Concepts - Image vs Container
• In Dockerland, there are images and containers. The two are closely
related, but distinct.
• To use a programming metaphor, if an image is a class, then a
container is an instance of a class - a run-time object. Containers are
hopefully why you're using Docker; they're lightweight and portable
encapsulations of an environment in which to run applications.
• An image is an inert, immutable, file that's essentially a snapshot of
a container and it is really a template that can be turned into a
container.
Docker
• Pull image from Docker hub or build from a Dockerfile => Gives a Docker image (not editable!)
• Run the image ("docker run image_name:tag_name") => Gives a running image (i.e. a container)
Install, Start and Test Docker (on Linux)
• New Linux distributions now use DNF (Dandified YUM). DNF technology provides increased performance, has well-defined APIs, and supports modular
content, software AppStreams for cloud, container workloads.
• DNF is a software package manager. It installs, performs updates, and removes packages on Linux distributions.
• Use DNF to add & enable the official Docker CE repository and also list the repos:
$ sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo; sudo dnf repolist -v
• To list all the available docker-ce packages:
$ sudo dnf list docker-ce --showduplicates | sort -r
• An efficient solution is to allow your Linux system to install the version that meets the criteria best, using the --nobest command: (The
installation skips the latest candidates and installs the most appropriate version with the required containerd.io packages.)
$ sudo dnf install docker-ce --nobest --allowerasing -y # or yum install docker-ce docker-ce-cli containerd.io
Docker
• Enable and start the Docker service with: (We see the timestamp and confirm that Docker is active.)
$ sudo systemctl enable --now docker; systemctl status docker
• Add your user to the docker group with the following command:
$ sudo usermod -aG docker $USER; id $USER
/# apk update
• You have successfully installed and configured Docker on Linux.
Install, Start and Test Docker
• Alternatively:
$ docker images
$ docker pull hello-world
$ docker run hello-world
$ docker images
$ docker ps -a
$ docker version
$ docker info
$ docker
• Example of the docker name structure (you can leave out the parts you don't need):
Docker
registry.example.com:port/organization/image-name:version-tag
• Try: https://labs.play-with-docker.com/
Docker Concepts - Creating an Image
• When creating an image form a file, you have a number of different options.
See: https://docs.docker.com/engine/reference/commandline/build/
$ docker build https://github.com/docker/rootfs.git#container:docker # from git repositories
$ docker build http://server/context.tar.gz # from git repositories
$ docker build - < Dockerfile # from text files
$ docker build . # build with path
$ docker build github.com/creack/docker-firefox # build with URL
$ docker build context.tar.gz # build from a compressed context
Docker
A container includes: code, configs, processes, networking, dependencies and it even includes just enough of the operating system to run
your code.
Each container has its own isolated little world.
And Docker is the program that manages all of this!
Docker Concepts - Container
Docker:
A client program named Docker (technically Container Runtime),
A server program that manages a Linux system,
A program that builds containers from code along with its dependencies and bundles
it up and then seals it into a container.
And it is a service that distributes these containers across the internet and makes it so
you can find other’s work, and the right people can find your work.
Docker
To see list of your running containers (except the Stopped Containers by default):
# docker ps
• If we just want to see the last container to exit specify the "-l" argument after "ps":
# docker ps -l
• "docker run" and "docker commit " are complementary to each other
• To run an image (i.e. ubuntu) and commit it as a new new image:
# docker run -ti ubuntu bash [new_container_name]
root@c7b98e3cf524 touch my-important-file; exit
# docker commit [c7b98e3cf524(container_id) or container_name] [my-new-image-name]
sha256:c6e5341fb9c7aea8700e4b1944912316e7262bd02b19214662e4120053221e7f
(if you didn’t provide [my-new-image-name]):
# docker tag c6e5341fb9c7aea8700e4b1944912316e7262bd02b19214662e4120053221e7f my-new-image-name
# docker images
The Docker Flow
A picture is worth a thousand words:
Docker
The Docker Flow
• "docker run" and "docker commit" are complementary to each other:
• "docker run" takes images to containers, and "docker commit" takes containers back
to new images. (It does not overwrite the image that the container was made from)
$ docker images
$ docker container ls # ="docker ps" list the running containers ; sure it is more typing, but it is a lot more clear on what it does.
$ docker container ls -l # ="docker ps -l" list latest created container
$ docker container ls -a # ="docker ps -a" list all (both running and stoppped) containers
$ docker run -ti [image_name] # ti: terminal interactive takes images to container and runs command(s) in this new container
$ docker run -ti --name [string] [image_name] # Assign a name to the container and run it
Docker