Udemy Docker Advanced PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 71

Advanced

Advanced
DOCKER ADVANCED
A deeper look into Docker

Mumshad Mannambeth | [email protected]


INTRODUCTION

• Lecture
• Demos
• Coding Exercises
• Assignment
PRE-REQUISITES

• Basic System Administration


• Basic Docker Commands
• Docker Files
• Docker Compose
• Docker Networking
OBJECTIVES


✓D ocker Over view ❑ Docker Concepts in Depth

✓Ruunni ng
g Docker Containers ❑ Docker For Windows

✓Crea
a tin
ng a Docker Image ❑ Docker Service

✓D ocker Compose ❑ Docker Swarm

✓Docker Swarm ❑ Overlay Networks

✓Neet working in Docker ❑ Load Balancing
❑ CI/CD Integration
DOCKER STORY

Founder: Solomon Hykes


Release: March 2013
Downloads: 13 Billion
DOCKER ON WINDOWS

Mumshad Mannambeth | [email protected]


DOCKER ON WINDOWS

• Docker on Windows using Docker Toolbox


• Docker for Windows
1. DOCKER TOOLBOX

• 64-bit operating
• Windows 7 or higher.
• Virtualization is enabled

• Oracle Virtualbox
• Docker Engine
• Docker Machine
• Docker Compose
• Kitematic GUI
2. DOCKER FOR WINDOWS

Support: Windows 10 Enterprise/Professional Edition


Windows Server 2016

Linux Containers (Default)


Or
Windows Containers
WINDOWS CONTAINERS
Container Types:

Windows Server Hyper-V Isolation

Container Container Container


Container Container Container
Kernel Kernel Kernel

Kernel Kernel

Base Images:

• Windows Server Core

• Nano Server

Support
• Windows Server 2016
• Nano Server
• Windows 10 Professional and Enterprise (Hyper-V Isolated Containers)
DEMO
DOCKER ENGINE

Mumshad Mannambeth | [email protected]


DOCKER ENGINE

Docker Engine Laptop

Docker CLI

REST API

Docker Deamon docker –H=remote-docker-engine:2375

docker –H=10.123.2.1:2375 run nginx


CONTAINERIZATION

Process ID Unix Timesharing

Network Namespace Mount

InterProcess
NAMESPACE - PID
Linux System
PID : 1

PID : 2

PID : 3
Child System (Container)
PID : 4

PID : 5 PID : 1

PID : 6 PID : 2
CGROUPS
Linux System

Docker Docker Docker


Container Container Container

CPU Memory

docker run --cpus=.5 ubuntu

docker run --memory=100m ubuntu


DOCKER STORAGE

Mumshad Mannambeth | [email protected]


FILE SYSTEM

/var/lib/docker
aufs
containers
image
volumes
LAYERED ARCHITECTURE
Dockerfile Dockerfile2
FROM Ubuntu FROM Ubuntu

RUN apt-get update && apt-get –y install python RUN apt-get update && apt-get –y install python

RUN pip install flask flask-mysql RUN pip install flask flask-mysql

COPY . /opt/source-code COPY app2.py /opt/source-code

ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run ENTRYPOINT FLASK_APP=/opt/source-code/app2.py flask run

docker build Dockerfile –t mmumshad/my-custom-app docker build Dockerfile2 –t mmumshad/my-custom-app-2

Layer 1. Base Ubuntu Layer 120 MB Layer 1. Base Ubuntu Layer 0 MB

Layer 2. Changes in apt packages 306 MB Layer 2. Changes in apt packages 0 MB

Layer 3. Changes in pip packages 6.3 MB Layer 3. Changes in pip packages 0 MB

Layer 4. Source code 229 B Layer 4. Source code 229 B

Layer 5. Update Entrypoint 0B Layer 5. Update Entrypoint 0B


LAYERED ARCHITECTURE

Read Write
Container Layer
Layer 6. Container Layer

docker run mmumshad/my-custom-app

Read Only
Layer 5. Update Entrypoint with “flask” command
Layer 4. Source code

Image Layers Layer 3. Changes in pip packages


Layer 2. Changes in apt packages
Layer 1. Base Ubuntu Layer

docker build Dockerfile –t mmumshad/my-custom-app


COPY-ON-WRITE

Read Write
Container Layer

temp.txt

Read Only

Image Layers app.py


app.py
VOLUMES docker run –v data_volume:/var/lib/mysql mysql

docker volume create data_volume docker run –v data_volume2:/var/lib/mysql mysql

