Docker
Docker
Docker
Hypervisor
When we run multiple application in one machine is called Monolithic. All application using common Library.
Any update in OS & library file get update. Updated Library file may not be compatible with one application. So,
we were distributing application across different machine.( One application, One machine)
If there are 100 requirement then we have to setup 100 machines resources (1G RAM, 1 CPU , 10 GB HDD)
2) Container based Provisioning ( OS based virtualization): Isolate application in OS or isolate application file.
Earlier we were giving 10 OS for 10 application . All container will have separate Library file , Ip, port. With this we will
save lots of cost. As it will save VM costing.
If OS gets down, then Kubernetes will migrate whole cluster to new location. ( managing number of dockers via
Kubernetes)
A B C D
OS
Docker : Docker is community name which provides container solution. Container solution given by many companies.
Everyone is using container of Docker community. ( Like Linux is being used of Redhat in many companies)
Container : It is an micro machine which isolates application binary/library in OS. Or we can run multiple application
inside the same OS without any conflict. Container only contain SW dependencies.If we need updated file then we will
update container.
There are many containers available Rocket , Drawbridge or LXD
Advantage :
1) LOW HW Footprint
2) Environment isolation ; On a single machine 4 teams are working with separate environment
3) Quick deployment : On Single click one can create deploy application .
4) Multiple environment deployment
5) Reusability :
( Containers not impacting performance of application)
Host can run many containers depends upon CPU & memory utilization of base machine.
If we haven’t defined CPU , memory while creating application then container will use based on requirement.
Day 2
Docker Architecture :
Docker host : Machine where container is being run. It can be any OS linux/windows. In most of the cases
docker host is Linux based. Container are portable & can be run on any OS. It can be run on any HW , Cloud based .
CPU, memory depends on how many application we want to run & CPU/Utilization of those application. Container is a
process & we can launch n number of container. Container works on thin provision . If we haven’t define allocation
then each container can use memory/CPU as per their requirement
Docker is open community. We 2 have docker edition . One is CE ( community edition) & EE ( enterprise edition , u got
support from docker community). Redhat also provided enterprise edition. Commands are same for both edition,
Redhat is providing complete solution i.e docker + orchestrtation & is called open shift. In RHEL 7 , docker is termed
as atomic host in RHEL-8 it is called podman. IN redhat we can install containerization technology from Redhat or
Docker.
Docker image : Every container is launched via image. Without Image we can't run container. Image is lightweight
package which contains required file to run application.
You want to make your own images or if images are not available in community
Docker registry server : Community uploads images on some servers. These all called Docker registry server/
Docker : docker.hub/docker.io
Redhat : registry.access.redhat.com/registry.redhat.io/quay.io
These are public registry server or community based registry
Private registry server : If we need for specific purpose
Container : Running mode of docker image. Every container will have IP , Process, file system , Virtual NIC card.
These things are also called namespace. One container can have lot of namespaces.
Cgroup : It restricts namespace usage. ( Memory, CPU ). It is linux feature. Configuration at back end is done by C
group.
Amazon EC2 ( Elastic Compute Cloud )
Public Key
AWS Instance
Private Key
root@dockerhost:~# ls -ltr
total 8
drwxr-xr-x 4 root root 4096 Sep 6 10:44 snap
-rw-r--r-- 1 root root 23 Sep 12 17:26 abc
Community Images :
root@ip-172-31-16-240:~# docker pull nginx ( if we don’t specify any path then it will go to docker.io and will pull official image)
Using default tag: latest
latest: Pulling from library/nginx
e1acddbe380c: Pull complete
e21006f71c6f: Pull complete
f3341cc17e58: Pull complete
2a53fa598ee2: Pull complete
12455f71a9b5: Pull complete
root@ip-172-31-16-240:~# docker pull nginx:1.18 ( One can specify version to be installed) ( One can search for version in docker .io & it will give the command as well)
1.18: Pulling from library/nginx
f7ec5a41d630: Pull complete
0b20d28b5eb3: Pull complete
1576642c9776: Pull complete
c12a848bad84: Pull complete
03f221d9cf00: Pull complete
If we want to make container then first, we will have to being images onto docker host
root@ip-172-31-16-240:~# df -TH /
Filesystem Type Size Used Avail Use% Mounted on
/dev/root ext4 8.3G 3.3G 5.0G 40% /
root@ip-172-31-16-240:~# docker run -it --name=test1 nginx:1.20 ( I. Interactive , T : Terminal , Mostly used in raw images)
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
root@ip-172-31-16-240:~# ps –ef
root@ip-172-31-16-240:~# docker top test2 (check PPID in base m/c)
root@ip-172-31-16-240:~# docker attach test2 ( attach will work if container containes /bin/bash shell, if any container is not having then /bin/bash then we cant
attach,you can check if top is showing /bin/bash)
[root@4a4f7edacaf6 /]#
root@ip-172-31-16-240:~# docker top test2 ( Now you will find 2 shells are running, earlier shell was getting exited/killed)
root@ip-172-31-16-240:~# docker run -it --name=test4 --restart always centos ( Works even after restarting docker host)
If we don’t allocate any memory or cpu while running container then by default it will use whatever required.
Above command gives us the view of CPU & memory being used by container.
Size will be approx 0 because container is reading from file system in docker host.
If you will write in docker then it will occupy file system from above path.
Container Logs :
Any changes in the container will not directly impact the container
Storage
Image Container
namespace
Every container has its own writing layer else every application with that container will be impacted
1) Base Image
2) Run image
3) Make changes in image as required
4) Logout
5) Commit changes
root@dockerhost:/var/lib/docker# docker info | grep -i storage
Storage Driver: overlay2
One can read the file in image without running it if someone has knowledge of overlay
root@dockerhost:~# cd /var/lib/docker/overlay2/
Go inside image & diff folder
root@dockerhost:~# docker network connect bridge con1 ( Con1 will be part of 2 network now)
Port : 80 Container
Type hostIP:8080
root@dockerhost:~# curl 3.138.196.213:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
[root@440d357f39f7 /]# yum install openssh-server –y
Useradd mani
Passwd mani
Day 5
Dynamic port Forwarding :
Any free port of docker host will be bind to container port ( -P)
If container is restarted, then host port may change .
So we do not use Dynamic forwarding or temporarily used.
If there are multiple NIC card & we need to bind container with particular NIC card
root@dockerhost:~# docker run -d -p 172.31.25.38:8080:80 --name=web12 nginx
35462e9062993d1c2c3fa9aefd6aca056f6bd9049881c1fbdb1f117532cf9a15
Sometime there is requirement that container are part of host network and not using bridge network
If one has used one type of network with host network, then u can't run another container of same type and
cant get isolated environment specifically network card.
If we have only host n/w & not bridge n/w then we will not be able to get isolated environment
We do not need port forwarding for such type of containers
eth0 eth0
Port :
8080
Some time we want to run container for testing and do not generate any traffic then we define network as none
Docker volume:
Check availability zone of EC instance and disk has to be mapped with same availability zone
Add Tag
Now map this disk to instance
root@dockerhost:~# lsblk ( list information about all available block devices , It reads mounted and unmounted file system, df reads only mounted file system , df reports
file system disk space usage)
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 33.3M 1 loop /snap/amazon-ssm-agent/3552
loop1 7:1 0 25M 1 loop /snap/amazon-ssm-agent/4046
loop2 7:2 0 55.4M 1 loop /snap/core18/2128
loop3 7:3 0 55.5M 1 loop /snap/core18/1997
loop4 7:4 0 32.3M 1 loop /snap/snapd/13170
loop5 7:5 0 70.3M 1 loop /snap/lxd/21029
loop7 7:7 0 32.3M 1 loop /snap/snapd/12883
loop8 7:8 0 61.8M 1 loop /snap/core20/1081
loop9 7:9 0 67.3M 1 loop /snap/lxd/21545
xvda 202:0 0 8G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdf 202:80 0 5G 0 disk
root@dockerhost:~# vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -g /docker -H fd:// --containerd=/run/containerd/containerd.sock ( red high lighted is new file system ) ( type in google to change docker file
system)
Now pull any image it will be stored in /docker , check overlay dir
Server 1 – 100 GB External Disk
OS User data
If Serevr1 OS gets correupted then user data will also be lost if OS & data are on same server
Similarly, if we make any changes in container those will be lost if container base image is corrupted or lost
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
root@dockerhost:~# docker rm -f con1
con1
root@dockerhost:~# docker run -d --name=con2 -v /volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql1 mysql
30bd24d5a4d248b892dfce27924c50282524d3ef0ac46db86de60c5684c2d3aa
root@dockerhost:~# docker exec -it con2 /bin/bash
root@30bd24d5a4d2:/#
root@30bd24d5a4d2:/#
root@30bd24d5a4d2:/#
root@30bd24d5a4d2:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@dockerhost:/docker/volumes# docker run -it --name=con8 -v volume2:/opt centos ( file written in /opt path of container)
Unable to find image 'centos:latest' locally
For every image there is docker file. Docker file has “docker parameter” & “Values”.
FROM : It always contains base image name for building new image (Mandatory, Once per file)
MAINTAINER : one who build image/email ( optional)
RUN: what are the instruction to be executed
ADD https://openui5.hana.ondemand.com/1.36.4/docs/guide/f7cbafc9a76140ec8fc55b51a63cf467.html
$BASE_DIR/page.html
VOLUME /var/lib/docker
EXPOSE 80
CMD ["ifconfig"] root@dockerhost:~/test# docker build -t centosv . ( -t : tag new image with specified name)
root@dockerhost:~/test# docker run -it --name=con11 centosv
[root@2ce8ed07169b /]# ifconfig -a
Day 7
Docker compose : ( container creation process automation)
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a
YAML file to configure your application's services.
It reduces docker admin job. This is third party binary & by default it is not part of docker files.
( YAML is text-based file used for configuration data)
( do not run command manually to execute container )
If we are getting continuous requirement to build container then we use docker compose.
Search for “docker compose install” & Look for “Linux” Installation
root@dockerhost:~# docker-compose --version
docker-compose version 1.29.2, build 5becea4c
root@dockerhost:~# pwd
/root
root@dockerhost:~# mkdir test1
root@dockerhost:~# cd test1
root@dockerhost:~/test1# vi docker-compose.yml ( new folder new compose file)
root@dockerhost:~/test1#
root@dockerhost:~/test1# docker-compose ps
Docker registry :
( Docker images are stored at one place; where we can maintain our images )
( container is a process, images are migrated/clone)
HTTP
HTTPS
Docker registry
server