Docker
Docker
Hypervisor
CPU 10%
Imagine Software A running on Server A which has Ubuntu running on it. This software can
only run in the Ubuntu environment.
Problems before Virtualization
Some time later, we needed Software B which can only run on Windows. Therefore, we
had to buy and run a Server B which had windows running on it. The software took only
10% of the CPU resources.
Problems before Virtualization
CPU 20%
Server A running
Software A Software B Windows and
Ubuntu
Windows and Ubuntu OS now are running on the same server in parallel using the Virtualization
technology. This accounts for better CPU utilization and cost savings!
Advantages of Virtualization
Container Engine
Operating System
Hardware
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
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.
Developer Operations/
Testing
Problems before Containerization
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 operations team.
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
Advantages of Containers
Pull
run
Docker Hub Push
stop
delete
Container Stages
Components of
Docker Ecosystem
Components of Docker Ecosystem
Containers
Docker Volumes
Docker File
Components of Docker Ecosystem
Docker Hub
Docker Engine is the heart of the docker ecosystem.
Containers
Docker Volumes
Docker File
Components of Docker Ecosystem
Docker Hub
Docker Image is like the template of a container.
It is created in layers.
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
Components of Docker Ecosystem
Docker Hub
A Docker Container is a lightweight software
environment.
Containers
Docker Volumes
Docker File
Components of Docker Ecosystem
Docker Hub
Docker Containers cannot persist data.
Docker Volumes
Docker File
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
Installing Docker
Common Docker
Commands
Common Docker Commands
docker --version
This command helps you know the installed version of the docker software on your system.
Common Docker Commands
This command helps you pull images from the central docker repository.
Common Docker Commands
docker images
This command helps you in listing all the docker images downloaded on your system.
Common Docker Commands
docker ps
This command helps in listing all the containers which are running in the system.
Common Docker Commands
docker ps -a
If there are any stopped containers, they can be seen by adding the -a flag in this command.
Common Docker Commands
For logging into/accessing the container, one can use the exec command.
Common Docker Commands
docker rm <container-id>
1. Navigate to https://hub.docker.com
4. Click on Sign up
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
apt-get update
apt-get install apache2
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.
The username has to match with the username you created on DockerHub.
The container-name can be anything.
Pushing the Container on
DockerHub
Pushing the Container on DockerHub
1. The first step is to login. It can be done using the following command:
docker login
Pushing the Container on DockerHub
2. For pushing your container on DockerHub, use the following command:
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.
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
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
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 install apache2
ADD . /var/www/html
ENTRYPOINT
ENV
Dockerfile
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 install apache2
ADD . /var/www/html
CMD apachectl –D FOREGROUND
ENTRYPOINT
ENV
Dockerfile
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 or not.
ADD
RUN Example
FROM ubuntu
RUN apt-get update
CMD RUN apt-get -y install apache2
ADD . /var/www/html
ENTRYPOINT apachectl –D FOREGROUND
ENTRYPOINT
ENV
Dockerfile
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 install apache2
ADD . /var/www/html
ENTRYPOINT apachectl –D FOREGROUND
ENTRYPOINT ENV name Devops Intellipaat
ENV
Dockerfile
Running the Sample
Dockerfile
Running the Sample Dockerfile
Example
FROM ubuntu
RUN apt-get update
RUN apt-get -y install apache2
ADD . /var/www/html
ENTRYPOINT apachectl -D FOREGROUND
ENV name Devops Intellipaat
Dockerfile
Running the Sample Dockerfile
2. Enter into this directory and create a file called ‘Dockerfile’, with the same contents as the sample Dockerfile.
Running the Sample Dockerfile
3. Create one more file called ‘index.html’ with the following contents.
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.