Unit 1
Docker is a very powerful containerization platform – not to mention very popular. It is used extensively and provides a large number of integrations, including GitHub, Jenkins, Kubernetes and Terraform. We will be exploring the concepts and how best we can leverage Docker.
Presented by – Seshagiri Sriram
Getting Started Docker
www.guvi.in
2
Why do we need Docker?
Shipping Transportation Challenges
www.guvi.in
3
Problem
www.guvi.in
4
Solution
Advantages :
Shipping Transportation Challenges
Relevance in IT Industry
www.guvi.in
5
Current IT Industry Problem :
www.guvi.in
6
Solution
Much as the intermodal containers simplified the transportation of goods, Docker containers simplify the transportation of software applications.
Developers can concentrate on building the application and shipping it through testing and production without worrying about differences in environment and dependencies.
Operations can focus on the core issues of running containers, such as allocating resources, starting and stopping containers, and migrating them between servers.
Relevance in IT Industry
How Does Docker Fit Into Devops Eco-System
www.guvi.in
7
Docker forms a lethal combination with Jenkins:
The lethal combination of Jenkins and Docker is proving to be very valuable for DevOps teams.
By leveraging the tight integration with source code control mechanisms such as Git, Jenkins can initiate a build process each time a developer commits his code. This process results in a new Docker image which is instantly available across environments.
Organizations are deploying private Docker registries to publish and maintain their internal Docker images
Docker makes it easier to test exactly what you deploy:
Docker containers encourage a central tenet of continuous delivery – reuse the same binaries at each step of the pipeline to ensure no errors are introduced in the build process itself. The complexities of the configuration Management tools are considerable and complicated to manage. Most of the problems stem from complex integrations.
Example: You have to make sure that you are using the same libraries and other important elements every time.
Making sure that all those environments are perfectly aligned can be a lot of work.
Docker solves that because you are starting with a given base image, and it’s guaranteed to be the same image everywhere.
www.guvi.in
8
Docker has revolutionized software packaging and deployment:
Instead of deploying the final set of artifacts such as EXE and JAR files to the target environment, ops teams are now packaging the entire application as a Docker Image.
This image shares the same build version before getting published to a central registry.
It is then picked up by various environments – development, testing, staging, and production – for final deployment
Docker containers provide the basis for immutable infrastructure:
Applications can be added, removed, cloned and/or its constituencies can change, without leaving any residues behind. Whatever mess a failed deployment can cause is constrained within the container.
Deleting and adding become so much easier that you stop thinking about how to update the state of a running application.
How Does Docker Fit Into Devops Eco-System
www.guvi.in
9
What is Docker ?
Docker
www.guvi.in
10
Docker is a Containerization platform which packages your application and all its dependencies together in the form of Containers so as to ensure that your application works seamlessly in any environment be it Development or Test or Production.
Host OS
Docker Engine
App 1
BINS / LIBS
Container 1
Container 2
App 2
BINS / LIBS
www.guvi.in
11
Containers v/s Virtual Machines
Virtualization; Adopted By VMs
www.guvi.in
12
Host OS
Hypervisor
Guest OS
Guest OS
Guest OS
BINS�&�LIBS
BINS� &� LIBS
BINS �&�LIBS
App 1
App 2
App 3
Virtualization Technique
VMs
Advantages
Disadvantages
Containerization
www.guvi.in
13
Host OS
App 3
Container Engine
App 1
App 2
Containerization Technique
BINS & LIBS
BINS�&�LIBS
Containers
BINS & LIBS
Advantages Over Virtualization
Note: Containerization Is Just Virtualization At The OS Level
www.guvi.in
14
Benefits of Docker over VM’s
VM vs. Docker
www.guvi.in
15
Resource/Memory Utilization
www.guvi.in
16
Total Memory:
16 GB
4 GB
2 GB
🡪
Memory Used: 9 GB
Memory wasted: 7 GB
VM 1
3 GB
1 GB
2 GB
4 GB
VM 2
VM 3
In case of Virtual Machines
🡪
6 GB
4 GB
6 GB
7 Gb of Memory is blocked and cannot be allotted to a new VM
Size
Start-up
Integration
www.guvi.in
17
Total Memory:
16 GB
4 GB
2 GB
🡪
Memory Used: 9 GB
Memory wasted: 7 GB
VM 1
3 GB
1 GB
2 GB
4 GB
VM 2
VM 3
Memory Allotted: 4 GB
App 1
App 2
App 3
Memory Used: 9 GB
In case of Virtual Machines
In case of Docker
🡪
🡪
6 GB
4 GB
6 GB
Total Memory:
16 GB
Memory Allotted: 35 GB
Memory Allotted: 10 GB
7 Gb of Memory is blocked and cannot be allotted to a new VM
Only 9 GB memory utilized;�7 GB can be allotted to a new Container
Size
Start-up
Integration
4 GB
3 GB
2 GB
Resource/Memory Utilization
www.guvi.in
18
Host OS Kernel
Guest OS
Guest OS
BINS & LIBS
BINS & LIBS
Ruby v1
Ruby v2
Host OS Kernel
Ruby v1
Ruby v2
BINS & LIBS
New Builds 🡪 Multiple OS 🡪 Separate Libraries 🡪 Heavy 🡪 More Time
New Builds 🡪 Same OS 🡪 Separate Libraries 🡪 Lightweight 🡪 Less Time
Build 1
Build 2
Build 1
Build 2
In case of Virtual Machines
In case of Docker
Size
Start-up
Integration
BINS & LIBS
Container
Virtual Machines
Building and Deployment
Integration in VMs
www.guvi.in
19
Size
Start-up
Integration
Integration In Virtual Machines Is Possible, But:
Jenkins
When to use what?
www.guvi.in
20
When to use what?
www.guvi.in
21
Docker – A DevOps Ally
www.guvi.in
22
Size
Start-up
Integration
Jenkins
Integration in Docker is Faster, Cheap & Easily Scalable
www.guvi.in
23
Architecture
Docker Architecture
www.guvi.in
24
docker pull
docker build
docker run
Docker Daemon
Client
Docker_Host
Registry
Containers
Images
Docker Daemon
www.guvi.in
25
Why Use Docker Daemon?
Docker Client
www.guvi.in
26
Why Use Docker Daemon?
Docker Registry
www.guvi.in
27
Why Use Docker Registries?
Private or Public Registry?
www.guvi.in
28
Which Public Registry?
www.guvi.in
29
Docker Images & Containers
www.guvi.in
30
Docker Images
Docker Containers
run
Docker Architecture In Action
www.guvi.in
31
docker pull
docker build
docker run
Docker Daemon
Client
Docker_Host
Registry
Containers
Images
build
Docker Architecture In Action
www.guvi.in
32
docker pull
docker build
docker run
Docker Daemon
Client
Docker_Host
Registry
Containers
Images
build
pull
Docker Architecture In Action
www.guvi.in
33
docker pull
docker build
docker run
Docker Daemon
Client
Docker_Host
Registry
Containers
Images
build
pull
run
www.guvi.in
34
Docker’s Plugins And Plumbing
Docker - Plugin’s And Plumbing
www.guvi.in
35
The Docker engine and the Docker Hub do not in-and-of themselves constitute a complete solution for working with containers
Docker API
Docker Compose
Docker Machine
Docker Kitematic
Docker - Plugin’s And Plumbing
www.guvi.in
36
Docker Trusted Registry
Docker Swarm – Docker’s Clustering Solution
Orchestration and cluster management
www.guvi.in
37
Docker Topics
Docker Topics
www.guvi.in
38
www.guvi.in
39
Working with Docker Images
What is an Image?
www.guvi.in
40
Docker File
Image
Container
Few Basic Commands
www.guvi.in
41
Few Basic Commands
www.guvi.in
42
Create Our First Image: Hello-World
www.guvi.in
43
Search – Hello World
www.guvi.in
44
Image Pull – Hello World
www.guvi.in
45
Result Details:
Image History
www.guvi.in
46
Image History – Why
www.guvi.in
47
Tagging
www.guvi.in
48
Tagging – Types and best Practices
www.guvi.in
49
Image Distribution - Repositories
www.guvi.in
50
Hierarchy For Image Storing
www.guvi.in
51
Image Storing:
Registry
A service responsible for hosting and distributing images. The default registry is the Docker Hub.
Repository
A collection of related images (usually providing different versions of the same application or service)
Tag
An alphanumeric identifier attached to images within a repository (e.g., 14.04 or stable)
Best practices for Image Storing
www.guvi.in
52
Pushing Images to Docker Hub
www.guvi.in
53
Pushing Images to Docker Hub(Contd..)
www.guvi.in
54
Pushing Images to Docker Hub(Contd..)
www.guvi.in
55
Pushing Images to Docker Hub(Contd..)
www.guvi.in
56
Image Namespaces
www.guvi.in
57
Image Namespaces
www.guvi.in
58
Image Selection – Base Image
www.guvi.in
59
www.guvi.in
60
www.guvi.in
61
www.guvi.in
62
Containers
What we will cover
www.guvi.in
63
Connection Modes
www.guvi.in
64
Connection Modes
Detached Mode
Root User Mode
Detached Mode Vs Root Mode
www.guvi.in
65
Detached Mode
Root User Mode
Detached Mode Vs Root Mode(Contd..)
www.guvi.in
66
Detached Mode
Root User Mode
Outputs in connected modes
www.guvi.in
67
Selection between Connection Modes
www.guvi.in
68
Docker File System - Initiate A Container
www.guvi.in
69
Container can be initiated using both the “Image name” as well as “Image ID” along with the required tag
Examine Existing Containers
www.guvi.in
70
Examine Existing Containers
www.guvi.in
71
Question: What happened to the container we initiated from our image “hello-world?
Display inference:
This container with the name “pedantic_jepsen” , with CONTAINER ID (7a45c308c3fa) ,got created 3 minutes ago from the and exited 3 minutes ago as soon as its processes were executed. e
Naming Containers
www.guvi.in
72
Getting Attached To a Container
www.guvi.in
73
www.guvi.in
74
By default a container exists as soon as its processes are executed
Restarting A Container
www.guvi.in
75
Inspect Containers
www.guvi.in
76
Why would we want to inspect containers?
www.guvi.in
77
Removing Images: Use Cases
www.guvi.in
78
With Associated Containers
Without Associated Containers
Removing by force
Removing Images: With Associated Containers
www.guvi.in
79
With Associated Containers
Without Associated Containers
Removing by force
Removing Images: Without Associated Containers
www.guvi.in
80
With Associated Containers
Without Associated Containers
Removing by force
Removing Images: Removing by force
www.guvi.in
81
With Associated Containers
Without Associated Containers
Removing by force
When to use remove with –f option?
www.guvi.in
82
www.guvi.in
83
Docker Networking
Default Networks
www.guvi.in
84
Default Networks - Types
www.guvi.in
85
Default Bridge Network Details
www.guvi.in
86
Default Bridge Network Details
www.guvi.in
87
When to use the bridge network?
www.guvi.in
88
When to use the user defined bridge network?
www.guvi.in
89
When to use the user defined bridge network?
www.guvi.in
90
Exposing Ports
www.guvi.in
91
Exposing Ports(Contd..)
www.guvi.in
92
Exposing Ports: Use Case
www.guvi.in
93
Exposing Ports
Free Ports
Binding Ports
Connection Through Host Port
www.guvi.in
94
Getting Ports – Binding Ports
www.guvi.in
95
Free or Binding ports?
www.guvi.in
96
www.guvi.in
97
Docker Volumes
www.guvi.in
98
Volumes – Use Cases
www.guvi.in
99
To Keep Data Persistent
Share Data Between Host and Container
Share Data With Other Containers
Docker Data Volumes: Use Case
Volumes – Use Cases
www.guvi.in
100
To Keep Data Persistent
Share Data Between Host and Container
Share Data With Other Containers
docker create -v /tmp --name datacontainer Ubuntu
docker run -t -i --volumes-from datacontainer ubuntu /bin/bash
Volumes – Use Cases
www.guvi.in
101
To Keep Data Persistent
Share Data Between Host and Container
Share Data With Other Containers
docker run -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 -i nginx
Volumes – Use Cases
www.guvi.in
102
To Keep Data Persistent
Share Data Between Host and Container
Share Data With Other Containers
Volumes – Use Cases
www.guvi.in
103
To Keep Data Persistent
Share Data Between Host and Container
Share Data With Other Containers
# Example of using flocker
docker run -d -P --volume-driver=flocker -v my-named-volume:/webapp --name web training/webapp python app.py
# (or) Create a volume
docker volume create -d flocker -o size=20GB my-named-volume �# use it..
docker run -d -P -v my-named-volume:/webapp --name web training/webapp python app.py
Which is the best way?
www.guvi.in
104
www.guvi.in
105
Docker Files
Docker File
www.guvi.in
106
Docker File – Creation Steps
www.guvi.in
107
Docker File Main Sections
www.guvi.in
108
Docker File – FROM and MAINTAINER
www.guvi.in
109
FROM:
Docker File – FROM and MAINTAINER
www.guvi.in
110
MAINTAINER:
Docker File – RUN
www.guvi.in
111
RUN:
Example:
FROM debian:stable
MAINTAINER docker<sriram@gmail.com >
RUN apt –get update
RUN apt –get upgrade
Docker File – RUN(Contd..)
www.guvi.in
112
RUN:
Docker File – ENV
www.guvi.in
113
ENV:
Example:
FROM debian:stable
MAINTAINER docker<sriram@gmail.com >
RUN apt –get update
RUN apt –get upgrade
ENV MYVALUE -test
Docker File – EXPOSE
www.guvi.in
114
EXPOSE:
Example:
FROM debian:stable
MAINTAINER docker<sriram@gmail.com >
RUN apt –get update
RUN apt –get upgrade
ENV MYVALUE -test
EXPOSE 80
EXPOSE 24
Docker File – CMD
www.guvi.in
115
CMD:
Example:
FROM debian:stable
MAINTAINER docker<sriram@gmail.com >
RUN apt –get update && apt –get upgrade
ENV MYVALUE -test
EXPOSE 80
EXPOSE 24
CMD [“usr/sbin/apache2ctl”, “-D”, “FOREGROUND”]
Create Docker File - Demo
www.guvi.in
116
Questions
www.guvi.in
117
Should I include my code with COPY/ADD or a volume?
www.guvi.in
118
Docker Compose
www.guvi.in
119
Docker Compose: Multi Container Applications
www.guvi.in
120
Without using Compose
With Compose
Docker Compose: Multi Container Applications
www.guvi.in
121
Without using Compose
With Compose
docker run -e MYSQL_ROOT_PASSWORD=<pass> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mysql:latest
docker run -e WORDPRESS_DB_PASSWORD=<pass> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html
-d wordpress
services:
db:
image: mysql:5.7 volumes:
- db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE: wordpress wordpress:
depends_on:
- db
image: wordpress:latest ports:
- "80:80"
environment: WORDPRESS_DB_PASSWORD=<pass>
Docker Compose
www.guvi.in
122
Compose works in all environments: production, staging, development, testing, as well as CI workflows.
A tool for defining and running multi- container Docker applications
With Compose, you use a YAML file to configure your application’s services.
With a single command, you create and start all the services from your configuration
Docker Compose
www.guvi.in
123
Define your app’s environment with a Dockerfile
Define the services that make up your app in Docker Compose file
Run the CLI:
docker-compose up
Docker Compose: Multi Container Applications
www.guvi.in
124
C
Services
Volumes
Networking
B
A
Docker Compose: Multi Container Applications
www.guvi.in
125
containers:
web:
build: .
command: python app.py ports:
- "5000:5000"
volumes:
redis:
image: redis:latest
command: redis-server --appendonly yes
Docker Compose: Multi Container Applications
www.guvi.in
126
Docker Compose: Multi Container Applications
www.guvi.in
127
Using .env file
Docker Compose: Multi Container Applications
www.guvi.in
128
Commands:
docker compose up
docker compose down
docker compose run –e DEBUG=1 <services>
Docker Compose: Multi Container Applications
www.guvi.in
129
It joins the network myapp_default under the
name web.
Docker Compose: Multi Container Applications
www.guvi.in
130
www.guvi.in
131
Q &A
www.guvi.in
132
We're done!
Thank you for your time and participation.