Docker Training - PPT
Docker Training - PPT
Agenda
• Overview on Docker
• Docker Volumes
2
Agenda
• Docker Compose
• Docker Machine
• Docker Swarm
• Overview on Kubernetes
3
What is Docker
4
Why would you use docker
control.
5
History
6
Hypervisor based Virtualization
7
Benefits of VM
• Easier to scale
• Rapid elasticity
8
Limitations of VM
• The more VMs you run, the more resources you need
• Guest OS means wasted resources
• Application portability not guaranteed
9
Introduction to Containers
10
Introduction to Containers
11
VM Vs Containers
12
Underlying Technology
13
Advantages of Docker
• Faster Deployments
• Isolation
• Sharing
14
Install Docker
• Docker Engine
• Docker Compose
• Docker Machine.
15
Install Docker on CentOS
• ssh root@IPAddress
• adduser demo - To add a new user
16
Install Docker on CentOS
• Install Docker
• To execute commands without sudo, add demo user to docker group: sudo usermod -
aG docker demo
17
Install Docker-Machine on CentOS
tmp/docker-machine &&
18
Install Docker-Compose on CentOS
19
Commands to Verify the Installation
• docker version
• docker-machine version
• docker-compose version
20
Check the Installation
Server:
• docker-machine version Version: 17.03.1-ce-rc1
API version: 1.27 (minimum version
• docker-machine version 0.10.0, build 76ed2a6 1.12)
Go version: go1.7.5
Git commit: 3476dbf
Built: Wed Mar 15 20:28:18 2017
• docker-compose version OS/Arch: linux/amd64
• docker-compose version 1.11.2, build dfed245 Experimental: true
• docker-py version: 2.1.0
• CPython version: 2.7.12
• OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
21
Docker Components
• Docker Engine
• Images
• Containers
• Registry
• Repository
• Docker Hub
• Docker Orchestration tools
22
Docker Engine
process.
• A REST API which specifies interfaces that programs can use to talk to
23
Docker Engine
24
Client and Server (Daemon)
hosts
25
Docker Architecture
26
Container & Images
Images
• Read only template used to create
containers
• Built by you or other docker users
• Stored in the Docker Hub or your local
registry
Containers
• Runnable instance of a docker image
• Isolated application platform
• Contains everything needed to run your
application
• Based on one or more images
27
Image Layers
28
Docker Registry
29
Docker Orchestration
30
Hands-On
1. Create your first container.
5. Detached Mode
32
Exercise
33
Interactive Mode & Getting Terminal Access
34
Interactive Mode & Getting Terminal Access
• Note: You need to run a terminal process as your command (e.g. /bin/bash)
35
What happens when you run the Container?
When you run this command, Docker Engine does the following:
• Allocates a filesystem and mounts a read-write layer: The container is created in the file
• Allocates a network / bridge interface: Creates a network interface that allows the Docker
36
Exercise
• Run command: docker run centos:7
• docker images
• docker ps
• docker ps -a
• Run in Interactive mode: docker run -it cents:7 /bin/bash
• You will be placed inside the container.
37
Start and Stop Container
39
Exercise
• docker run -it centos:7
• exit container
• You will not see GIT installed, because a new container has been spinned up
40
Starting a Web Server in a Container
41
Running in Detached Mode
42
Container Networking
• Typically, a Docker host comprises multiple Docker containers and hence the
networking has become a crucial component for realizing composite containerized
applications. Docker containers also need to interact and collaborate with local as well
run subcommand.
docker network ls
docker network inspect
43
Networking - Exposing Containers with Port Redirect
automatically
• -p - To assign specific port, use -p
44
Exec Command
45
Exercise
page
46
Docker Volumes
• Docker manages data within the docker container using Docker Volumes.
• For e.g. let’s say that you are running an application that is generating data and it creates files
or writes to a database and so on. Now, even if the container is removed and in the future you
launch another container, you would like that data to still be there
• Until now, all the files that we created in an image or a container are part and parcel of the
Union filesystem. However, the data volume is part of the Docker host filesystem, and it
47
Docker Volumes
48
Docker Volumes
• It is initialized when the container is created. By default, it is not deleted when the
container is stopped.
• Data volumes are designed to persist data, independent of the container’s lifecycle.
Docker therefore never automatically deletes volumes when you remove a container,
nor will it “garbage collect” volumes that are no longer referenced by a container.
• Data volumes can be shared across containers too, and can be mounted in read-only
mode also.
49
Docker Volumes
50
Docker Volumes
(cd /var/lib/docker/<containerID>/_data
docker run -it -v —volumes-from centos2 —name centos3 centos
51
Exercise
Volumes
docker volume ls
Volumes-from
52
Create Your Own Image
• Docker Commit:
• Dockerfile
53
Create Your Own Image
• Docker Commit:
• docker run -it centos:7 /bin/bash
• yum update
• yum install git
• yum install curl
• exit container
• docker commit <containerID> <username><yourreponame>:<tag>
• docker images
• Run the image, and verify if git is installed
• exit
• docker push <username><yourreponame>:<tag>
54
Exercise
• Docker Commit:
• docker run -it centos:7 /bin/bash
• yum update
• yum install git
• yum install curl
• exit container
• docker commit <containerID> <repository:tag>
• docker images
• Run the image, and verify if git is installed
55
Dockerfile
• A Dockerfile is a text document that contains all the commands a user could call on
the command line to assemble an image.
• Using “docker build” users can create an automated build that executes several
command-line instructions in succession.
• The docker build command builds an image from a Dockerfile and a context.
56
Dockerfile
FROM
• The FROM instruction sets the Base image for subsequent instructions
• A valid dockerfile must have a FROM instruction
• FROM can occur multiple times in the dockerfile
CMD
• CMD defines a default command to execute when a container is created
• CMD performs no action during the build image
• Shell and EXEC form
Can only be specifed once in a Dockerfile
FROM ubuntu
CMD echo "This is a test." | wc -
57
Dockerfile
RUN
• Executes a command in a new layer on top of the current image and commit the results
COPY
• The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container
at the path <dest>
• COPY is preferred over ADD
58
Dockerfile
• MAINTAINER - This sets the author for the generated image, MAINTAINER <name>
• ADD <src> <dst> - This copies files from the source to the destination:
59
Dockerfile
• EXPOSE - This exposes the network ports on the container on which it will listen at
runtime
• ENV - This will set the environment variable <key> to <value>. It will be passed all the
future instructions and will persist when a container is run from the resulting image
• VOLUME [“/data”] OR /data - This instruction will create a mount point with the given
name and flag it as mounting the external volume
• USER <username>/<UID> - This sets the username for any of the following run
instructions
60
Dockerfile Examples
Example1 Example2
FROM busybox:latest • FROM centos:7
•RUN yum install -y git
CMD echo Hello World!!
• VOLUME /myvol
• CMD [“git”, “—version”]
Example3
• FROM java:7
• COPY First.java .
• RUN javac First.java
• CMD ["java", "First”]
61
Exercise
Example1
• FROM ubuntu:14.04
• RUN apt-get update
• RUN apt-get -y install git
• CMD [“git”, “—version”]
Example2
• FROM java:7
• COPY First.java .
• RUN javac First.java
• CMD ["java", "First”]
62
Pushing image to DockerHUB
63
Docker Private Repository
• docker run -d -p 5000:5000 --restart=always --name registry registry:2
• systemctl daemon-reload
• docker images - To list down the images you uploaded to private registry
64
Exercise
65
Automated Builds with Docker
66
Automation through dockerHub
Steps
1. Create an application
2. Create Dockerfile for this application to be built. Dockerfile will compile,
build, test and package as required.
3. Create an automated build on Dockerhub (Assuming account already
created)
4. Push the code on GITHUB.
5. This will run the build on dockerhub automatically and create an image
6. This image can be pulled to QA or any other server.
67
Docker Orchestration
• Docker Machine
• Tool that provisions Docker hosts and installs the Docker Engine
on them
• Docker Swarm
• Tool that clusters many Engines and schedules containers
68
Microservices Architecture Vs Monolithic Architecture
69
Docker Compose
• Docker Compose is a tool for defining and running multi-container Docker applications.
• With Compose, you use a Compose file to configure your application's services
• Then, using a single command, you create and start all the services from your configuration.
70
Docker Compose
2. Define the services that make up your app in docker-compose.yml so they can be run
3. Lastly, run docker-compose up and Compose will start and run your entire app.
71
docker-compose.yml
72
HandsOn - Sample Python Example
• docker-compose up
73
HandsOn - Sample Python Example
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
74
HandsOn - Sample Python Example
version: "3"
services:
web:
image: tomcat:7
ports:
- "8080:8080"
volumes:
- ./myvol:/myvol
75
Troubleshooting - Debug Commands
• Docker details:
• docker info
• docker version
• Network debugging
• docker network inspect <networkname/id>
• Basic Swarm Debugging
• docker node ls
76
Docker Daemon Logs
• Ubuntu — /var/log/upstart/docker.log
• Boot2Docker — /var/log/docker.log
77
Troubleshooting Containers
• Troubleshooting Basics
• Command Issues
• Volumes
• Networking
• TLS
78
Troubleshooting Basics
79
Submitting Diagnostics, feedback and GITHUB issues
80
Checking Logs
• In Linux: /var/lib/docker/containers/……
• In Windows; Use the systray menu to view logs:
• To view Docker for Windows latest log, click on the Diagnose & Feedback
menu entry in the systray and then on the Log file link. You see the full can
history of logs in your AppData\Local folder.
81
Troubleshooting Volume Errors
82
Error: Unable to remove file system
• Some container-based utilities, such as Google cAdvisor, mount Docker system directories, such as /var/lib/
docker/, into a container. For instance, the documentation for cadvisor instructs you to run the cadvisor container.
• When you bind-mount /var/lib/docker/, this effectively mounts all resources of all other runningcontainers as
filesystems within the container which mounts/var/lib/docker/. When you attempt to remove any of these
containers, the removal attempt may fail with an error like the following:
containers/74bef250361c7817bee19349c93139621b272bc8f654ae112dd4eb9652af9515/shm: Device or
resource busy
• To work around this problem, stop the container which bind-mounts/var/lib/docker and try again to remove the
other container.
83
Permission Errors on data directories for shared volumes
of 0755 (read, write, executepermissions for user, read and execute for group).
If you are working with applications that require permissions different than this
the permissions to 0755 so that the directory cannot be listed by other users.
84
Volume Mounting requires shared drives for Linux Conatiners
If you are using mounted volumes and get runtime errors indicating an
application file is not found, a volume mount is denied, or a service cannot start
(e.g., with Docker Compose), you might need to enable shared drives.Volume
mounting requires shared drives for Linux containers (not for Windows
containers). Go to -->Settings --> Shared Drives and share the drive that
85
Verify Domain User has permission for shared drives (volumes)
Permissions to access shared drives are tied to the username and password you
use to set up shared drives. If you run docker commands and tasks under a
different username than the one used to set up shared drives, your containers
will not have permissions to access the mounted volumes. The volumes will
show as empty.
The solution to this is to switch to the domain user account and reset credentials on
shared drives.
86
Docker Networking
Overlay network
87
Troubleshooting - Using Sysdig to Debug
From a top level, what sysdig brings to our container management is this:
• Ability to access and review processes (inclusive of internal and external PIDs) in each container
• Ability to easily filter sets of containers for process review and analysis
Sysdig provides data on CPU usage, I/O, logs, networking, performance, security, and system state.
88
Docker Orchestration
• Docker Machine
• Tool that provisions Docker hosts and installs the Docker Engine
on them
• Docker Swarm
• Tool that clusters many Engines and schedules containers
• Kubernetes
• Docker cluster management tool by Google
89
Docker Machine
Docker Machine is a tool that lets you create a virtual host and install
Docker Engine on virtual hosts, and manage the hosts with docker-
machine commands.
You can use Machine to create Docker hosts on your local Mac or Windows
90
Docker Machine
secret-key 8T9******aws-sandbox
91
Docker Machine
Env Setup
docker-machine env machinename
docker-machine env -u
eval $(docker-machine env machinename) - This configures our docker CLI utility to
• docker-machine ip <name>
92
Docker Machine
• docker-machine ls
• docker-machine ip <name>
93
Clustering & Load Balancing with Docker Swarm
containers
94
Swarm Mode
• Rolling updates
95
Clustering & Load Balancing with Docker Swarm
network
96
Create Cluster
• Deploy Service:
• docker service create --replicas 5 -p 80:80 --name myservice
nginx
• docker service ls
• docker service ps myservice 98
Command Summary
100
Scale & Manage Service
• Inspecting Nodes: docker node inspect self, docker node inspect worker1, docker
web
Manage Cluster Nodes
• Managing nodes
102
Deploying Services using Compose YAML file
103
Kubernetes
104
Key Concepts of Kubernetes
• Master
• Node
• Pod - A group of Containers
• Kubelet - Container Agent
• Services
• Deployments
• Replica Sets
• Labels - Labels for identifying pods
• Selectors
105
Kubelet
Kubelet, a process responsible for communication between the Kubernetes Master and
the Nodes; it manages the Pods and the containers running on a machine.
The kubelet is the primary “node agent” that runs on each node. The kubelet works in
terms of a PodSpec. A PodSpec is a YAML or JSON object that describes a pod. The
kubelet takes a set of PodSpecs that are provided through various mechanisms (primarily
through the apiserver) and ensures that the containers described in those PodSpecs are
running and healthy. The kubelet doesn’t manage containers which were not created by
Kubernetes.
106
Nodes
• A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be
• A Node can have multiple pods, and the Kubernetes master automatically handles
scheduling the pods across the Nodes in the cluster. The Master's automatic scheduling
107
Nodes
108
Pods
containers and some shared resources for those containers.Those resources include:
Information about how to run each container, such as the container image version
109
Kubernetes Cluster
• Minikube - For testing & Learning purpose
• Custom Cluster from Scratch
• Hosted Solutions
• Google Container Engine
• Azure Container Service
• IBM Bluemix Container Service
• Turn-key cloud Solutions
• AWS Ec2
• Asure
• CenturyLink Cloud
• IBM Bluemix
110
Introduction to Minikube & Kubectl
• Minikube is a lightweight Kubernetes implementation that creates a VM on your
local machine and configures a simple cluster containing only one node. Minikube is available
for Linux, Mac OS and Windows systems.
• The Minikube CLI provides basic bootstrapping operations for working with your
cluster, including start, stop, status, and delete.
• kubectl version - To check if kubectl is installed and running. The client version is
the kubectl version; the server version is the Kubernetes version installed on the
master.
111
HandsOn - Create a Kubernetes Cluster
• minikube version
• minikube start - This command will create a vm on Virtual Box and setup a kubernetes
cluster
• kubectl config use-context minikube - This will set the context of your machine to
minikube.
• kubectl cluster-info - This command give detail information about the cluster
• kubectl get nodes - This command shows all nodes that can be used to host our
applications.
112
Create a Deployment
113
Scaling Up
114
Rolling Out Changes
115
Exercise
• Scale up to 10 replicas
116
Deployment
• Deployment: A Deployment provides declarative updates for Pods and ReplicaSets (the
Deployment object, and the Deployment controller will change the actual state to the
desired state at a controlled rate for you. You can define Deployments to create new
117
Replica Sets
provides declarative updates to pods along with a lot of other useful features.
unless you require custom update orchestration or don’t require updates at all.
118
Creating a Service
We have pods running nginx in a flat, cluster wide, address space. In theory, you could talk to
these pods directly, but what happens when a node dies? The pods die with it, and the
Deployment will create new ones, with different IPs. This is the problem a Service solves.
A Kubernetes Service is an abstraction which defines a logical set of Pods running somewhere
in your cluster, that all provide the same functionality. When created, each Service is assigned a
unique IP address (also called clusterIP). This address is tied to the lifespan of the Service, and
will not change while the Service is alive. Pods can be configured to talk to the Service, and
know that communication to the Service will be automatically load-balanced out to some pod
that is a member of the Service.
You can create a Service for your 2 nginx replicas with kubectl expose:
119
Service
• Although Pods each have a unique IP address, those IPs are not exposed outside the cluster
without a Service.
• A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by
which to access them - sometimes called a micro-service.
• Services match a set of Pods using labels and selectors, a grouping primitive that allows
logical operation on objects in Kubernetes.
• Although Pods each have a unique IP address, those IPs are not exposed outside the cluster
without a Service.
120
Service
121
Service
122
Expose a Service
Kubernetes allows you to define 3 types of services using the ServiceType field in its yaml file.
123
Expose a Service
target-port=80
• curl http://<ClusterIP>:<port>
port=80
124
minikube - Node - IP
service IP
podIP
125
Creating Deployment using YAML
apiVersion: apps/v1beta1
kind: Deployment
• Create an nginx pod, and metadata:
name: my-nginx
note that it has a
spec:
template:
specification: metadata:
labels:
• This makes it accessible run: my-nginx
spec:
from any node in your
containers:
image: nginx
ports:
- containerPort: 80
126
Creating a Service using YAML
name: my-nginx
labels:
You should now be able to curl the nginx Service on run: my-nginx
type: NodePort
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
127
• Exercise
128
sKillspeed
for the serious leamer