You are on page 1of 24

3/31/2023

DevOps

Lecture
Containerization
using Docker
By
Asad Hanif

Asad Hanif 1

DevOps

Declaimer
Contents of the lecture are taken from following links

 https://docs.docker.com/get-started/

 https://www.youtube.com/playlist?list=PLoz1vq3JRiWNZBHNOf8uGuXaYTTogQ
A0t

 https://www.youtube.com/playlist?list=PL0hSJrxggIQoKLETBSmgbbvE4FO_eE
goB

 https://www.youtube.com/playlist?list=PLyvfcZJ0pCaKOt1jWmsMTuGBq76EPu
kMh

 https://devopscube.com/what-is-docker/

 https://devopscube.com/build-docker-image/
Asad Hanif 2

1
3/31/2023

DevOps

Going Back in Time


Traditional Deployment era
 Early on, organizations ran applications on
App App App App
physical servers.
Libraries Dependencies
 There was no way to define resource
Operating System
boundaries for applications in a physical server,
Hardware Infrastructure
and this caused resource allocation issues.
Physical Machine / Server

Asad Hanif 3

DevOps

Going Back in Time


Traditional Deployment era
 For example, if multiple applications run on a
App App App App
physical server, there can be instances where
Libraries Dependencies
one application would take up most of the
Operating System
resources, and as a result, the other
Hardware Infrastructure
applications would underperform.
Physical Machine / Server

Asad Hanif 4

2
3/31/2023

DevOps

Going Back in Time


Traditional Deployment era
 A solution for this would be to run each
App App App App
application on a different physical server.
Libraries Dependencies
 But this did not scale as resources were
Operating System
underutilized, and it was expensive for
Hardware Infrastructure
organizations to maintain many physical
Physical Machine / Server
servers.

Asad Hanif 5

DevOps

Going Back in Time


Virtualized Deployment era
App App App App
 As a solution, virtualization was introduced. Bin / Library Bin / Library

 It allows us to run multiple Virtual Machines OS OS

(VMs) on a single physical server's CPU. Virtual Machine Virtual Machine

Hypervisor
 Virtualization allows applications to be isolated
Host Operating System
between VMs.
Hardware Infrastructure

Physical Machine / Server

Asad Hanif 6

3
3/31/2023

DevOps

Going Back in Time


Virtualized Deployment era
App App App App
 It provides a level of security as the information of
Bin / Library Bin / Library
one application cannot be freely accessed by
OS OS
another application.
Virtual Machine Virtual Machine
 Virtualization allows better utilization of resources
Hypervisor
in a physical server
Host Operating System
 It allows better scalability because an application
Hardware Infrastructure
can be added or updated easily, reduces hardware
costs, and much more. Physical Machine / Server

Asad Hanif 7

DevOps

Going Back in Time


Virtualized Deployment era
App App App App
 With virtualization we can present a set of Bin / Library Bin / Library
physical resources as a cluster of disposable OS OS
virtual machines. Virtual Machine Virtual Machine

Hypervisor
 Each VM is a full machine running all the
Host Operating System
components, including its own operating
Hardware Infrastructure
system, on top of the virtualized hardware.
Physical Machine / Server

Asad Hanif 8

4
3/31/2023

DevOps

Going Back in Time


Container Deployment era
App App
 Containers are similar to VMs, but they have
Bin / Library Bin / Library
relaxed isolation properties to share the
Container Container
Operating System (OS) among the
Container Runtime
applications.
Host Operating System
 Therefore, containers are considered Hardware Infrastructure
lightweight. Physical Machine / Server

Asad Hanif 9

DevOps

Going Back in Time


Container Deployment era
App App
 Similar to a VM, a container has its own
Bin / Library Bin / Library
filesystem, share of CPU, memory, process
Container Container
space, and more.
Container Runtime
 As they are decoupled from the underlying Host Operating System
infrastructure, they are portable across clouds Hardware Infrastructure
and OS distributions. Physical Machine / Server

Asad Hanif 10

10

5
3/31/2023

DevOps

Benefits of Containers
Containers have become popular because they provide extra
benefits, such as:

1) Agile application creation and deployment:


• Increased ease and efficiency of container image creation compared to
VM image use.

2) Continuous development, integration, and deployment:


• Provides for reliable and frequent container image build and deployment
with quick and efficient rollbacks (due to image immutability).

Asad Hanif 11

11

DevOps

Benefits of Containers
Containers have become popular because they provide extra
benefits, such as:

