Mar 2023-7-45AM Docker Notes
Mar 2023-7-45AM Docker Notes
Docker Editions:
Type: Containerization
Vendor: Docker INC
DockerContainer --> Run time instance of an image.If you run docker image container
will be created
that's where our application(process) is running.
DockerRegistriy/Repository
Public Repo --> Docker hub is a public reposotiry. Which contains all the open
source softwares as
a docker images. We can think of docker hub as play store for docker images.
Docker Enigine/Daemon/Host --> It's a software or program using which we can create
images & contianers.
Docker is cross platform.
Docker CE
Docker CE will not be supported by Redhat.
Docker EE
Docker EE will be support most of the os including redhat.
# You will get permison denied error as regular user dosn't have permisions to
execute docker commands.Add user to docker group.
# Exit From Current SSH Terminal & SSH(Login) again .Then execute
docker ps
# Amazon Linux
================
sudo yum update -y
sudo yum install docker -y
sudo service docker start
ex:
sudo usermod -aG docker ec2-user
Once you add user to group exit from the server and login again.
# Get docker information
docker info
# You will get permison denied error as regular user dosn't have permisions to
execute docker commands.Add user to docker group.
# Exit From Current SSH Terminal & SSH(Login) again .Then execute
docker ps
# List Images
docker images
Docker Registry
---------------
Docker registry is responsible for hosting and distributing images. It's a
collection of repositories. The default registry is the Docker Hub.
Registry allows you to push and pull the container images as needed.
Nexus
JFrog
DTR
Docker Repository
----------------
Tag
---
Docker Is a containerization software using which we can create build and deploy
our applications as containers.
Docker Editions:
Type: Containerization
Vendor: Docker INC
Public Repo --> Docker hub is a public reposotiry. Which contains all the open
source softwares as
a docker images. We can think of docker hub as play store for docker images.
Docker Enigine/Daemon/Host --> It's a software or program using which we can create
images & contianers.
Docker CE
Docker CE will not be supported by Redhat.
Docker EE
Docker EE will be support most of the os including redhat.
# You will get permison denied error as regular user dosn't have permisions to
execute docker commands.Add user to docker group.
# Exit From Current SSH Terminal & SSH(Login) again .Then execute
docker ps
# Amazon Linux
================
sudo yum update -y
sudo yum install docker -y
sudo service docker start
ex:
sudo usermod -aG docker ec2-user
Once you add user to group exit from the server and login again.
# Get docker information
docker info
# You will get permison denied error as regular user dosn't have permisions to
execute docker commands.Add user to docker group.
# Exit From Current SSH Terminal & SSH(Login) again .Then execute
docker ps
docker images
Docker Registry
---------------
Docker registry is responsible for hosting and distributing images. It's a
collection of repositories. The default registry is the Docker Hub.
Registry allows you to push and pull the container images as needed.
Nexus
JFrog
DTR
Docker Repository
----------------
Tag
---
FROM tomcat:8-jdk8-corretto
COPY target/maven-web-application*.war /usr/local/tomcat/webapps/maven-web-
application.war
# Build Image
Defautl Docker file Name: Dockefile
docker build -t <imageName> .
If you have docker file with custom name using -f <fileName> while building docker
image.
docker build -f DockerfileMaven -t <imageName> .
Note: Image name should have registry & repository details along with tag(version).
Public Repo (Docker Hub)
ex:
docker build -t dockerhandson/maven-web-application:1 .
In Case ECR:
-----------
docker build -t 485430697192.dkr.ecr.ap-south-1.amazonaws.com/maven-web-
application:1 .
In Case OF Nexus/Jfrog
---------------------
ex:
docker build -t nexus.tcs.com/maven-web-application:1 .
Priavate Repo
docker login -u <username> -p <password> <URL>
In Case ECR
-----------
docker login -u <username> -p <password> 485430697192.dkr.ecr.ap-south-
1.amazonaws.com
Nexu/Jfrog
ex:
# In Case of Dockerhub
# Incase of Nexus
docker push nexus.tcs.com/maven-web-application:1
Delete Image
Note: We cann't remove images if there are running container for the image.We
cann't force delete images if there is running container.
If container is in stopped(exited) state we can force delete image for the stopped
container.
How can we move/copy images from one server to another server with out repo?
# In destination server
docker load -i <fileName>.tar
How can we move/copy images from one server to another server with out repo?
# In destination server
docker load -i <fileName>.tar
docker create will only create a container but it will not start the container.
docker run will create a container and start the container.
Access Application which is running Using Docker Server IP & Host Port.
http://<DOCKERSERVERPUBLICIP>:<HOSTPORT>/maven-web-application
docker ps
docker container ls
docker ps -a
docker container ls -a
docker ps -q
docker container ls -q
docker ps -aq
Restart Container
Stop Container
Kill container
docker stop will first send SIGTERM then SIGKILL it will kill process with grace
period. Docker kill send SIGKILL it will kill process with out any grace period.
ex:
docker exec javawebapp ls
docker exec javawebapp pwd
or
If we have to come out with out stoping the process cntl p+q.
How to copy files from container to host system or host system to container?
docker cp
Container to the system
docker cp javawebappone:/usr/local/tomcat/logs/catalina.2020-04-23.log
javawebappone.log
docker cp <SystemPath>/<fileName><containerName>:</pathOftheContainerFile>
ex:
docker run --name javawebapp -d --cpus="0.25" --memory="256Mb" -p 7070:8080
dockerhandson/java-web-application:1
EX:
FROM tomcat:8.0.20-jre8
COPY target/java-web-app*.war /usr/local/tomcat/webapps/java-web-app.war
DockerImage --> It's package which contains application code + all it's
dependencies(Software+ENV Varibles + Config Files) together.
Dockerfile keywords
===================
FROM
MAINTAINER
COPY
ADD
RUN
CMD
ENTRYPOINT
WORKDIR
ENV
EXPOSE
USER
VOLUME
LABEL
ARG
FROM --> FROM indicates the base image(Layers) such as OS(Libraries) like
centos,alpine,ubuntu or any programatic language(java,python,node,go) or some other
sofwares like tomcat,nginx,mysql..etc. Using base image we can create our own
image.All sub sequent instructions(Layers) will be executed/created on top of base
image
layers.
Syntax:
FROM <ImageName>
Ex:
FROM tomcat:8.0.20-jre8
FROM openjdk:8-alpine
FROM nginx
FROM node
FROM python
COPY --> Using COPY we can copy files/folders to the image. Files/Folders will be
copied to the imgage while building an image.
Using COPY instruction we can COPY local(build context) files or
directories(folders) to the image while building image.
SYTNAX:
======
COPY <source> <destination>
ServerFile/FolderPath PathInsideImage
EX:
COPY target/java-web-app.war /usr/local/tomcat/webapps/java-web-app.war
# Below also valid it will copy all the files/folder from HOST Machine current
working
directory to Image working dirctroy.
COPY . .
ADD --> ADD also can copy files to the image while creating image. ADD can copy
local files from host server(build context) and also it can download files from
remote HTTP/S locations while creating a image.
EX:
# Local file
ADD target/java-web-app.war /usr/local/tomcat/webapps/java-web-app.war
Note: If it's tar file ADD will copy file and also it will extract tar file.
1) #Shell Form
2) #Executable Form
When the exec form of the CMD instruction is used the command will be executed
without a shell.It will run that on PID 1.
CMD ["executable","param1","param2"]
Examples:
FROM ubuntu:trusty
CMD ["/bin/ping","localhost"]
Examples:
FROM ubuntu:trusty
ENTRYPOINT ["/bin/ping","localhost"]
When instruction is executed in exec form it calls executable directly, and shell
processing does not happen.
For example, the following snippet in Dockerfile-
ENV name John Doe
ENTRYPOINT ["/bin/echo", "Hello, $name"]
When the container runs as docker run -it <image> , it will produce output- Hello,
$name
Note that the variable name is not substituted.
SHELL form-
<instruction> <command>
CMD executable param1 param2
When using the shell form, the specified binary is executed with an invocation of
the shell using /bin/sh -c
Examples:
FROM ubuntu:trusty
ENTRYPOINT echo "Hello world"
FROM ubuntu:trusty
CMD echo "Hello world"
When instruction is executed in shell form it calls /bin/sh -c <command> under the
hood and normal shell processing happens.
Syntax:
#Shell Form
RUN <commond with args>
#Executable Form
RUN ["commond" , "Arg1","Arg2"]
EX:
RUN mkdir -p /opt/app
RUN tar -xvzf /opt/apache-tomcat-8.5.54.tar.gz
CMD --> CMD instruncation will execute commands. CMD commands or instructions will
be executed while starting the container.CMD insturction can be used to start the
process inside the container.
#Shell Form
CMD <commond with args>
#Executable Form
CMD ["commond" , "Arg1","Arg2"]
# Shell Form
CMD java -jar springapplication.jar.
# Executable form
CMD ["java", "-jar" , "springapplication.jar"]
RUN instructions will be executed while creating a image. CMD Instructions will be
executed while creating a
container.We can have more than one RUN keyword in a docker file. All the RUN
keywords will be processed while creating an image in the defined order(top to
bottom).
#Shell Form
ENTRYPOINT <commond with args>
#Executable Form
ENTRYPOINT ["commond" , "Arg1","Arg2"]
Yes we can have both in a docker file. CMD instructions will not be executed if we
have both CMD & ENTRYPOINT.CMD instructions will be passed as an arguments for
ENTRYPOINT.
FOR Example:
CMD ls
ENTRYPOINT ["echo", "Hello"]
# Out Put
Hello ls
CMD start
ENTRYPOINT ["sh", "catalina.sh"]
ARG -> ARG Instruction defines a variable that can be passed at build time. Once it
is defined in the Dockerfile you can pass with this flag --build-arg while building
the image. We can have multiple ARG instruction in the Dockerfile. ARG is the only
instruction that can precede the FROM instruction in the Dockerfile.
ARG values are not available after the image is built. A running container won’t
have access to an ARG variable value
EX:
ARG TAG=latest
FROM centos:$TAG
LABEL
LABEL branch=develop
An image can have more than one label. You can specify multiple labels on a single
line.
WORKDIR --> WORKDIR is used to define the working directory of a Docker container
at any given time. The command is specified in the Dockerfile.It is optional
(default is / , but base image might have set it), but considered a good practice.
Subsequent instructions in the Dockerfile, such as RUN , CMD and ENTRYPOINT will
operate in this dir.
Ex:
WORKDIR /app
USER
The USER instruction sets the user name (or UID) and optionally the user group (or
GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions
that follow it in the Dockerfile
EXPOSE
The EXPOSE instruction informs Docker that the container listens on the specified
network ports at runtime.
The EXPOSE instruction does not actually publish the port. It functions as a type
of documentation between the person who builds the image and the person who runs
the container, about which ports are intended to be published. To actually publish
the port when running the container, use the -p flag on docker run to publish and
map one or more ports.
EXPOSE 8080
# Authentication with ECR(Install AWS CLI And execute below command after attaching
IAM Role)
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --
password-stdin 935840844891.dkr.ecr.ap-south-1.amazonaws.com
Note: Create IAM Role with required policy and attach to EC2 Servers.
Docker Networks
What is network ?
Group of servers/devices connected to each other in a specific network. If Servers
are in same network each one can talk to another server.
Docker network
If Containers are in two different networks. They can't accees each other.
In which docker network the container will be created if we don't mention network
name while
creating a container ?
docker network ls
To Check Go inside javawebapp container and ping mavenwebapp container using name &
ip. When we ping using ip it will work it will not able to communicate using name.
Developers should not code the connectivity based on the IP in case of contianers.
Since IP address of cotnainers will be dynamic.
IP will keep changing.
# Create Network
Syntax: docker network create -d <driver> <networkName>
Ex:
docker network create -d bridge mithuntechbridge
# Inspect network
docker network inspect <networkNameOrId>
If containers are created in custom bridge network. Each container can access other
using containerName/ContainerIP.
# Delete Containers which are running in default bridge or create container with
different name.
# Remove Network
docker network rm <networkNameOrId>
But we can't create more than one cotnainer with same container port in host
network.We no need to do port publish to access
containers.
Docker volumes are file systems mounted on Docker containers to preserve data
generated by the running container.
Create docker network using below commond(If it's not created already)
Create Spring Application Container in above network & which will talk to mongo
data base container
Access Spring application & insert data it will be inserted to mongo db. Delete
and recreate mongo container
what ever you have inserted will no longer be availbale. As once we delete
contaienr data also will be deleted
in container.
Bind Mounts:
============
Bind mounts may be stored anywhere on the host system. They may even be important
system files or directories. Non-Docker processes on the Docker host or a Docker
container can modify them at any time.
Volumes are the preferred mechanism for persisting data generated by and used by
Docker containers. While bind mounts are dependent on the directory structure and
OS of the host machine, volumes are completely managed by Docker. Volumes have
several advantages over bind mounts:
Volumes are stored in a part of the host filesystem which is managed by Docker
(/var/lib/docker/volumes/ on Linux).
Non-Docker processes should not modify this part of the filesystem. Volumes are the
best way to persist data in Docker.
# To list volumes
docker volume ls
Access Spring application & insert data it will be inserted to mongo db. Delete and
recreate mongo container with same volume mapping. You can see the data back.
Volume Driver Plugin --> It's a piece of code or software which is responsible for
creating a storage and attaching the storage to the container.
1) Create IAM User with EC2 Full Access and user access key & Secret Key of the
same. Replace your access key & secret below. Or Use Your root aws account access
Key & Secret Key.
EX:
Docker Compose
==============
With out compose to deploy above applications which has only 2 images we executed
below commnads.
With Docker Compose we deploy/create all of the above 4 with single command using
compose file.
With Compose
We will define all the serivces(cotainers) details in compose file using compose
file we can deploy multi container applications.
version: '3.1'
services:
springboot:
image: dockerhandson/spring-boot-mongo:1
restart: always # This will be ignored if we deploy in docker swarm
container_name: springboot
environment:
- MONGO_DB_HOSTNAME=mongo
- MONGO_DB_USERNAME=devdb
- MONGO_DB_PASSWORD=devdb@123
ports:
- 8080:8080
depends_on:
- mongo
networks:
- springappbridge
mongo:
image: mongo
container_name: springboot-mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=devdb
- MONGO_INITDB_ROOT_PASSWORD=devdb@123
volumes:
- mongodbvol:/data/db
restart: always
networks:
- springappbridge
volumes:
mongodbvol:
driver: local
networks:
springappbridge:
driver: bridge
Commands
# Syntax Check
docker-compose config
# Create Services/Contianers
docker-compose up -d
# Remove Services/Contianers
docker-compose down
Ex:
docker-compose -f docker-compose-springapp.yml config
docker-compose -f docker-compose-springapp.yml up -d
docker-compose help
# In docker swarm we will use below command to deploy services using docker
compose.
docker stack deploy --compose-file docker-compose.yml <stackName>
#!/bin/bash
sudo apt-get update
sudo apt-get install curl -y
sudo curl -fsSL get.docker.com | bash
sudo usermod -aG docker ubuntu
Note: Make Sure You Open Required/All Ports in AWS Security Groups.
======================================================================
# Initialize docker swarm cluster by exeuting below command on docker server which
you want make it as Manager
docker swarm join-token master (Get the master token in manager & exeute in nodes
which u want to join cluster as managers)
======================================================================
Global --> A Global Service in Docker Swarm Mode is used to create a service that
has a container running on each and every node in cluster.When we add any new node
to cluster cotainer for Gloabl servcies will be created on that node
automatically.
docker service ls
#Inspect docker node to check the labels and role of the node
docker stack ls
version: '3.1'
services:
springboot:
image: dockerhandson/spring-boot-mongo:latest
restart: always # This will be ignored if we deploy in docker swarm
container_name: springboot
environment:
- MONGO_DB_HOSTNAME=mongo
- MONGO_DB_USERNAME=devdb
- MONGO_DB_PASSWORD=devdb1234
ports:
- 8080:8080
working_dir: /opt/app
depends_on:
- mongo
deploy: # This will be considered only in docker swarm.
replicas: 2
update_config:
parallelism: 1
delay: 20s
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 5
networks:
- springapp
mongo:
image: mongo
container_name: springboot-mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=devdb
- MONGO_INITDB_ROOT_PASSWORD=devdb1234
volumes:
- mongodb:/data/db
restart: always
networks:
- springapp
volumes:
mongodb:
networks:
springapp:
# Authentication with ECR(Install AWS CLI And execute below command after attaching
IAM Role)
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --
password-stdin 935840844891.dkr.ecr.ap-south-1.amazonaws.com
Note: Create IAM Role with required policy and attach to EC2 Servers.