0% found this document useful (0 votes)
5 views

Module 41

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module 41

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Containerization Using

Docker - II

© Copyright 2019 IntelliPaat, All rights reserved


Agenda

Introduction to Understanding Introduction to


01 Docker Storage
02 Microservices 03 Docker Compose

What are YAML Introduction to Docker


04 files?
05 Docker Swarm 06 Networks

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Docker
Storage

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Docker Storage
By default, all data of a container is stored on a writable container layer. This layer has the following properties:

Data only exists while the container is active. If the container no


longer exists, the data is also deleted along with the container.

The writable container layer is tightly coupled with the host


machine; hence, it is not portable.

Data on the writable layer in the container is written using a


storage driver.

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Docker Storage
To persist data inside the container, even after it is deleted, we have two options:

Docker Bind
Volumes Mounts

© Copyright 2019 IntelliPaat, All rights reserved


Types of Docker Storage
A Docker Volume is a mountable entity which can be used to store
data in the docker filesystem.

Syntax

docker volume create my-vol


Docker Volumes

Bind Mounts

© Copyright 2019 IntelliPaat, All rights reserved


Types of Docker Storage
A Docker Volume is a mountable entity which can be used to store
data in the docker filesystem.

Syntax

docker run -it --mount


Docker Volumes
source=<source=folder>,destination=<destination-folder> -d
<container-name>

Bind Mounts

© Copyright 2019 IntelliPaat, All rights reserved


Types of Docker Storage
Bind Mounts mount a directory of the host machine to the docker
container.

Syntax

docker run -it –v <source-directory>:<destination-directory>


-d <container-name>
Docker Volumes

Bind Mounts

© Copyright 2019 IntelliPaat, All rights reserved


Linking Docker
Containers

© Copyright 2019 IntelliPaat, All rights reserved


Linking Docker Containers

Linking is a legacy feature of Docker, which is used to connect multiple containers.

With linking, containers can communicate among each other.

Name of containers is an important aspect while linking containers.

Once you link containers, they can reach out to others using their names.

© Copyright 2019 IntelliPaat, All rights reserved


Linking Docker Containers

Syntax

docker run –it - -link <name-of-container> -d <image-name>

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Linking Docker
Containers

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Linking Docker Containers

1. Create two containers of Ubuntu with names as follows: container1 and container2

2. Link container2 to container1

3. Try pinging from container2 to container1 by just using the command “ping container1”

© Copyright 2019 IntelliPaat, All rights reserved


Understanding
Microservices

© Copyright 2019 IntelliPaat, All rights reserved


What is a Monolithic Application?

A Monolithic application is a single-tiered software application in which different components


are combined into a single program which resides in a single platform.

Notifications Ma i l Pa yments

Loca tion Pa s senger


Cus tomer
Servi ces Ma na gement
Servi ce

© Copyright 2019 IntelliPaat, All rights reserved


Disadvantages of a Monolithic Application

Application is large and complex to


understand.

Entire application has to be re-deployed on


an application update.

Bug, in any module, can bring down the


entire application.

It has a barrier to adopting new


technologies.

© Copyright 2019 IntelliPaat, All rights reserved


What are Microservices?

Microservices are a software development architectural style that structures an application as a


collection of loosely coupled services.

Ma i l

Notifications Pa yments

Loca tion
Servi ces

Cus tomer Pa s senger


Servi ce Ma na gement

© Copyright 2019 IntelliPaat, All rights reserved


What are Microservices?

Microservices are a software development architectural style that structures an application as a


collection of loosely coupled services.

Ma i l

Notifications Pa yments

Loca tion
Servi ces

Cus tomer Pa s senger


Servi ce Ma na gement

© Copyright 2019 IntelliPaat, All rights reserved


Advantages of Microservices

Application is distributed, hence easy to


understand.

The code of only the Microservice which is


supposed to be updated is changed.

Bug, in one service, does not affect other


services.

There is no barrier to any specific


technology.

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Docker
Compose

© Copyright 2019 IntelliPaat, All rights reserved


What is Docker Compose?

Compose is a tool for defining and running multi-container Docker applications. With Compose,
you use a YAML file to configure your application's services. Then, with a single command, you
create and start all the services from your configuration. Run docker-compose up
and compose starts and runs your entire app.

