5.1) Docker-DetailedNotes
5.1) Docker-DetailedNotes
1) Docker-DetailedNotes
Monolith
● The complete application is packaged as a unit and deployed on to the server
● The whole insurance application on the server will have one more server to store the
data.
● There is an income tax submission season, where there will be a lot of sales
○ Agents will create a lot of quotes
○ Consumer logins might need to be created
● The insurance web portal is running on 3 servers that can handle approx 15000
parallel requests, in this tax season, users will be around 25k. So we need to have at
least 5 servers for the web portal.
● Docker containers can be used to run monolith applications
MicroServices
● Application is broken down into multiple individually executable services. With each
service offering some functionality
● Microservices can be scaled individually.
● Docker containers are the best fit for microservices.
● Applications will be of two types.
○ Stateless applications:
■ Do Not store state locally, they store the state on external systems.
■ Docker container is the best fit for stateless applications.
○ Stateful application
■ Stores all or some state locally.
■ We can run them on docker, extra processing is required.
NOTE:- after installing the above script docker user is created and we need to add that used to
Ubuntu group
● Ubuntu (https://docs.docker.com/engine/install/ubuntu/)
● Centos (https://docs.docker.com/engine/install/centos/)
● When we install docker, two major docker components get installed
Lets execute simple commands in docker and we will check what is happening:-
● When we run docker container hello-world the request is going to demon and it will
ask do you have any image called “hello-world” if it is a fresh image and demon
doesn’t have any images then it will forward the request to container registry/docker
registry, (image is packaging format of docker container)
● If next time it is trying to ask the same image called ‘hello-world’ to the docker
daemon then it will give the image.
● For docker containers to run we need docker images. Docker Image is a packaging
format which uses all the ingredients to run your application.
When we run the command to create another container with same image the following happens
Note:- Default Docket registry i.e Docket Hub
https://hub.docker.com/
Topic:-Docker commands:-
● Use help commands
● Execute docker help
● Docker commands are generally organised as “docker <command> --help”
docker management commands:-
docker commands:-
Examples
https://docs.docker.com/engine/reference/commandline/cli/
1) docker image build –t <containername> . {This will build the container from the image here .
means location of Docker file}
2) docker pull <image-name> {It is used to pull image from docker hub}
3) docker image ls {It will show list of images present in local registry}
4) docker container run –P –d <image-name>{To run the docker image hear ‘P’ caps it is
responsible for the port forex, if we give EXPOSE 8080 then the ‘P’ will map to any available port
on the host OS/VM . If we want to give custom port we need to use ‘-p’ small p “-p 32790:8080”
observe 15 example}
6) docker image inspect <image name:label> {It will give complete info about specific image}
7) docker image run –it <image-name: version> {It will download image and it will enter into
that container}
8) docker stats {It will display running container process memory details }
15) docker container run –p <host-port>:<container -port> <image>{-p is used to define port
number }
16) docker container exec <container name> <command > {It will execute commands in side
the given container without going inside to that container }
18) docker image inspect -f {{.config.Labels}} spc:slim {to display the LABEL which we
mentioned. -f is format type. Also we can use any specific things to display in output}
19)
Detached:-
Interactive:-
● In this mode we can interact with docker using terminal (/bin/bash, /bin/sh)
● add -it to container run to set this mode
● Example docker container run -it -P jenkins /bin/bash
TOPIC: Docker Image:-
Containerization: Process of making your application run on docker containers
Docker image: Docker image is a packing format to run an application on docker engine as a
container
It includes everything to run an application. Like code or binaries, runtime, dependencies, other
file system objects
Basic Docker
1. Docker daemon
2. Docker client
Docker Registry:-
Workflow:
● When we build docker image an image id, image name & image tag are associated with
a image
● Now lets use an instruction LABEL to add metadata Refer Here for the changeset
{https://github.com/asquarezone/DockerZone/commit/b3b3d0f36d1f7f12f97eedfb4dd50
7be0eb7893c}
Now lets try find history of image
6) The above steps are manual steps same way we will try to create Dockerfile
1. Create Deckerville
2. Find the base image (we will take Ubuntu image)
3. And install java and the spring petclinic application file and start the
application
{
apt-get update
apt-get install openjdk-8-jdk wget -y
wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPS
HOT.jar
# Now to start application
java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
}
● no
● If you had to create a container it was very difficult and docker has
simplified this process
Docker Image
● First Responsibility of DevOps Engineer: Containerize your applications. This means
creating docker images for your applications.
● Docker image is collection of layers.
● Lets build a docker image for an application gameoflife
○ Installation:
■ tomcat with Java 8 installed
■ copy the gameoflife.war file into webapps folder of tomcat
○ To make this work we have taken an existing image with java and tomcat installed
and copied our package into the folder and then application started working
○ Doing this on running container is not sensible, it would be better if we can have
some way of creating images.
○ Steps:
■ Take existing image
■ Copy the necessary files/configurations
■ Create an image with this.
● To Create images docker has Dockerfile , we will be using that from our next class.
Refer Here for docker file reference.{https://docs.docker.com/engine/reference/builder/}
Impartment links:-
https://directdevops.blog/2019/09/26/docker-image-creation-and-docker-image-layers/
https://directdevops.blog/2019/09/27/impact-of-image-layers-on-docker-containers-storage-dri
vers/
● If we can observe two image layers below “spc:0.1 & openjdk:8” images by command
“docker image inspect <image name>” the layers are almost the same in spc:0.1 we can
observe one extra layer
● Docker image is a collection of layers. Each layer in the Image contains its own data,
storage.
● When are layers created?. Mostly they are created when the content is changed
(Executing RUN, COPY, ADD instruction)
● For example in existing Dockerfile we will add one extra command like RUN mkdir
/helow then build an image
● So creating too many layers is not optimal. Use RUN statement carefully
● The docker image layers are read-only
·
● Forex if we create an image ‘X’ it will take 500MB size and again we created the same
image we think it will create an additional 500MB. But here Docker will take images
from the existing ‘X’ itself.
● The image layers are read-only. Whenever we create an image it will use the same image
layer.
● Whenever we try to create a container Docker will create one extra read-write layer.
● Whenever we change any content in that image. Only this read-write layer will change
no more modifications in original read-only layers.
● Whenever we defeat the container this read-write layer also created
● When we create a container, an extra R/w layer is created. All the changes done in that
container will be stored in the R/W layer. When we delete the container the R/W layer
gets deleted. So in Docker to preserve changes done inside the container we need to take
an extra measure (Volume)
● Docker Container is created from Docker Image. Docker Image is a collection of Image
Layers. Now while docker container is created all of the Image Layers are mounted as
one file system
● When the docker container is created CMD/ENTRYPOINT gets executed and as long as
this command is running the container will be in running state.
● When we create a docker container. And we run the container the container will be live
until the CMD/ENTRYPOINT is executing
● For ex:- the container spc:1.0 CMD[“java” “-jar”
“spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar”] if we executed docker container run
–d spc:1.0 (the container will be in running state
● If we executed like “docker container run –d spec:1.0 echo hollow” then docker will
execute only echo command and it will execute caz echo will replace the CMD so
docker executed the command and exit
● when we create a docker container every container will have a name and id.
● Every Docker container will have a unique container name and id. if the names are not
passed while creating, docker engine gives adhoc names
● If we want to work with particular container we need to know that container name/ID
* To create a container with specified name “docker container run --name <name> -d
<container name>”
● docker container is started in a detached mode. Is there any way to login into it
● execute docker attach <cont-id or name>
● execute docker container exec -it <cont> /bin/bash
● Remove all docker containers in running and stopped state {docker container rm
container id / docker container rm -f $(docker container ls -q) }
● Removing all docker container which are in stop position {docker container rm -f
$(docker container ls -q -a) }
● Logs: The logs command will show all the standard out-put stream of your
container{docker container logs containername:version}
● Also we can give filters to this logs like {docker container logs --since
2020-11-06T05:00 969acaf42cd1}
● The stats provides real-time information on the specified container or if you don’t pass
name or id it shows for all the running containers docker container stats
● TO put restrictions on docker container on how many CPUs it used and how much ram it
want to use {docker container run -d --name gov --cpus 1 --memory 128m -P gol:1.0}
NOTE:- if we don’t give any restrictions then docker will automatically take all the cpus and
memory it have and allocate which ever the container wants
● If we need to increase memory and cpu we can achieve it {docker container update
--memory 256M gol:3.0 gol:3.0 }
2) detached/background: in this mode your container will just show the container id and run
in background
https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/engine/reference/builder/
FROM
Syntax
FROM ubuntu
FROM openjdk:8
RUN:
● This instruction execute the commands in the created base image container in a new layer
● Run command runs with /bin/bash -c on Linux and cmd on windows
Example:
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
● shell =>
ENTRYPOINT
● This is the command that gets executed when starting the container. if ENTRYPOINT is
not found then CMD is executed when starting the container
● Syntax:
NOTE:- If both ENTRYPOINT and CMD Exists then ENTRYPOINT will be executable and
CMD will be arguments
CMD
Syntax:-
EX:1
FROM openjdk:8
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPS
HOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
EX:2 (Create a image with tag testing:2.0 with the following Dockerfile)
FROM openjdk:8
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPS
HOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
EX:3 (Create a image with tag testing:2.0 with the following Dockerfile)
FROM openjdk:8
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPS
HOT.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
We will execute the above Dockerfile and see the images below
NOTE:- ENTRYPOINT cannot be changed with args but whatever we write in CMD can be overwritten with
args
For ex:- we have “test” with ENTRYPOINT now we will pass the argument
docker container run –P test echo helloworld
there is no change in execution ENTRYPOINT will not effected
same way in test:2.0 we have CMD so we will pass arguments at run time and see. Below pic
the output came hello
In the same way in “test:3.0” ENTRYPOINT [“java”] CMD[] so if we pass argument CMD
will execute with argument pass and we have ENTRYPOINT here so only it can execute and
EXPOSE:
● This instruction informs Docker that the application listens on the specific network ports at runtime.
● Two Protocols can be specified TCP and UDP. TCP is default
Syntax:
● Example
FROM openjdk:8
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
docker container ls
LABEL:
● This instruction adds metadata to an image
Syntax:
Example:
FROM openjdk:8
LABEL author="khaja"
LABEL version="0.2"
LABEL project="QT"
RUN wget
https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
Syntax:
FROM openjdk:8
LABEL author="khaja"
LABEL version="0.3"
LABEL project="QT"
ADD https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-jar", "/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
ARG
● While building the image. Values of ARG will be changed
● ARG: this instruction allows variables to be passed while building an image.
● ARG is available only while building the docker image.
● While building images we need some customizations we go with ARG.
● ARG: this instruction allows variables to be passed while building image
EX:
FROM openjdk:8
ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
RUN wget ${url}
EXPOSE 8080
CMD ["java", “-jar", “spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
EX:-
FROM openjdk:8
LABEL author="shaik khaja ibrahim"
LABEL org="Learning Thoughts"
# Adding an argument with default value
# This value can be changed by passing
# docker image build --build-arg download_location="<newlocation>" ....
ARG download_location='https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar'
ADD ${download_location} spring-petclinic.jar
EXPOSE 8080
CMD [ "java", "-jar", "spring-petclinic.jar" ]
● If we we can build the image using the above dockerfile, “docker image build --build-arg
'https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-S
NAPSHOT.jar' -t spc:1.0 .
● We have define variable called “download location” and we have used that by using $download_location
● Lets create a container with this image and we will go inside the container and check for the variables “docker
container run --name myexp-1 -d -P spc:1.0 ”
● Lets enter into this image and verify the ARG “docker exec -it myexp-1 /bin/bash” and execute set command, set
command will show all available Environment variables.
https://docs.docker.com/engine/reference/builder/#arg
ENV:
· This instruction will set the environmental variable for all the subsequent instruction and
also in containers Build the image
EX;
FROM openjdk:8
ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
ENV myfilename=spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
RUN wget ${url}
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
EX:-
FROM openjdk:8
LABEL author="shaik khaja ibrahim"
LABEL org="Learning Thoughts"
# Adding an argument with default value
# This value can be changed by passing
# docker image build --build-arg download_location="<newlocation>" ....
ARG download_location='https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar'
ENV FILE_LOCATION='spring-petclinic.jar'
ADD ${download_location} ${FILE_LOCATION}
EXPOSE 8080
CMD java -jar ${FILE_LOCATION}
● Let's build a docker image with this dockerfile “docker image build --build-arg
download_location='https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petc
linic-2.2.0.BUILD-SNAPSHOT.jar' -t spc:2.0 . ”
● And let's create container “docker container run --name envdemo -d -P spc:2.0 ”
● And verify the environment variables “docker container exec -it envdemo /bin/bash”
USER:
● user instruction sets the username for any subsequent RUN, CMD and ENTRYPOINT
instructions in Dockerfile. Ensure the user is created before USER instruction in
dockerfile. Default username is root
WORKDIR
● workdir instruction sets the working directory for any subsequent RUN, CMD and
ENTRYPOINT instructions in Dockerfile and the default value would be /
ONBUILD
● This instruction adds to the image a trigger to be executed at later time when the image
is used as the base image for another build
STOPSIGNAL
● The STOPSIGNAL instruction sets the system call signal that will be sent to the
container to exit.
HEALTHCHECK
● This instruction tells how to test a container to check if it is still working
Syntax:-
WORKDIR
● WORKDIR sets the default home directory.
USER:
● Instruction will set the user. default user will be root. Before using USER instruction
user should be created.
Sample Dockerfile
Ex: 1
FROM ubuntu
RUN apt-get update && apt-get install openjdk-8-jdk wget -y
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
Activity:
EX:-1
FROM tomcat:9.0.39-jdk8-openjdk-slim
COPY --from=packaging /game-of-life/gameoflife-web/target/gameoflife.war /usr/local/tomcat/webapps/gameoflife.war
EXPOSE 8080
● If we observe the above Dockerfile if the first step we use ‘AS’ we have given the firs
build as one name and we use that in the second stage.
● If we can create an image using “docker image build -t gameoflife:1.0 .”
● And run the container
EX:-2
FROM openjdk:8u272-slim
COPY --from=builder /spring-petclinic/target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
/spring-petclinic.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic.jar"]
is working)
https://directdevops.blog/2019/01/31/docker-internals/
● PIDname space gives a virtual process tree to ur container to believe it has its own
process
● But for your host operating system, your container is another type of process
Network Namespace :-
● net namespace (Network Namespace) creates the isolated networking for each container
with its own network interface.
● So to my container I get process by PID Namespace and net-work by network id
namespace
https://www.toptal.com/linux/separation-anxiety-isolating-your-system-with-linux-namespaces
mount namespace:-
● mount namespace creation allows each container to have a different view of entire
systems mount point, this allows containers to have their own file system view which
starts from root
user namespace:-
● user namespace allows to create whole new set of user & groups for the containers
· Fortunately even in the Windows world we have namespaces now. The purpose of the
namespace is the same but underlying implementation differs. Refer this article
https://docs.microsoft.com/en-us/archive/msdn-magazine/2017/april/containers-bringing-docke
r-to-windows-developers-with-windows-server-containers
FROM tomcat:9.0.39-jdk8-openjdk-slim
LABEL author="khaja"
LABEL org="learningthoughts"
COPY --from=packaging /game-of-life/gameoflife-web/target/gameoflife.war
/usr/local/tomcat/webapps/gameoflife.war
EXPOSE 8080
● Whenever the code is committed we can generate a Docker Image with a new tag. This
tag is build locally on some machine, but it should be made available to others (In
Enterprise means other members, In public means anyone)
● Let's get Started with using a Default Registry called as DockerHub
● Let's build the docker image {docker image build -t gameoflife:1.0 }
● Lets create a tag that matches repository name
● {docker image tag gameoflife:1.0 tmaheedhar2/gameoflife:1.0}
● To push our imge to docker repository we nee to create a repository on docker hub for
that login to docker hub → hear we have organisations we can create a separate
organisations for separate project. Hear I have put my default organisation
‘tmaheedhar2’ → and create a repository i have given my repository name as
‘gameoflife’ → we can see multiple options in repository creation we can add
description, we can assign out git/bitbucket url to docker hub, so that when ever we push
the code to git it will automatically create an image and sent to docker hub
● The Important point is we have to give a proper tag to our image that matches out
repository neme.
● Then log in to repository via command line and give docker hub credentials {docker
login }
● Once login has succeeded then push the image to repository {docker push
tmaheedhar2/gameoflife:1.0 }
Using ACR (Azure container repository) :-
● Now lets use azure container registry. Refer Here for creating a docker registry in azure
cloud
● To login to Azure registry we need to install Azure CLI on our instance{curl -sL
https://aka.ms/InstallAzureCLIDeb | sudo bash}
● Once Azure CLI is installed we need to do login {az login}
● Then login into azure container registry {az acr login --name nameofouracr}
● Change the image tag to azure registry tag {docker image tag gameoflife:1.0
acrname/repositoryname:version}
● Then push the image to Azure container repository {docker push gameoflife:1.0
acrname/repositoryname:version}
●
AWS Elastic Container Registry
● Lets create a ECR in aws. Goto ecr service → get started → select repository for public
or private → give repository name on (access and tags) → and create repository
● Once repository is created and then open the repository then we will notice ‘view push
commands’
● We need to install AWS CLI to our vm { apt-get install awscli && aws -version }
● And if we can see view push commands we can notice authentication {aws ecr
get-login-password --region us-east-1 | docker login --username AWS --password-stdin
296760598094.dkr.ecr.us-east-1.amazonaws.com}
● If we are not authentication was not success then we need to configure “aws configure”
● We have to build image with keys name {docker tag gameoflife:1.0
296760598094.dkr.ecr.us-east-1.amazonaws.com/gameoflife:0.1}
● Then we need to push the image {docker push
296760598094.dkr.ecr.us-east-1.amazonaws.com/gameoflife:1.0}
● Docker Trusted Registry:
○ This registry is part of Docker Enterprise
○ This registry integrates with LDAP
● Artifactory (Jfrog) Docker Registry
● Docker Registry: This docker image can be used as registry Refer Here
NOTE:- This container is dealing with 3 layers now but inside the container it will look as if it
is one file system
● Internally the layers are combined and presented as one disk to container using storage
drivers.
● Now lets try to make some changes
Since the layers are read-only whenever you modify any existing content, the file gets copied
to read/layer and changes are recorded in the R/W. This approach is called as “copy-on-write”
How to preserve the data changed by the docker container
● Docker will store in os “/var/lib/docker”
● If we go to “/var/lib/docker/overlay2” it is the location docker will store all image layers
● Persistence/Preserving the data even after the container is deleted can be achieved by
storing the data outside the container. To do this we have 3 options
● Volumes; bind-mounts; tmpfs
Storage Drivers :-
● Copy-on-write strategy
● As we discussed above every docker container has image layers along with R/W layer
● All the changes are copied to the R/W layer for ex. If we modify any files in existing
image layers a copy of that will come to the R/W layer. And if we create a file then also
stored in an R/W layer. Even if we delete the file also it will maintain a copy
● For this We need a file system which understands image layers and R/W Layers and to
the container it should be file system much like any other linux/Windows file system
● Storage Driver understands read only image layers, Read/Write Thin layer of container
and combines (union) the layers to give one filesystem to container
Docker has the following storage drivers{to view our storage driver “docker info”}
● overlay2: This is preferred storage driver for all linux distributions
● aufs: This was preferred storage driver for Docker 18.06 and previous versions
● devicemapper
● btrfs and zfs
https://directdevops.blog/2019/09/27/impact-of-image-layers-on-docker-containers-storage-dri
vers/
● The above article will tell about “Impact of image layers and storage drivers on docker
filesystem”
● If we can see the container, root partition the root volume is overlay
● Internally the is drivers will do lot of work to give this kind of output
Bind mounts
● are stored anywhere on the host system.
● Oldest option available for persisting the changes
● Create a docker container with bind mount option
Syntax:- docker container run –it –name <name> -v <path in host-os>:<path in container>
<image name> /bin/sh
Let’s create a new container using the same storage as mount and the same data should be
loaded in the new container
NOTE:- By using the ‘mount’ command we will map the folder location. Hear –v and mount
command will do the same job .
One extra feature with mount command is for ex” docker container run --mount
type=bind,source=/home/ubuntu/test,target=/app,read only <imagename” we can see readonly option here so we
can give read only to folder in container so that docker can’t modify files in that location
Note:- In this concept bind-mounts . We need to share a folder which is created by us and
which can be used by many processes in systems and they want to modify something which
causes a lot of errors. TO avoid that docker has concept called “Volumes”(a mechanism
which docker manages)
Tempfs mount:-
● Mounted on to tmpfs in memory
● Tempfs mount can be created using –tmpfs or –mount flag
● Tempfs will be mounted on the RAM of our container
docker container run -d --name tmp1 --tmpfs /app alpine sleep 1d
docker container inspect tmp1
docker container run -d --name tmp2 --mount type=tempfs,destination=/app alpine sleep 1d
● We will take two containers cont1 & cont2 and every container has IP address lets see
docker container run -d --name cont1 alpine sleep 1d
docker container run -d --name cont2 alpine sleep 1d
● Now let’s see if containers have network connectivity b/w then by executing
docker container exec cont1 ping cont2
· Now lets check connectivity by pinging ip address
Syntax:- docker container exec <container-1> ping –c <count> <IP address of second container>
NOTE:- We are able to ping from one container to other by ip addresses but names are not
resolving
Docker Volumes:-
FROM tomcat:jdk8-openjdk
LABEL team="qtdevops"
RUN cd webapps/ && wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/gameoflife.war
VOLUME /usr/local/tomcat
EXPOSE 8080
● Now lets create a docker container from image without volume instruction and other
with volume instruction
* if we can see the path “/var/lib/docker/volumes/” the container's data will be present and if
we delete the containers also data will be still present.
● “Docker volume ls “ is the best command to know the volume info
● Even if we delete the the docket container the volume/data is still present for that best
way is to create a docker volume by volume instrecton or creating docker file with
‘volume’ instruction
● What if the VOLUME instruction is not used in Docker Image?
https://directdevops.blog/2019/10/03/docker-volumes/
● If your organization is running some kind of third party storage like netapp,
vsphere storage etc Refer Here to install volume drivers
https://hub.docker.com/search?q=&type=plugin&category=volume
● for cloud related storage drivers
http://docs.docker.oeynet.com/docker-for-azure/persistent-data-volumes/#u
se-a-unique-volume-per-task
Docker volume commands:-
1) docker volume ls {to view all volumes}
2) docker volume prune {to delete all volumes explicitly}
3) docker volume create volume_name {to create a new volume}
4) docker volume inspect volume_name {to inspect the volume}
5)
Activity:-
● Lets create a volume and mount it to tomcat container {docker container run -d -P
--name my-cont-1 --volume volume1:/usr/local/tomcat/volume1 tomcat:jdk8-openjdk}
● Not if we see the container inspect it is mounted
●
Activity 2:-
EG:-
FROM openjdk:8
ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
ENV myfilename=spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
RUN mkdir /app && cd /app && wget ${url}
VOLUME /app
WORKDIR /app
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"
● Volume is still available after the container is deleted, so we don't lose data.
● Planning for volume creation in Dockerfile might not happen in all cases, the we need to
create and mount volumes
● Volumes are of two types
i) anonymous volumes
ii) named volumes
My using below command we can create our own volume and default location of the volume is
“var/lib/docker/volumes”
● Let’s try to share the data between two container using the same volume
docker container run -d --name alp3 --mount source=myvol,target=/test alpine sleep 1d
docker container exec alp3 touch /test/2.txt
docker container exec alp2 ls /test
● Docker volume has also an option of docker volume drivers which allows volumes to be
created in nfs, aws s3 etc
● Now let's remove the containers
docker container rm -f $(docker container ls -a -q)
docker volume ls
● 1. Sandbox: this is containers network stack with features such as container network
interfaces, routing tables and DNS settings
● 2. Endpoint: joins sandbox to network
● 3. Network: Network is collection of endpoints with connectivity b/w them.
● Whenever we need to connect to network we need whole network to be workable for
that Docker need ‘CNM-driver interface’
CNM Driver Interfaces:-
● Network Driver
● IPAM (IP Address Management) Drivers
● And now install docker and execute “ifconfig”. And then execute the ifconfig and we
can see ‘docker0 (docker network)’. Once we install docker we are getting an additional
interface.
● Run the container and inspect that container
docker container run -d --name my-cont-1 tomcat:jdk8-openjdk
docker container inspect my-cont-1
● If we observe the newly created container is taking a new IP address with the docker ip
address pool and also it is having a separate MAC address.
Host: with a host driver, a container uses the networking stack of the host.
Bridge: This is the default driver. Connecting multiple containers on the same bridge network
in a single host. The containers created by default are connected to bridge network
Overlay: This driver creates an overlay network to support multi-host containers.
MACVLAN:- Establish a connection b/w container interfaces and host interfaces. It provides
Private ip. It will use a MAC address to establish a connection. A container which is created
using MACVLAN will have a MAC address.
None:- used to create containers with its own network stack. It doesn’t configure any interfaces
inside the container so it can’t establish any sort of communication b/w containers
Activity:-
● Lets experiment with default bridge network
● Create two containers dc1 and dc2
Now let's find IP addresses of dc1 and dc2 . try pinging with ip address
● As per the observations we can ping from one container to another container using ip
address in default bridge network
● Now lets create user defined bridge network
Now let's inspect the created network docker network inspect <network-name>
* Lets create two containers as shown above in user-defined network
Activity:-
Bridge Network
● In docker by default we will have a default bridge network with name ‘bridge’
● Let’s create a new bridge (user-defined bridge)
Let’s find the ip address of the container (docker network inspect <mybridge>)
● Communication b/w containers if they are on different servers which are in the same
network.
Activity:-
● Create 3 servers connected to each other within a network and install docker on all the
three servers
● How can we enable communication b/w two container running on different hosts
● Docker has network driver which is called as overlay which can help in connecting two
containers running on different hosts