docker run –v /data/mysql:/var/lib/mysql mysql


/var/lib/docker
volumes docker run \

data_volume –-mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql

Read Write Read Write

/var/lib/mysql /var/lib/mysql

mysql-container layer mysql-container layer

data_volume mysql

/var/lib/docker/volumes /data

Read Only
mysql – image layer
Docker Host
STORAGE DRIVERS

• AUFS
• ZFS
• BTRFS
• Device Mapper
• Overlay
• Overlay2
DOCKER SWARM

Mumshad Mannambeth | [email protected]


DOCKER SWARM

Docker Swarm

Web Web Web Web Web


Container Container Container Container Container

MySQL
Container

Docker Host Docker Host Docker Host Docker Host


SETUP SWARM

Node Node Node


Swarm Manager Worker Worker Worker

docker swarm init docker swarm join docker swarm join docker swarm join

--token <token> --token <token> --token <token>

Docker Host Docker Host Docker Host Docker Host


DOCKER MANAGERS
MANAGER NODES

Swarm Manager Swarm Manager Swarm Manager

Leader
Docker Host Docker Host Docker Host

Worker Worker Worker Worker

Docker Host Docker Host Docker Host Docker Host


DISTRIBUTED CONSENSUS - RAFT

L
L
DISTRIBUTED CONSENSUS - RAFT

DB

D
Instruction

DB DB
HOW MANY MANAGER NODES?
• Docker Recommends – 7 Managers
• No limit on Managers

Managers Majority Fault N+1


Tolerance
Quorum of N =
1 1 0 2
5+1
2 2 0 Quorum of 5 = = 3.5 = 3
2
3 2 1

4 3 1

5 3 2

6 4 2
N-1
7 4 3 Fault Tolerance of N =
2
ODD OR EVEN?

Managers Majority Fault


Tolerance

1 1 0

2 2 0

3 2 1

4 3 1

5 3 2
7 O
O6 4 2

7O 4 3
WHAT HAPPENS WHEN IT FAILS?

Worker Worker Worker Worker Worker

Web Server Web Server Web Server Web Server Web Server

Docker Host Docker Host Docker Host Docker Host Docker Host
docker node promote

docker swarm init --force-new-cluster


CAN MANAGER WORK?

Web Server

Docker Host

docker node update --availability drain <Node>


QUIZ
DOCKER SERVICE

Mumshad Mannambeth | [email protected]


DOCKER SERVICE

docker service create –replicas=3 my-web-server


docker run my-web-server

Web Server Web Server Web Server


Web Server

Docker Host Worker Node Worker Node Worker Node

Docker Swarm
TASKS
docker service create –replicas=3 my-web-server

Orchestrator

Scheduler
Manager Node

Task Task Task

Web Server Web Server Web Server

Worker Node Worker Node Worker Node


Docker Swarm
REPLICAS
docker service create –replicas=3 my-web-server

Web Server Web Server

Web Server

Worker Node Worker Node

Docker Swarm
REPLICAS
docker service create –replicas=3 my-web-server

Web Server Web Server Web Server Web Server

Worker Node Worker Node Worker Node Worker Node

Docker Swarm
REPLICAS VS GLOBAL
docker service create --replicas=3 my-web-server

docker service create --mode global my-monitoring-agent

Web Server Web Server Web Server

Monitor Agent Monitor Agent Monitor Agent Monitor Agent

Worker Node Worker Node Worker Node Worker Node

Docker Swarm
SERVICE NAME
docker service create --replicas=3 -m-yn-awmeeb-wseebr-vseerrver

web-server.1 web-server.2 web-server.3

Worker Node Worker Node Worker Node

Docker Swarm
SERVICE UPDATE
docker service create –replicas=3 --name web-server my-web-server

docker service update –replicas=4 web-server

web-server.1 web-server.2 web-server.3 web-server.4

Worker Node Worker Node Worker Node Worker Node

Docker Swarm
CODING EXERCISES

• Docker Service Commands


QUIZ
DOCKER STACKS

Mumshad Mannambeth | [email protected]


DOCKER COMPOSE
Public Docker registry - dockerhub
docker run mmumshad/simple-webapp

docker run mongodb

docker run redis:alpine

docker run ansible

docker-compose.yml
services:
web:
image: “mmumshad/simple-webapp"
database:
image: “mongodb“
messaging:
image: "redis:alpine“
orchestration:
image: “ansible“