3) Dev and Ops separation of concerns:


• Create application container images at build/release time rather than
deployment time, thereby decoupling applications from infrastructure.

4) Observability:
• Not only surfaces OS-level information and metrics, but also application
health and other signals.

Asad Hanif 12

12

6
3/31/2023

DevOps

Benefits of Containers
Containers have become popular because they provide extra
benefits, such as:

5) Environmental consistency across development,


testing, and production:
• Runs the same on a laptop as it does in the cloud.

6) Cloud and OS distribution portability:


• Runs on Ubuntu, RHEL, CoreOS, on-premises, on major public clouds,
and anywhere else.

Asad Hanif 13

13

DevOps

Benefits of Containers
Containers have become popular because they provide extra
benefits, such as:

7) Application-centric management:
• Raises the level of abstraction from running an OS on virtual hardware to
running an application on an OS using logical resources.

8) Resource isolation:
• Predictable application performance.

Asad Hanif 14

14

7
3/31/2023

DevOps

Benefits of Containers
Containers have become popular because they provide extra
benefits, such as:

9) Resource utilization:
• High efficiency and Density.

10) Loosely coupled, distributed, elastic, liberated micro-


services:
• Applications are broken into smaller, independent pieces and can be
deployed and managed dynamically – not a monolithic stack running on
one big single-purpose machine.
Asad Hanif 15

15

DevOps

Containerization with Docker

Asad Hanif 16

16

8
3/31/2023

DevOps

Docker Installation

Asad Hanif 17

17

DevOps

Docker Installation – Linux


 To get started with Docker Engine on Ubuntu, use official
documentation.

 https://docs.docker.com/engine/install/ubuntu/

Asad Hanif 18

18

9
3/31/2023

DevOps

Docker Installation – Linux


Uninstall Old Versions
 Older versions of Docker went by the names of docker, docker.io,
or docker-engine.
 Uninstall any such older versions before attempting to install a new
version:

$ sudo apt-get remove docker docker-engine docker.io


containerd runc
 It’s OK if apt-get reports that none of these packages are installed.
Asad Hanif 19

19

DevOps

Docker – Install using Repository


Set up the repository
1) Update the apt package index and install packages to allow apt to
use a repository over HTTPS:
$ sudo apt-get update
$ sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release

Asad Hanif 20

20

10
3/31/2023

DevOps

Docker – Install using Repository

Set up the repository


2) Add Docker’s official GPG key:
$ sudo mkdir -m 0755 -p /etc/apt/keyrings

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg


| sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Asad Hanif 21

21

DevOps

Docker – Install using Repository

Set up the repository


3) Use the following command to set up the repository:
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-
by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee
/etc/apt/sources.list.d/docker.list > /dev/null

Asad Hanif 22

22

11
3/31/2023

DevOps

Docker – Install using Repository

Install Docker Engine


1) Update the apt package index:

$ sudo apt-get update

Asad Hanif 23

23

DevOps

Docker – Install using Repository

Install Docker Engine


2) Install Docker Engine, containerd, and Docker Compose:

$ sudo apt-get install docker-ce docker-ce-cli


containerd.io docker-buildx-plugin docker-
compose-plugin

Asad Hanif 24

24

12
3/31/2023

DevOps

Docker – Install using Repository

Install Docker Engine


3) Verify that the Docker Engine installation is successful by
running the hello-world image:

$ sudo docker run hello-world

Asad Hanif 25

25

DevOps

What is Docker?

Asad Hanif 26

26

13
3/31/2023

DevOps

The Docker platform


 Docker is an open platform for developing, shipping, and
running applications.

 Docker enables us to separate our applications from our


infrastructure so we can deliver software quickly.

 With Docker, we can manage our infrastructure in the same


ways you manage our applications.

Asad Hanif 27

27

DevOps

The Docker platform


 Docker provides the ability to package and run an application in a
loosely isolated environment called a container.

 The isolation and security allows us to run many containers


simultaneously on a given host.

 Containers are lightweight and contain everything needed to run the


application, so we do not need to rely on what is currently installed on
the host.

 We can easily share containers while we work, and be sure that


everyone share with gets the same container that works in the same
way.
Asad Hanif 28

28

14
3/31/2023

DevOps

The Docker platform


 Docker provides tooling and a platform to manage the lifecycle of
your containers:
• Develop the application and its supporting components using containers.

• The container becomes the unit for distributing and testing the application.

• When app is ready, deploy application into production environment, as a