© Copyright 2019 IntelliPaat, All rights reserved


Installing Docker
Compose

© Copyright 2019 IntelliPaat, All rights reserved


Installing Docker Compose
1. First, download the Docker Compose file using the following command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-


$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

© Copyright 2019 IntelliPaat, All rights reserved


Installing Docker Compose
2. Now, give the required permission to the Docker Compose file to make it executable:

sudo chmod +x /usr/local/bin/docker-compose

© Copyright 2019 IntelliPaat, All rights reserved


Installing Docker Compose
3. Finally, verify your installation using the following command:

docker-compose --version

© Copyright 2019 IntelliPaat, All rights reserved


What is Docker Compose?

For deploying containers using Docker Compose, we use YAML files.

© Copyright 2019 IntelliPaat, All rights reserved


What are YAML files?

© Copyright 2019 IntelliPaat, All rights reserved


What are YAML files?

YAML is a superset of a JSON file. There are only two types of structures in YAML which
you need to know to get started:

Maps

Lists

© Copyright 2019 IntelliPaat, All rights reserved


What are YAML files?

When we map a key to a value in YAML files,


they are termed as Maps.

Maps

<key> : <value>

Lists For example:

Name: Intellipaat
Course: Devops

© Copyright 2019 IntelliPaat, All rights reserved


What are YAML files?

YAML Lists are a sequence of objects.

Maps Args
- arg 1
- arg 2
- arg 3

Lists For example:

© Copyright 2019 IntelliPaat, All rights reserved


Writing a Docker
Compose File

© Copyright 2019 IntelliPaat, All rights reserved


Writing a Docker Compose File

version: '3'
services:
sample1:
image: httpd
ports:
- “80:80"
sample2:
image: nginx

Sample Docker Compose File

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Running a Sample
Docker Compose File

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Sample Docker Compose File
1. Create a folder called “docker”
2. Write the sample YAML file in “docker-compose.yml” file
3. To build this docker-compose file, the syntax is as follows:

docker-compose up -d

4. Ensure that all your containers are running

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Deploying WordPress

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Deploying WordPress
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
docker-compose.yaml
© Copyright 2019 IntelliPaat, All rights reserved
Hands-on: Deploying WordPress
1. Create a folder called “docker-wordpress”
2. Write the sample YAML file in “docker-compose.yml” file
3. To build this docker-compose file, the syntax is as follows:

docker-compose up -d

4. Ensure that all your containers are running

© Copyright 2019 IntelliPaat, All rights reserved


What is Container
Orchestration?

© Copyright 2019 IntelliPaat, All rights reserved


What is Container Orchestration?

Applications are typically made up of individually containerized components (often called microservices)
that must be organized at the networking level in order for the application to run as intended. The process
of organizing multiple containers in this manner is known as container orchestration.

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Docker
Swarm

© Copyright 2019 IntelliPaat, All rights reserved


What is Docker Swarm?

Docker Swarm is a clustering and scheduling tool for Docker containers.


With Swarm, IT administrators and developers can establish and manage a
cluster of Docker nodes as a single virtual system.

© Copyright 2019 IntelliPaat, All rights reserved


What is Docker Swarm?
Worker 1 Worker 2 Worker 3

Leader

© Copyright 2019 IntelliPaat, All rights reserved


Creating a Docker Swarm
Cluster

© Copyright 2019 IntelliPaat, All rights reserved


Creating a Docker Swarm Cluster

docker swarm init --advertise-addr=<ip-address-of-leader>

This command should be passed on to the worker


node to join the docker swarm cluster.

© Copyright 2019 IntelliPaat, All rights reserved


Introduction to Services

© Copyright 2019 IntelliPaat, All rights reserved


What is a Service?

Containers on the cluster are deployed using services on Docker Swarm. A service is a long-
running Docker container that can be deployed to any node worker.

Service

© Copyright 2019 IntelliPaat, All rights reserved


Creating a Service

docker service create --name <name-of-service> --replicas <number-of-replicas> <image-name>

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Creating a Service
in Docker Swarm
© Copyright 2019 IntelliPaat, All rights reserved
Hands-on: Creating a Service in Docker Swarm