docker-compose up
DOCKER COMPOSE
docker run mmumshad/simple-webapp docker service create mmumshad/simple-webapp

docker run mongodb docker service create mongodb

docker run redis:alpine docker service create redis

docker run ansible docker service create ansible

docker-compose.yml docker-compose.yml
services: services:
web: web:
image: “mmumshad/simple-webapp" image: “mmumshad/simple-webapp"
database: database:
image: “mongodb“ image: “mongodb“
messaging: messaging:
image: "redis:alpine“ image: "redis:alpine“
orchestration: orchestration:
image: “ansible“ image: “ansible“

docker-compose up docker stack deploy


STACK

Container Container Container


Service

Stack

Container
Service Service

Container
Container Container
Service

Stack
STACK DEFINITION
docker service create mmumshad/simple-webapp

docker service create mongodb

docker service create redis

docker service create ansible

docker-compose.yml
services:
web:
image: "mmumshad/simple-webapp"
database:
image: "mongodb"
messaging:
image: "redis:alpine"
orchestration:
image: "ansible"

docker stack deploy


DOCKER NETWORKING

Mumshad Mannambeth | [email protected]


DEFAULT NETWORKS

Bridge none host

docker run ubuntu docker run Ubuntu –-network=none docker run Ubuntu --network=host

5000 5000

Web Web Web Web


Container Container
Container Container
172.17.0.2 172.17.0.3
172.17.0.1 Web
docker0
Container
172.17.0.4 172.17.0.5

Web Web
Container Container

Docker Host Docker Host Docker Host


OVERLAY NETWORK
docker network create --driver overlay --subnet 10.0.9.0/24 my-overlay-network

docker service create --replicas 2 --network my-overlay-network nginx

Web Web Web Web Web Web


Container Container Container Container Container Container
172.17.0.2 172.17.0.3 172.17.0.2 172.17.0.3 172.17.0.2 172.17.0.3
172.17.0.1 172.17.0.1 172.17.0.1
docker0 docker0 docker0

Overlay Network
10.0.9.0

Docker Host Docker Host Docker Host


INGRESS NETWORK
80

Load Balancer
http://192.168.1.5:80

docker run –p 80:5000 my-web-server

docker service create \


5000 5000

--replicas=2 \ Web Web


Container Container
–p 80:5000 \
172.17.0.2 172.17.0.3
my-web-server

docker0
172.17.0.1

Docker Host
Docker Swarm
INGRESS NETWORK

80 80 80

Load Balancer Load Balancer Load Balancer

Routin g Mesh

5000 5000

Web Web
Container Container

Docker Host Docker Host Docker Host


Docker Swarm
EMBEDDED DNS

Host IP
mysql.connect( 172.17.0.3
mysql ) web mysql web 172.17.0.2
Container Container
172.17.0.2 172.17.0.3 mysql 172.17.0.3
docker0

DNS
Server

127.0.0.11

Docker Host
LEARN MORE
Ingress Network 10.255.0.0/16

10.255.2.139 10.255.2.140
CI/CD

Mumshad Mannambeth | [email protected]


CI – CONTINUOUS INTEGRATION

Test
Feature #1 Build System Framework
Code Repository

✓ Unit Test
✓ Web UI Test
✓ Integration Test
Feature #2
Robot
Framework

BugFix #1

Continuous Integration
CD – CONTINUOUS DELIVERY/DEPLOYMENT

Production Environment
Release
Management

CI

Continuous Delivery

Continuous Deployment
CI/CD - DOCKER

Mumshad Mannambeth | [email protected]


BUILD SYSTEMS – DOCKER SUPPORT

Test
Framework
Build System
Code Release
Repository
my-app:1.0
Docker
Hub
Dockerfile

Robot
Framework

Docker Plugin

Continuous Integration

Continuous Delivery
PUBLIC CLOUD – DOCKER SUPPORT

Google Container Engine


(GKE)
Pivotal Container Service
(PKS)
EC2 Container Service Docker Swarm
(ECS)
DEMO
DOCKER REGISTRY

Mumshad Mannambeth | [email protected]


DOCKER REGISTRY

Public Docker registry - dockerhub


Private Docker Registry

docker push m
lmou
cmas
lhod
s/ my
t: 5-
0c us
00 /t om-app docker push mmumshad/my-custom-app

docker build . –t mmumshad/my-custom-app docker build . –t mmumshad/my-custom-app

docker run -d -p 5000:5000 registry:2

You might also like