Docker
Docker
Virtualization
===================
This is the process of running multiple OS's parallelly on
a single pice of h/w.
Here we have h/w(bare metal) on top of which we have host os
and on the host os we install an application called as hypervisor
On the hypervisor we can run any no of OS's as guest OS
Containarization
======================
Here we have bare metal on top of whcih we install the host Os
and on the hsot OS we install an application called as Docker Engine
On the docker engine we can run any application in the form of containers
Docker is a technology for creating thse containers
These containers pass through less no of layers to access the h/w resources
also organizations need not spend money on purchasing licenses of different
OS's to maintian various applications
Docker can be used at the the stages of S/W development life cycle
Build---->Ship--->Run
===========================================================================
Docker comes in 2 flavours
Docker CE (Community Edition)
Docker EE (Enterprise Edition)
2 Install it
========================================================================
========================================================================
Install docker on Linux
==============================
1 Create an Ubuntu instance on AWS
2 Connect to it using git bash
3 Execute the below 2 commands
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
==========================================================================
Docker Client:This is the CLI of docker where the user can execute the
docker commands,The docker client accepts these commands and passes them
to a background process called "docker deamon"
Docker deamon: This process accepts the commands coming from the docker client
and routes them to work on docker images or containers or the docker registry
Docker registry: This is the cloud site of docker where docker images are
stored.This is of two types
1 Public Registry( hub.docker.com)
2 Private Registry(Setup on one of our local servers)
===========================================================================
Important docker commands
==============================
Working on docker images
===============================
1 To pull a docker image
docker pull image_name
===================================================================
11 To create a docker image from a dockerfile
docker build -t image_name .
15 To start a container
docker start container_id/container_name
16 To stop a container
docker stop container_id/container_name
17 To restart a container
docker restart container_id/container_name
To restart after 10 seconds
docker restart -t 10 container_id/container_name
40 To delete a volume
docker volume rm volume_name/volume_id
===================================================================================
=====
===================================================================================
=======
==========================================================================
UseCase 1
=============
Create an nginx contaienr in detached mode and name it webserver
Also perfrom port mapping
============================================================================
UseCase 2
===============
Start tomcat as a container and perfrom automatic port mapping
docker run --name appserver -d -P tomee
===================================================================================
UseCase 3
================
Start a jenkins container in detached mode and also perfrom port mapping
docker run --name myjenkins -d -p 9999:8080 jenkins/jenkins
===================================================================================
=
UseCase
===================
Create a mysql container and login as root user and create some sql tables
==========================================================================
UseCase
==================
Create an ubuntu container and launch interactive terminal
docker run --name u1 -it ubuntu
4 Python Scripting
5 Ansible Playbooks
========================================================================
UseCase
========================
4 Check if c2 is pinging to c1
ping c1
===========================================================================
=================================================================================
UseCase
Setup wordpress and link it with mysql container
=======================================================================
=======================================================================
UseCase
=============
Setup CI-CD environment where a Jenkins container is linked with
2 tomcat containers for QAserver and PRodserver
1 Create a jenkins container
docker run --name myjenkins -d -p 5050:8080 jenkins/jenkins
=======================================================================
=======================================================================
3 Create a php container and link with mysql and apache containers
docker run --name php -d --link mydb:mysql --link apache:httpd php:7.2-apache
==================================================================
UseCase
================
Create a testing environment where a selenium hub container is linked
with 2 node containers one with chrome and other with firefox installed
1 Create a selenium hub container
docker run --name hub -d -p 4444:4444 selenium/hub
========================================================================
========================================================================
Docker compose
=======================
The disadvantage of "link" option is it is depricated and
the same individual command have to be given multiple times
to setup similar architectures.
To avaoid this we can use docker compose
Docker compose uses yml files to setup the multu container
architecture and these files can be resused any number of time
=======================================================================
USeCase
==============
Create a docker compose file to setup a mysql and wordpress
container and link them
vim docker-compose.yml
---
version: '3.8'
services:
mydb:
image: mysql:5
environment:
MYSQL_ROOT_PASSWORD: intelliqit
mywordpress:
image: wordpress
ports:
- 8888:80
links:
- mydb:mysql
...
To setup the containers from the above file
docker-compose up -d
vim docker-compose.yml
---
version: '3.8'
services:
myjenkins:
image: jenkins/jenkins
ports:
- 5050:8080
qaserver:
image: tomee
ports:
- 6060:8080
links:
- myjenkins:jenkins
prodserver:
image: tomee
ports:
- 7070:8080
links:
- myjenkins:jenkins
...
=============================================================================
UseCase
==============
Create a docker compose file to setup the LAMP architecture
vim lamp.yml
---
version: '3.8'
services:
mydb:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: intelliqit
apache:
image: httpd
ports:
- 8989:80
links:
- mydb:mysql
php:
image: php:7.2-apache
links:
- mydb:mysql
- apache:httpd
...
========================================================================
UseCase
============
Create a docker compose file to setup the selenium testing
environment where a selenium hub container is linked with
2 node containers one with chrome and other with firefox
vim docker-compose.yml
---
version: '3.8'
services:
hub:
image: selenium/hub
ports:
- 4444:4444
container_name: hub
chrome:
image: selenium/node-chrome-debug
ports:
- 5901:5900
links:
- hub:selenium
container_name: chrome
firefox:
image: selenium/node-firefox-debug
ports:
- 5902:5900
links:
- hub:selenium
container_name: firefox
...
===================================================================================
==============
===================================================================================
==============
Docker Volumes
====================
Containers are ephemeral(temporary) but the data processed by the
containers should be persistent.Once a container is delete all the
data of the container is lost
To preserve the data even if the container is deleted we can use volumes
UsedCase
============
Create a directory /data and mount it as a volume on an ubuntu container
Create some files in the mounted volumes and check if the files
are preserved on the host machine even after the container is deleted
4 Create another centos container c2 and it should used the voluems used by c1
docker run --name c2 -it --volumes-from c1 centos
7 Create another centos container c3 and it should use the volume used by c2
docker run --name c3 -it --volumes-from c2 centos
10 Go into any of the 3 contianers and we will see all the files
docker attach c1
cd /data
ls
exit
=======================================================================
=======================================================================
Docker volume containers
----------------------------
These volumes are bidirectoinal ie the changes done on host
will be reflected into container and changes done by container
will be reflected to host machine
1 Create a volume
docker volume create myvolume
5 Create a centos container and mount the above volume into the tmp folder
docker run --name c1 -it -v myvolume:/tmp centos
===========================================================================
UseCase
============
Create a volume "newvolume" and create tomcat-users.xml file in it
Create a tomcat container and mount the above volume into it
Copy the tomcat-users.xml files to the required location
1 Create a volume
docker volume create newvolume
==================================================================
=======================================================================
Creating customsied docker images
=========================================
This can be done in 2 ways
1 Using docker commit command
2 Using dockerfile
=============================================================================
Dockerfile
===================
Dockerfile uses predefined keyword to create customsied
docker images.
ADD : This is similar to copy where it can copy files from host
to image but ADD can also downlaod files from some remote server
CMD : USed to run the default process of the container from outside
ENTRYPOINT : This is also used to run the default process of the container
LABEL: Used to store data about the docker image in key value pairs
SHELL : Used to specify what shell should be by default used by the image
===============================================================================
UseCase
===========
Create a dockerfile to use nginx as abse image and specify
the maintainer as intelliqit
FROM nginx
MAINTAINER intelliqit
-----------------------------------------------------------------------
UseCase
==============
Create a dockerfile from ubuntu base image and install
git in it
1 Create dockerfile
vim dockerfile
FROM ubuntu
MAINTAINER intelliqit
RUN apt-get update
RUN apt-get install -y git
2 Create an image from the above file
docker build -t myubuntu .
4 Create a container from the new image and it should have git installed
docker run --name u1 -it myubuntu
git --version
=========================================================
Cache Busting
===================
When we create an image from a dockerfile docker stores all the
executed isntructions in a its cache.Next time if we edit the
same docker file and add few new instructions and build an image
out of it docker will not execute the previously executed statements
Instead it will read them from the cache
This is a time saving mechanism
The disadvantage is if the docker file is edited with a huge time
gap then we might end up installing s/w's that are outdated
Eg:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y git
To avoid this problem and make docker execute all the instructions
once more time without reading from cache we use "cache busting"
docker build --no-cache -t myubuntu .
===============================================================
=================================================================================
Download docker shll script into the docker host and copy it into the customsied
docker image
and later install it at the time of creating the image
===================================================================================
======
Create a dockerfile to create an ansible image
1 vim dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-get install -y ansible
2 Build an image
docker build -t ansible
This container will have ansible installed in it
===================================================================================
==========
Create a dockerfile from ubunt base image and downlaod jenkins.war
into it
1 Create a dockerfile
vim dockerfile
FROM ubuntu
MAINTIANER intelliqit
ADD https://get.jenkins.io/war-stable/2.263.4/jenkins.war /
===========================================================================
Create a dockerfile from jenkins base image and make the default user as root
1 vim dockerfile
FROM jenkins/jenkins
MAINTAINER intelliqit
USER root
4 Go into the interactive shell and check if the default user is root
docker exec -it j1 bash
whoami
==============================================================================
Create a docekerfile from nginx base image and expose 90 port
1 vim dockerfile
FROM nginx
MAINTAIENR intelliqit
EXPOSE 90
========================================================================
========================================================================
Create a dockerimage with a volume on an image
1 Create a dockerfile
vim dockerfile
FROM ubuntu
MAINTAINER intelliqit
VOLUME /data
2 Create a image
docker build -t myubuntu .
3 Create a container
docker run --name u1 -it myubuntu
cd data
touch file1 file2
exit
4 Delete container
docker rm -f u1
2 Create an image
docker build -t myubuntu .
3 Create a container
docker run --name u1 -it myubuntu
the container behaves like jenkins
=======================================================================
UseCase
=============
Create a dockerfile from ubuntu base image and make it behave
like nginx
1 Create a dockerfile
vim dockerfile
FROM ubuntu
MAINTAINER intelliqit
RUN apt-get update
RUN apt-get install -y nginx
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]
EXPOSE 80
3 Create a container from the above image and it will work like nginx
docker run --name n1 -d -P myubuntu
Eg:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
CMD ["/usr/sbin/nginx","-g","daemon off;"]
EXPOSE 80
===============================================================================
Working on docker registry
==============================
This is the location where the docker images are saved
This is of 2 types
1 Public registry
2 Private regsitry
UseCase
Create a customised centos image and upload into the public registry
3 Push to registry
docker push intelliqit/nginx19
===================================================================================
===================================================================================
Private Registry
============================
ECR
================
1 Create an IAM role with admin previlages and assign to docker host
2 Search for ECR service on aws and create a private ecr registry
3 Click on View push command and copy paste the command in the docker host
===================================================================================
Note: To create network with a specific subnet range
docker network create --driver bridge --subnet=192.168.2.0/24 intelliqit
Docker compose by deafult creates its own customised bridge network and creates
containers on the netowork
vim docker-compose.yml
---
version: '3.8'
services:
mydb:
image: postgres
environment:
POSTGRES_PASSWORD: intelliqit
POSTGRES_DB: mydb
POSTGRES_USER: myuser
adminer:
image: adminer
ports:
- 8080:8080
===================================================================================
==
UseCase
=============
Create a custom bridge network and create a docker compose file
to start postgres and adminer container on the above created
network
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: intelliqit
POSTGRES_USER: myuser
POSTGRES_DB: mydb
adminer:
image: adminer
ports:
- 8888:8080
networks:
default:
external:
name: intelliqit
...
========================================================================
Create a dockerfile and use it directly in docker-compsoe
vim dockerfile
FROM jenkins/jenkins
MAINTAINER intelliqit
RUN apt-get update
RUN apt-get install -y git
vim docker-compose.yml
version: '3.8'
services:
jenkins:
build: .
ports:
- 7070:8080
mytomcat:
image: tomee
ports:
- 6060:8080
...
=============================================================================
Docker compose file to create 2 networks and run containers on different network
vim docker-compose.yml
---
version: '3.8'
services:
mydb:
image: jenkins/jenkins
ports:
- 5050:8080
networks:
- abc
qaserver:
image: tomee
ports:
- 6060:8080
networks:
- xyz
prodserver:
image: tomee
ports:
- 7070:8080
networks:
- xyz
networks:
abc: {}
xyz: {}
...
===================================================
Docker compose file to create 2 containers and also create 2 volumes for both the
containers
---
version: '3.8'
services:
db:
image: mysql:5
environment:
MYSQL_ROOT_PASSWORD: intelliqit
volumes:
mydb:/var/lib/mysql
wordpress:
image: wordpress
ports:
- 9999:80
volumes:
wordpress:/var/www/html
volumes:
mydb:
wordpress
===================================================================================
===
==================================================
Docker Swarm
========================================================================
Setup of Docker Swarm
============================
1 Create 3 AWS ubuntu instances
2 Name them as Manager,Worker1,Worker2
3 Install docker on all of them
4 Change the hostname
vim /etc/hostname
Delete the content and replace it with Manager or Worker1 or Worker2
5 Restart
init 6
6 To initilise the docker swarm
Connect to Manager AWS instance
docker swarm init
This command will create a docker swarm and it will also generate
a tokenid
7 Copy and paste the token id in Worker1 and Worker2
===============================================================================
TCP port 2376 for secure Docker client communication. This port is required for
Docker Machine to work. Docker Machine is used to orchestrate Docker hosts.
TCP port 2377. This port is used for communication between the nodes of a Docker
Swarm or cluster. It only needs to be opened on manager nodes.
TCP and UDP port 7946 for communication among nodes (container network discovery).
UDP port 4789 for overlay network traffic (container ingress networking).
=========================================================================
Load Balancing:
Each docker containers has a capability to sustain a specific
user load.To increase this capability we can increase the
number of replicas(containers) on which a service can run
UseCase
------------
Create nginx with 5 replicas and check where these replicas are
running
===================================================================================
==========
===================================================================================
==========
Scalling
============
This is the process of increasing the number of replicas or decreasing
the replicas count based on requirement without the end user experiencing
any down time.
UseCase
============
Create httpd with 4 replicas and scale it to 8 and scale it
down to 2
========================================================================
Rolling updates
======================
Services running in docker swarm should be updated from once
version to other without the end user downtime
UseCase
===========
Create redis:3 with 5 replicas and later update it to redis:4
also rollback to redis:3
1 Create redis:3 with 5 replicas
docker service create --name myredis --replicas 5 redis:3
4 Check redis:3 replcias are shut down and in tis palce redis:4 replicas are
running
docker service ps myredis
6 Check if redis:4 replicas are shut down and in its place redis:3 is running
docker service ps myredis
================================================================================
To remove a worker from swarm cluster
docker node update --availability drain Worker1
=============================================================================
FailOver Scenarios of Workers
================================
Create httpd with 6 replicas and delete one replica running on the manager
Check if all 6 replicas are still running
Drain Worker1 from the docker swarm and check if all 6 replicas are running
on Manager and Worker2,make Worker1 rejoin the swarm
Make Worker2 leave the swarm and check if all the 6 replicas are
running on Manager and Worker1
4 Delete a replica
docker rm -f container_id_from_step3
======================================================================
FailOver Scenarios of Managers
====================================
If a worker instance crashses all the replicas running on that
worker will be moved to the Manager or the other workers.
If the Manager itself crashes the swarm becomes headless
ie we cannot perfrom container orchestration activites in this
swamr cluster
If one manager node goes down other manager becomes the Leader
Quorum is resonsible for doing this activity and if uses a RAFT
algorithm for handling the failovers of managers.Quorum also
is responsible for mainting the min number of manager
===================================================================================
=====
===================================================================================
=====
Overlay network
=====================
This is the default network used by docker swarm when contiainer run a multiple
servers
and the name of this network is ingress.
UseCase
===========
Create 2 overlay networks intelliqit1 and intelliqit2
Create httpd with 5 replacs on intelliqit1 network
Create tomcat with 5 replicas on default overlay "ingres" network
and later perform rolling network update to intelliqit2 network
===============================================================================
===============================================================================
Docker Stack
=====================
docker compose + docker swarm = docker stack
docker compose + kubernetes = kompose
4 To delete a stack
docker stack rm stack_name
=====================================================================
UseCase
================
Create a docker stack file to start 3 replicas of wordpress
and one replica of mysql
vim stack1.yml
---
version: '3.8'
services:
db:
image: "mysql:5"
environment:
MYSQL_ROOT_PASSWORD: intelliqit
wordpress:
image: wordpress
ports:
- "8989:80"
deploy:
replicas: 3
=====================================================================
UseCase
==============
Create a stack file to setup CI-cd architecture where a jenkins
container is linked with tomcats for qa and prod environments
The jenkins contianers should run only on Manager
the qaserver tomcat should run only on Worker1 and prodserver
tomcat should run only on worker2
vim stack2.yml
---
version: '3.8'
services:
myjenkins:
image: jenkins/jenkins
ports:
- 5050:8080
deploy:
replicas: 2
placement:
constraints:
- node.hostname == Manager
qaserver:
image: tomcat
ports:
- 6060:8080
deploy:
replicas: 3
placement:
constraints:
- node.hostname == Worker1
prodserver:
image: tomcat
ports:
- 7070:8080
deploy:
replicas: 4
placement:
constraints:
- node.hostname == Worker2
...
==============================================================================
==============================================================================
UseCase
Create a stack file to setup the selenium hub and nodes architecture
but also specify a upper limit on the h/w
vim stack3.yml
---
version: '3.8'
services:
hub:
image: selenium/hub
ports:
- 4444:4444
deploy:
replicas: 2
resources:
limits:
cpus: "0.1"
memory: "300M"
chrome:
image: selenium/node-chrome-debug
ports:
- 5901:5900
deploy:
replicas: 3
resources:
limits:
cpus: "0.01"
memory: "100M"
firefox:
image: selenium/node-firefox-debug
ports:
- 5902:5900
deploy:
replicas: 3
resources:
limits:
cpus: "0.01"
memory: "100M"
===================================================================================
==========
Docker Secrets
====================
This is a feature of docker swarm using which we can pass secret data
to the services running in swarm cluster
These secrets are created on the host machine and they will be
availbale from all the replicas in the swarm cluster
==============================================================================