1. Create a Service for nginx webserver

2. There should be 3 replicas of this service running on the swarm cluster

3. Try accessing the service from Master IP and Slave IP

Service

© Copyright 2019 IntelliPaat, All rights reserved


Docker Networks

© Copyright 2019 IntelliPaat, All rights reserved


Why Docker Networks?

Let’s take an example. Say, there are two containers which we deploy in the docker ecosystem.

Website Container Database Container

© Copyright 2019 IntelliPaat, All rights reserved


Why Docker Networks?

By default, these containers cannot communicate with each other.

Website Container Database Container

© Copyright 2019 IntelliPaat, All rights reserved


Why Docker Networks?

Therefore, in-order to have interactions between Docker Containers, we need Docker Networks.

Website Container Database Container

Docker Network
© Copyright 2019 IntelliPaat, All rights reserved
What are Docker Networks?

One of the reasons Docker containers and services are so powerful is that you can
connect them together or connect them to non-Docker workloads. And, this can be
accomplished using Docker Networks.

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

Docker Networks are of the following types:

bridge host overlay

macvlan none

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

bridge Bridge Networks

host The default network driver. If you don’t specify a driver, this is
the type of network you are creating. Bridge networks are
usually used when your applications run in standalone
overlay containers that need to communicate.

macvlan

none

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

bridge Host Networks

host For standalone containers, remove network isolation between


the container and the Docker host and use the host’s
networking directly. Host is only available for swarm services
overlay on Docker 17.06 and higher.

macvlan

none

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

bridge Overlay Networks

host Overlay networks connect multiple Docker daemons


together and enable swarm services to communicate
with each other. You can also use overlay networks to
facilitate communication between a swarm service and a
overlay standalone container or between two standalone
containers on different Docker daemons.

macvlan

none

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

bridge Macvlan Networks

host Macvlan networks allow you to assign a MAC address to a


container, making it appear as a physical device on your
network. The Docker daemon routes traffic to containers by
their MAC addresses.
overlay

macvlan

none

© Copyright 2019 IntelliPaat, All rights reserved


Docker Network Types

bridge None

host
For this container, disable all networking. This is
usually used in conjunction with a custom network
driver. And, none is not available for swarm services.
overlay

macvlan

none

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Deploying a Multi-tier
App in Docker Swarm

© Copyright 2019 IntelliPaat, All rights reserved


Hands-on: Multi-tier App in Docker Swarm

1. Create an overlay network named “my-overlay”

2. Deploy a website container in the overlay network


• Image: hshar/webapp

3. Deploy a database container in the overlay network


• image: hshar/mysql:5.6
• username: root password: intelli

4. Make changes in the website code to point to MySQL service

5. Test the configuration by entering values in the website

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

1. ________ is a document used to deploy multiple containers at once.

A. Docker File

B. Docker Compose File

C. Docker Network

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

1. ________ is a document used to deploy multiple containers at once.

A. Docker File

B. Docker Compose File

C. Docker Networks

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

2. Which of these is used to mount a directory from the hard disk.

A. Docker Volumes

B. Bind Mounts

C. Docker Network

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

2. Which of these is used to mount a directory from the hard disk.

A. Docker Volumes

B. Bind Mounts

C. Docker Network

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

3. For Building a Microservices Architecture, which of the following should you choose?

A. Docker Compose

B. Docker Volumes

C. Docker Swarm

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

3. For Building a Microservices Architecture, which of the following should you choose?

A. Docker Compose

B. Docker Volumes

C. Docker Swarm

D. None of these

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

4. The Docker Volume of type local is available throughout the swarm cluster.

A. True

B. False

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

4. The Docker Volume of type local is available throughout the swarm cluster.

A. True

B. False

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

5. A Docker Swarm service can have more than one containers.

A. True

B. False

© Copyright 2019 IntelliPaat, All rights reserved


Quiz

5. A Docker Swarm service can have more than one containers.

A. True

B. False

© Copyright 2019 IntelliPaat, All rights reserved


India: +91-7847955955

US: 1-800-216-8930 (TOLL FREE)

[email protected]

24/7 Chat with Our Course Advisor

© Copyright 2019 IntelliPaat, All rights reserved

You might also like