Docker For Beginners Mumshad Mannambeth
Docker For Beginners Mumshad Mannambeth
Docker For Beginners Mumshad Mannambeth
for beginners
MUMSHAD
MANNAMBETH w w w . k o d e k l o u d . c o m
Objectives
➢ What are Containers?
➢ What is Docker?
➢ Why do you need it?
➢ What can it do?
➢ Docker Swarm
➢ Docker vs Kubernetes
docker
overview
Why do you need docker?
Web Server Database Messaging Orchestration
• Compatibility/Dependency
• Long setup time
• Different Dev/Test/Prod
environments
The Matrix from Hell !!
Libraries Dependencies
OS
Hardware Infrastructure
What can it do?
Container Container Container Container
Applications
• Run each service with its
own dependencies in
separate containers
Docker
?
OS
Hardware Infrastructure
What are containers?
Docker
OS Kernel
Operating System
OS
OS Kernel
Sharing the kernel
Docker
OS - Ubuntu
Containers vs Virtual Machines
✓
✓
GB ✓
✓ MB
✓ ✓
Utilization Size Boot up Utilization Size Boot up
Docker
Hypervisor
OS
Docker Docker
OS OS
Hypervisor
OS
Hardware Infrastructure
Hardware Infrastructure
How is it done? Public Docker registry - dockerhub
Docker Container #1
Docker Image
Docker Container #3
d o c k e r
Getting Started
Docker Editions
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
796856ac413d nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds silly_sammet
cff8ac918a2f redis "docker-entrypoint.s…" 6 seconds ago Exited (0) 3 seconds ago relaxed_aryabhata
STOP – stop a container
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
796856ac413d nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 80/tcp silly_sammet
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
796856ac413d nginx "nginx -g 'daemon of…" 7 seconds ago Exited (0) 3 seconds ago silly_sammet
cff8ac918a2f redis "docker-entrypoint.s…" 6 seconds ago Exited (0) 3 seconds ago relaxed_aryabhata
Rm – Remove a container
docker rm silly_sammet
silly_sammet
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
cff8ac918a2f redis "docker-entrypoint.s…" 6 seconds ago Exited (0) 3 seconds ago relaxed_aryabhata
images – List images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f68d6e55e065 4 days ago 109MB
redis latest 4760dc956b2d 15 months ago 107MB
ubuntu latest f975c5035748 16 months ago 112MB
alpine latest 3fd9065eaf02 18 months ago 4.14MB
rmi – Remove images
docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223a
Deleted: sha256:f68d6e55e06520f152403e6d96d0de5c9790a89b4cfc99f4626f68146fa1dbdc
Deleted: sha256:1b0c768769e2bb66e74a205317ba531473781a78b77feef8ea6fd7be7f4044e1
Deleted: sha256:34138fb60020a180e512485fb96fd42e286fb0d86cf1fa2506b11ff6b945b03f
Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e
docker ps
docker ps -a
docker ps
docker ps -a
5
4
3
2
1
Exec – execute a command
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
538d037f94a7 ubuntu "sleep 100“ 6 seconds ago Up 4 seconds distracted_mcclintock
run
Run – tag
docker run redis
Using default tag: latest
latest: Pulling from library/redis
f5d23c7fed46: Pull complete
Status: Downloaded newer image for redis:latest
IP: 192.168.1.5
http://172.17.0.2:5000 Internal IP
5000 5000 5000
Data
docker run –v /opt/datadir:/var/lib/mysql mysql /var/lib/mysql
/opt/datadir
MYSQL
Docker Container
Docker Host
Inspect Container
docker inspect blissful_hopper
[
{
"Id": "35505f7810d17291261a43391d4b6c0846594d415ce4f4d0a6ffbf9cc5109048",
"Name": "/blissful_hopper",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
},
"Mounts": [],
"Config": {
"Entrypoint": [
"python",
"app.py"
],
},
"NetworkSettings": {..}
}
]
Container Logs
docker logs blissful_hopper
This is a sample web application that displays a colored background.
A color can be specified in two ways.
import os
from flask import Flask
app = Flask(__name__)
…
…
color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
python app.py
Environment Variables
app.py
import os
from flask import Flask
app = Flask(__name__)
…
…
color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
Environment Variables
app.py
import os
from flask import Flask
app = Flask(__name__)
…
…
color = os.environ.get('APP_COLOR')
"red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
app.py
import os
from flask import Flask
app = Flask(__name__)
…
…
os.environ.get('APP_COLOR')
color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)
if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
simple-webapp-color
docker run -e APP_COLOR=blue
ENV Variables in Docker
docker run -e APP_COLOR=blue simple-webapp-color
"Mounts": [],
"Config": {
"Env": [
"APP_COLOR=blue",
"LANG=C.UTF-8",
"GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D",
"PYTHON_VERSION=3.6.6",
"PYTHON_PIP_VERSION=18.1"
],
"Entrypoint": [
"python",
"app.py"
],
}
]
d o c k e r
images
What am I containerizing?
How to create my own image?
Dockerfile
COPY . /opt/source-code
4. Install Python dependencies using pip
INSTRUCTION ARGUMENT
Dockerfile
Start from a base OS or
FROM Ubuntu another image
Containerize Everything!!!
docker
CMD
vs
ENT RY POINT
docker run ubuntu
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
45aacca36850 ubuntu "/bin/bash" 43 seconds ago Exited (0) 41 seconds ago
docker run ubuntu [COMMAND]
5
4
3
2
1
FROM Ubuntu
CMD sleep 5
CMD sleep 5
Command at Startup: sleep 10
FROM Ubuntu
docker run ubuntu-sleeper 10
10
sleep
ENTRYPOINT [“sleep”]
Command at Startup:
docker run ubuntu-sleeper
sleep: missing operand
Try 'sleep --help' for more information.
Command at Startup:
FROM Ubuntu docker run ubuntu-sleeper
sleep: missing operand
Try 'sleep --help' for more information.
ENTRYPOINT [“sleep”]
sleep
CMD [“5”]
5
Command at Startup:
10
docker run ubuntu-sleeper 10
Command at Startup:
sleep2.0 10
docker run --entrypoint sleep2.0 ubuntu-sleeper 10
Command at Startup:
d o c k e r
networking
Default networks
Bridge none host
docker run ubuntu docker run Ubuntu –-network=none docker run Ubuntu --network=host
5000 5000
Web Web
Container Container
172.17.0.4 172.17.0.5
182.18.0.2 docker network ls
Web Web
Container Container
Docker Host
Inspect Network
docker inspect blissful_hopper
[
{
"Id": "35505f7810d17291261a43391d4b6c0846594d415ce4f4d0a6ffbf9cc5109048",
"Name": "/blissful_hopper",
"NetworkSettings": {
"Bridge": "",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.6",
"MacAddress": "02:42:ac:11:00:06",
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.6",
"MacAddress": "02:42:ac:11:00:06",
}
}
}
]
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
d o c k e r
storage
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
Read Write
Container Layer
Layer 6. Container Layer
Read Only
Layer 5. Update Entrypoint with “flask” command
Layer 4. Source code
Read Write
Container Layer
temp.txt
Read Only
/var/lib/mysql /var/lib/mysql
data_volum
e mysql
/var/lib/docker/volumes /data
Read Only
mysql – image layer
Docker Host
Storage drivers
• AUFS
• ZFS
• BTRFS
• Device Mapper
• Overlay
• Overlay2
d o c k e r
compose
Docker compose Public Docker registry - dockerhub
docker run mmumshad/simple-webapp
docker-compose.yml
services:
web:
image: “mmumshad/simple-webapp"
database:
image: “mongodb“
messaging:
image: "redis:alpine“
orchestration:
image: “ansible“
docker-compose up
Sample application – voting application
voting-app result-app
python NodeJS
C
in-memory DB db
PostgreSQL
CATS DOGS
0
1 0
worker
.NET
docker run --links
docker run -d --name=redis redis voting-app result-app
docker run -d --name=db
worker
! Deprecation Warning
Docker compose - build
docker-compose.yml docker-compose.yml
redis: redis:
image: redis image: redis
db: db:
image: postgres:9.4 image: postgres:9.4
vote: vote:
image: voting-app build: ./vote
ports: ports:
- 5000:80 - 5000:80
links: links:
- redis - redis
result: result:
image: result build: ./result
ports: ports:
- 5001:80 - 5001:80
links: links:
- db - db
worker: worker:
image: worker build: ./worker
links: links:
- db - db
- redis - redis
Docker compose - versions
docker-compose.yml docker-compose.yml docker-compose.yml
registry
Image
Image
Login Succeeded
engine
Docker Engine
Docker Engine Laptop
Docker CLI
REST API
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
CPU Memory
On Windows
Docker on windows
1. Docker on Windows using Docker Toolbox
2. Docker Desktop 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 Desktop for Windows
Kernel Kernel
Base Images:
• Nano Server
Support
• Windows Server 2016
• Nano Server
• Windows 10 Professional and Enterprise (Hyper-V Isolated
Containers)
VirtualBox Or Hyper-V
d o c k e r
On Mac
Docker on Mac
1. Docker on Mac using Docker Toolbox
2. Docker Desktop for Mac
1. Docker toolbox
• macOS 10.8 “Mountain Lion” or newer
• Oracle Virtualbox
• Docker Engine
• Docker Machine
• Docker Compose
• Kitematic GUI
2. Docker Desktop for Mac
Linux Containers
container
orchestration
Why Orchestrate? Public Docker registry - dockerhub
Docker Swarm
Docker swarm
Docker Swarm
MySQL
Container
docker swarm init docker swarm join docker swarm join docker swarm join
--token <token> --token <token> --token <token>
Docker Swarm
kubernetes
kubectl rolling-update my-web-server --rollback
A B
Kubernetes Cluster
Network
Storage
Nodes (Minions)
Node
Cluster
Container
API Key-value store
Controller
Scheduler
kubelet
Kube
etcd
Runtime
Server
Master vs Worker Nodes
etcd
controller
scheduler
Container Runtime
kubectl cluster-info