container or an orchestrated service.

 This works the same whether the production environment is a


local data center, a cloud provider, or a hybrid of the two.

Asad Hanif 29

29

DevOps

Docker Architecture

Asad Hanif 30

30

15
3/31/2023

DevOps

Docker Architecture

Asad Hanif 31

31

DevOps

Docker Hands-on

Asad Hanif 32

32

16
3/31/2023

DevOps

List Containers
 Show running stopped containers

$ docker ps

 Show both running and stopped containers

$ docker ps -a

Asad Hanif 33

33

DevOps

List Images
 Show all top-level images, their repository and tags, and
their size

$ docker images

 List images by name and tag

$ docker images java

 Show untagged images (dangling)


https://docs.docker.com/engin
e/reference/commandline/ima
$ docker images --filter "dangling=true" ges/
Asad Hanif 34

34

17
3/31/2023

DevOps

Pull Images
 Download an image from a registry (Docker Hub)

$ docker pull <image-name>

https://docs.docker.com/engine/refer
ence/commandline/pull/

Asad Hanif 35

35

DevOps

Run Containers
 Create and run a new container from an image

$ docker run hello-world

 Assign name and allocate pseudo-TTY (--name, -it)

$ docker run --name test -it nginx

 Example

$ docker run --name mynginx1 -p 8080:80 -d nginx


https://docs.docker.com/engine/reference/commandline/run/
Asad Hanif 36

36

18
3/31/2023

DevOps

Rename Container
 To rename a container, we use following command.

$ docker rename CONTAINER NEW_NAME

https://docs.docker.com/engine/re
ference/commandline/rename/

Asad Hanif 37

37

DevOps

Remove Containers
 Remove one or more containers

$ docker rm <container-name or ID>

https://docs.docker.com/engine/refer
ence/commandline/rm/

Asad Hanif 38

38

19
3/31/2023

DevOps

Remove Images
 Remove one or more images

$ docker rmi <image-name or ID>

https://docs.docker.com/engine/re
ference/commandline/rmi/

Asad Hanif 39

39

DevOps

Demo
Run Nginx in a Docker
Container
Asad Hanif 40

40

20
3/31/2023

DevOps

Run Nginx in a Docker Container


 Downloading Nginx From Docker Hub

$ docker pull nginx

 Start your Nginx Docker container

$ docker run --name my-nginx -p 8080:80 nginx

 Open browser and hit localhost:8080

Asad Hanif 41

41

DevOps

Run Nginx in a Docker Container


 Running in Detached Mode

$ docker run --name my-nginx -p 8080:80 -d nginx

 Stop the container

$ docker stop my-nginx

 Remove the container

$ docker rm my-nginx

Asad Hanif 42

42

21
3/31/2023

DevOps

Run Nginx in a Docker Container


 Building a Web Page to Serve on Nginx

 Create a new directory

$ mkdir -p ~/docker-nginx/html

$ cd ~/docker-nginx/html

 Create an HTML file to serve on your server and paste a


page

$ nano index.html
Asad Hanif 43

43

DevOps

Run Nginx in a Docker Container


 Linking the Container to the Local Filesystem

 Docker allows to link directories from virtual machine’s local


file system to container.

 The Nginx container is set up by default to look for an index


page at /usr/share/nginx/html.

Asad Hanif 44

44

22
3/31/2023

DevOps

Run Nginx in a Docker Container


 To do this, use the -v flag to map the ~/docker-nginx/html
folder from server to a relative path in the container
/usr/share/nginx/html.

$ docker run --name docker-nginx -p 8080:80 -d -v


~/docker-nginx/html:/usr/share/nginx/html nginx

Asad Hanif 45

45

DevOps

Run Nginx in a Docker Container


$ docker run --name docker-nginx -p 8080:80 -d -v
~/docker-nginx/html:/usr/share/nginx/html nginx

 Explanation of the command:


• -v flag specifies that you’re linking a volume.
• To the left of the : is the location of directory on server, ~/docker-
nginx/html.
• To the right of the : is the location that are symbolically linking to
container /usr/share/nginx/html.
Asad Hanif 46

46

23
3/31/2023

DevOps

List of Containers
 We can create various independent services using different
programming languages and platforms.

 For example, service A can be developed using Python,


service B using Java, service C using JavaScript, etc.

 It enables the rapid, frequent, and reliable delivery of large


and complex applications.

Asad Hanif 47

47

DevOps

What are Microservices?

Asad Hanif 48

48

24

You might also like