Containerization [ Docker ]
AGENDA
Virtualization
Containerization
Introduction to Docker
Understanding Docker Lifecycle
Components of the Docker Ecosystem
Common Docker Operations
Creating a Docker Hub Account
WHAT IS
VIRTUALIZATION?
© Copyright. All Rights Reserved.
WHAT IS VIRTUALIZATION?
Virtualization is the process of running multiple virtual systems or resources on
top of a single physical machine. These resources could be a storage device,
network or even an operatingsystem!
App App App
Guest OS Guest OS Guest OS
Hypervisor
Host Operating System
© Copyright. All Rights Reserved.
WHAT IS
CONTAINERIZATION?
© Copyright. All Rights Reserved.
WHAT IS CONTAINERIZATION?
Application containerization is an OS-level virtualization method used to deploy and run
distributed applications without launching an entire virtual machine (VM) for each app.
App1 App2 App3
Bins/Libs Bins/Libs Bins/Libs
Container Engine
Operating System
Hardware
© Copyright. All Rights Reserved.
PROBLEMS BEFORE CONTAINERIZATION
Developers when run the code on their system, it would run perfectly. But the same code
would not run on the operations team’s system.
Works fine on
my system!
Doesn’t work
on my system.
Faulty code!
Developer Operations/
Testing
© Copyright. All Rights Reserved.
PROBLEMS BEFORE CONTAINERIZATION
The problem was with the environment the code was being run in. Well, a simple answer
could be, why not give the same VM to the operations/testing team to run the code.
Well, try the
VM that I’m
workingin. That could break
another code on
testing/production
server!
Developer Operations/
Testing
© Copyright. All Rights Reserved.
PROBLEMS BEFORE CONTAINERIZATION
VMs took too many resources torun.
VMs were too big in size to be portable.
VMs were not developer friendly.
© Copyright. All Rights Reserved.
HOW DID CONTAINERS SOLVE THE PROBLEMS?
With containers, all the environment issues were solved. The developer could easily wrap
their code in a lightweight container and pass it on to the operationsteam.
Here is the
container. I
have wrapped
Wow, it’s hardly 30
my code in.
MB. Awesome,your
code works just
fine!
Developer Operations/
Testing
© Copyright. All Rights Reserved.
ADVANTAGES OF CONTAINERS
Containers are not resource hungry.
They are lightweight and hence
portable.
They are developer friendly and can be
configured through the code.
© Copyright. All Rights Reserved.
CONTAINERIZATION
TOOLS
© Copyright. All Rights Reserved.
CONTAINERIZATION TOOLS
WHAT IS
DOCKER?
© Copyright. All Rights Reserved.
WHAT IS DOCKER?
Docker is a computer program that performs operating-system-level virtualization, also
known as "containerization". It was first released in 2013 and is developed by Docker, Inc.
Docker is used to run software packages called "containers".
© Copyright. All Rights Reserved.
DOCKER CONTAINER LIFE CYCLE
Pull
run
Docker Hub Push
stop
Docker Engine Docker Images
delete
Container Stages
© Copyright. All Rights Reserved.
COMPONENTS OF
DOCKER ECOSYSTEM
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub Docker Engine Docker Images
Containers Docker Volumes Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
Docker Hub is a central public docker registry.
Itcan store custom docker images.
Docker Engine
The service is free, but your images would be public.
Docker Images It requires username/password.
Containers
Docker Volumes
Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
Docker Engine is the heart of the docker ecosystem.
It is responsible for managing your container runtimes.
Docker Engine
It works on top of operating system level.
Docker Images It utilizes the kernel of the underlying OS.
Containers
Docker Volumes
Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
Docker Image is like the template of acontainer.
It is created inlayers.
Docker Engine
Any new changes in the image results in creating a
new layer.
Docker Images
One can launch multiple containers from a single
docker image.
Containers
Docker Volumes
Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
A Docker Container is a lightweight software
environment.
Docker Engine It works on top of the underlying OS kernel.
It is small in size and therefore is highly portable.
Docker Images
It is created using the docker image.
Containers
Docker Volumes
Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
Docker Containers cannot persist data.
To persist data in containers, we can use Docker
Docker Engine Volume.
A Docker Volume can connect to multiple containers
simultaneously.
Docker Images
If not created explicitly, a volume is automatically
created when we create a container.
Containers
Docker Volumes
Docker File
© Copyright. All Rights Reserved.
COMPONENTS OF DOCKER ECOSYSTEM
Docker Hub
Dockerfile is a YAML file, which is used to create
custom containers
Docker Engine
It can include commands that have to be run on the
command line
Docker Images This Dockerfile can be used to build custom
container images
Containers
Docker Volumes
Dockerfile
© Copyright. All Rights Reserved.
INSTALLING
DOCKER
© Copyright. All Rights Reserved.
COMMON DOCKER
COMMANDS
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker --version
This command helps you know the installed version of the docker software on your system.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker pull <image-name>
This command helps you pull images from the central docker repository.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker images
This command helps you in listing all the docker images downloaded on your system.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker run <image-name>
This command helps in running containers from their image name.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker ps
This command helps in listing all the containers which are running in the system.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker ps -a
If there are any stopped containers, they can be seen by adding the -a flag in this command.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker exec <container-id>
For logging into/accessing the container, one can use the exec command.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker stop <container-id>
For stopping a running container, we use the stop command.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker kill <container-id>
This command kills the container by stopping its execution immediately.
The difference between docker kill and docker stop: ‘docker stop’gives the container
time to shutdown gracefully; whereas, in situations when it is taking too much time for
getting the container to stop, one can opt to kill it.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker rm <container-id>
To remove a stopped container from the system, we use the rmcommand.
© Copyright. All Rights Reserved.
COMMON DOCKER COMMANDS
docker rmi <image-id>
To remove an image from the system, we use the rmicommand.
© Copyright. All Rights Reserved.
CREATING A DOCKER
HUB ACCOUNT
© Copyright. All Rights Reserved.
CREATING A DOCKER HUB ACCOUNT
1. Navigate to https://hub.docker.com
2. Sign up on the website
3. Agree to the terms and conditions
4. Click on Sign up
5. Check your email, and verify your email by clicking
on the link
6. Finally, login using the credentials you provided
on the sign uppage
© Copyright. All Rights Reserved.
COMMITTING CHANGES
TO A CONTAINER
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
Let’s try to accomplish the following example with a container and see how we can
commit this container into an image.
Commit these
changes to the
container
Ubuntu Install Apache server Ubuntu Container with
Container on thiscontainer Apache installed
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
1. Pull the Docker Container using the command:
docker pull ubuntu
In our case, the image name is “ubuntu”.
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
2. Run the container using the command:
docker run –it –d ubuntu
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
3. Access the container using thecommand:
docker exec –it <container-id> bash
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
4. Install Apache2 on this container using the followingcommands:
apt-get update
apt-get install apache2
© Copyright. All Rights Reserved.
COMMITTING CHANGES TO A DOCKER CONTAINER
5. Exit the container and save it using this command. The saved container will be converted into an image with the name
specified.
docker commit <container-id> <username>/<container-name>
The username has to match with the username you created on DockerHub.
The container-name canbe anything.
© Copyright. All Rights Reserved.
PUSHING THE CONTAINER
ON DOCKERHUB
© Copyright. All Rights Reserved.
PUSHING THE CONTAINER ON DOCKERHUB
1. The first step is to login. It can be done using the followingcommand:
docker login
© Copyright. All Rights Reserved.
PUSHING THE CONTAINER ON DOCKERHUB
2. For pushing your container on DockerHub, use the following command:
docker push <username>/<container-id>
devopsdemo/apache
[docker.io/devopsdemo/apache]
© Copyright. All Rights Reserved.
PUSHING THE CONTAINER ON DOCKERHUB
3. You can verify the push on DockerHub.
devopsdemo
Now anyone, who wants todownload
this container, can simply pass the
following command:
docker pulldemo/apache
devopsdemo/apache
© Copyright. All Rights Reserved.
INTRODUCTION
TO DOCKERFILE
© Copyright. All Rights Reserved.
INTRODUCTION TO 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 the docker build, users can create an automated build that executes several
command-line instructions in succession.
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM
The FROM keyword is used to define the base image, on which we
will be building.
ADD
RUN Example
FROM ubuntu
CMD
Dockerfile
ENTRYPOINT
ENV
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM
The ADD keyword is used to add files to the container being built. The
syntax used is:
ADD <source> <destination in container>
ADD
RUN Example
FROM ubuntu
ADD . /var/www/html
CMD
Dockerfile
ENTRYPOINT
ENV
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM
The RUN keyword is used to add layers to the base image,
by installing components. Each RUN statement adds a
new layer to the docker image.
ADD
RUN Example
FROM ubuntu
RUN apt-get update
CMD RUN apt-get -y installapache2
ADD . /var/www/html
ENTRYPOINT
ENV
Dockerfile
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM
The CMD keyword is used to run commands on the start of the
container. These commands run only when there is no argument
specified while running the container.
ADD
RUN Example
FROM ubuntu
RUN apt-get update
CMD RUN apt-get -y installapache2
ADD . /var/www/html
CMD apachectl –D FOREGROUND
ENTRYPOINT
ENV
Dockerfile
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM The ENTRYPOINT keyword is used strictly to run commands the
moment the container initializes. The difference between CMD and
ENTRYPOINT: ENTRYPOINT will run irrespective of the fact whether
the argument is specified ornot.
ADD
RUN Example
FROM ubuntu
RUN apt-get update
CMD RUN apt-get -y installapache2
ADD . /var/www/html
ENTRYPOINT apachectl –DFOREGROUND
ENTRYPOINT
ENV
Dockerfile
© Copyright. All Rights Reserved.
VARIOUS COMMANDS IN DOCKERFILE
FROM
The ENV keyword is used to define environment variables
in the container runtime.
ADD
RUN Example
FROM ubuntu
RUN apt-get update
CMD RUN apt-get -y installapache2
ADD . /var/www/html
ENTRYPOINT apachectl –DFOREGROUND
ENTRYPOINT ENV name Devops Tutorial
ENV
Dockerfile
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE
DOCKERFILE
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
Let’s see how we can run this sample Dockerfile now.
Example
FROM ubuntu
RUN apt-get update
RUN apt-get -y installapache2
ADD . /var/www/html
ENTRYPOINT apachectl -DFOREGROUND
ENV name Devops Tutorial
Dockerfile
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
1. First, create a folder docker in the home directory.
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
2. Enter into this directory and create a file called ‘Dockerfile’, with the same contents as the sample Dockerfile.
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
3. Create one more file called ‘index.html’ with the following contents.
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
4. Now, pass the following command:
docker build <directory-of-dockerfile> -t <name of container>
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
5. Finally, run this built image, using the following command:
docker run –it –p 81:80 –d demo/custom
demo/apache
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
6. Now, navigate to the server IP address on port 81.
© Copyright. All Rights Reserved.
RUNNING THE SAMPLE DOCKERFILE
7. Finally, login into the container and check the variable $name. It will have the same value as given in the Dockerfile.
© Copyright. All Rights Reserved.