DOCKER
Prepared by:
DILLI BALAJI
-THINK GOOD & DO GOOD-
DOCKER
1. Why you need containerization
2. ADVANTAGES of CONTAINARIZATION
3. Why do we use Docker
4. Docker Commands
5. Port Mapping
6. Docker Image
7. Docker File
Why you need containerization?
It is also virtualization technology and alternative to hypervisor virtualization
Containerization, using technologies like Docker, is essential for modern software development and
deployment because it enables portability, efficiency, scalability, and improved security by packaging applications and
their dependencies into isolated containers, facilitating faster and easier deployment across different environments
container is isolated
ADVANTAGES of CONTAINARIZATION
Docker
Docker simplifies and accelerates software development and deployment by allowing you to package
applications with their dependencies into lightweight, portable containers, ensuring consistent environments and
efficient resource usage across different machines
Within a docker host multiple number of same host can be deployed
smallest docker images is alphine and it is 4mb
Introduction to Docker
Why do we use Docker?
1. Portability - An applications can be bundled in to a single unit and same unit can be deployed to various
environments such as dev server, testing server and production server without making any changes to the container.
2. Light Weight - Docker containers are pretty lightweight so it provides a smaller footprint of the operating system via
containers.
3. Fast Delivery and Scalable - Since Docker containers are pretty lightweight, So they can be deployed faster and they
are very easily scalable.
4. Docker used for Continuous Deployment and Testing. So With the help of containers, it becomes easier for teams
across different units, such as development, QA and Operations to work seamlessly across applications.
5. Docker also provides you, ability to run multiple isolated OS on single host.
6. Resource Optimization - Docker enable you to utilize the maximum resources and reduce the resource wastages of
your hardware.
Creating instance of Amazon Linux OS using AWS CLI
aws ec2 run-instances \
--image-id ami-0dc3a08bd93f84a35 \
--count 1 \
--instance-type t2.micro \
--key-name Devops_project_key \
--security-group-ids sg-09167fa17131f7433 \
--subnet-id subnet-0850f61964f555d03 \
--associate-public-ip-address \
--tag-specifications
'ResourceType=instance,Tags=[{Key=Name,Value=Docker_Server_AmazonLinux}]' \
--region us-east-1
Creating instance of Ubuntu OS using AWS CLI
aws ec2 run-instances \
--image-id ami-084568db4383264d4 \
--count 1 \
--instance-type t2.micro \
--key-name Devops_project_key \
--security-group-ids sg-09167fa17131f7433 \
--subnet-id subnet-0850f61964f555d03 \
--associate-public-ip-address \
--tag-specifications
'ResourceType=instance,Tags=[{Key=Name,Value=Docker_Server_Ubuntu}]' \
--region us-east-1
connect the instance Server 1 via ssh:
Name the Hostname as below for our Identification:
cat etc/hostname
sudo vi /etc/hostname
cat etc/hostname
restart the instance to take effect we changed
after reboot:
sudo systemctl start docker
sudo systemctl enable docker
docker -v
sudo docker version
Install Docker Server 2(Ubuntu OS):
🛠 2. Set a new hostname (e.g., my-new-hostname)
sudo hostnamectl set-hostname my-new-hostname
or
cat /etc/hostname
Sudo vi
/etc/hostname
Edit as Docker_ServerUbuntu
Reboot the server for the changes to take effect.
sudo systemctl reboot
after reboot the name is changed
Install Docker on Ubuntu
1. Update your package index
sudo apt-get update
2. Install required packages
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release -y
3. Add Docker’s official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
4. Set up the Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) \
signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. Update your package index again
sudo apt-get update
6. Install Docker Engine
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
🔍 Verify Docker installation
sudo docker --version
sudo docker -v
sudo docker version
sudo docker info
Lists all details like Container, Running,stopped, Paused,Images
docker system --help
Commands:
df Show docker disk usage
events Get real time events from the server
info Display system-wide information
prune Remove unused data
sudo docker system prune
sudo docker stats – (shows real time usage)
sudo docker images
Sudo docker pull httpd
sudo docker pull ubuntu
Sudo docker images
sudo docker system df
sudo docker ps
sudo docker run -it --name httpd01{name}
sudo docker exec -itd httpd02
sudo docker ps -a
to go inside the container
sudo docker exec -it httpd02 /bin/bash
without going inside the contaainer we can execute commands like below,
sudo docker exec httpd02 uname -a
sudo docker exec httpd02 mkdir /tmp/Success
then you switch over to the container and check
sudo docker exec -it httpd02 /bin/bash
How to copy files from local host to container and how to take the files from
container:
sudo docker cp {source} {destination}
sudo docker cp do-it httpd02:/tmp/did-it
when you login inside the container and check ,copied files are seen
sudo docker ps -a
to stop a container
sudo docker stop {container ID or Name}
sudo docker stop 6e3d2 or httpd02
to start a container
sudo docker start {container ID or Name}
sudo docker start httpd01
sudo docker sustem df
sudo docker start 6e
sudo docker ps -a
to remove a contaainer
sudo docker rm httpd01
it throws error while removing it, as the container is still running .
If we need to remove the running container forcefuly ,then
sudo docker rm -f httpd01
It is not recommended to remove the container forcefully
It is highly recommended to stop the container and then remove it.
sudo docker stop {container name or ID}
sudo docker stop httpd02
sudo docker logs {container name or ID}
sudo docker logs httpd02
sudo docker top {container name}
sudo docker top httpd02
for understanding and if you need to change the container name we provided
sudo docker rename httpd02 web02
sudo docker pull ubuntu:tags
if we give without tags it’s automatically pulls the latest image
Port Mapping:
Accessing the container using network port:
sudo docker run -itd --name myapp -p 82:80 httpd
-p 82:80 — What does it mean?
This is port mapping between your host machine and the Docker container.
Port Location Meaning
The port you use to access the app from outside the
82 Host port (your laptop/server)
container.
Container port (inside
80 The port where Apache listens inside the container.
Docker)
sudo docker run -itd --name myapp -p 82:80 httpd
while copying the IP
and pasting the IP with
:port number {:82} ,it will gives the output like below
if we
check
the
docker Container with
sudo docker ps -a
Here { 0.0.0.0:82->80/tcp }
:82 Host is listening on port 82.
->80 Traffic is forwarded to port 80 inside the container.
For example:
In this same case we have Developer team and quality team. Both access this IP , but port
for them is different.
How ?
lets assign port 82 is for DEV team as above and 84 is for quality team
so
sudo docker run -itd --name myapp-qa -p 84:80 httpd
when we copy the IP
and paste with port 84 as we given the port for Quality team. The output will be:
To Know the complete info of a Docker Images:
sudo docker inspect myapp-qa
you will see the complete info of a image,and particularly you can see the port 84 (port
forwarding) which is assigned to that container for quality team.
"84"
→
Requests to port 84 on your host machine are forwarded to port 80 in the container.
sudo docker history httpd
Steps For Back up
(it’s just for example- Ubuntu OS backedup to AmazonLinux Server
it can be done with other):
lets take ubuntu Image needs to take backup
sudo docker save ubuntu:latest > ubuntu-backup.tar
Now we are going to take copy the Back-up file from Ubuntu Server to another server (Amazon-
Linux Server)
Ubuntu Server → AmazonLinux Server
In AmazonLiniux Server
In Ubuntu Server
scp ubuntu-backup.tar [email protected]:/home/backedup_file
but we are getting the errors as
this due to Private key is needed to transfer the file from Ubuntu Server to AmazonLinux
Server
The steps need to be performed:
Step 1:
copy the key to Server 1 temporarily
Copy the Private Key from Local Machine to Server 1 (Ubuntu Host)
scp -i ~/Devops_project_key.pem ~/Devops_project_key.pem
[email protected]:/home/ubuntu/
after this command check in Ubuntu Server(Host)
Step 3:
Securly copying the Backup File to server 2 (AmzonLinux Host)
scp -i ~/Devops_project_key.pem ubuntu-backup.tar [email protected]:/home/ec2-user
after this command check in AmazonLinux Server(Host)
Load the ubuntu-backup.tar file in server 2 (AmazonLinux)
sudo docker load -i ubuntu-backup.tar
sudo docker images
To remove Container
sudo docker rmi httpd
sudo docker rmi -f httpd
Delete image in a single command
sudo docker images -a -q {display all the images quitly}
for removing images
sudo docker rmi $(sudo docker images -a -q)
-a =all -q =quitely
for this container should not be in use
then you see there are no images
sudo docker ps (shows running container)
sudo docker stop running_container-1
sudo docker stop c2 88
sudo docker ps -a (shows all container )
sudo docker rm {container_name or Container_ID}
sudo docker rm 8133f
sudo docker rm myapp
Docker Image
Create Docker Image:
we should have base image and we have ubuntu
sudo docker images
Launching this ubuntu latest image creating a container and making changes in that and
again converting into image
run Ubuntu Container
sudo docker run -itd ubuntu001 ubuntu:latest /bin/bash
sudo docker ps
execute the ubuntu001 container
sudo docker exec -it de90 /bin/bash
make changes in it example make a directory
mkdir try-file && cd try-file
vi success
this error is due vi editor is not installed.
So installing vim editor in it. (This will install vim, which includes the vi command.)
apt-get update
vi success
after doing some changes exit that container
and do commit
sudo docker commit image_ID
sudo docker commit de90dedc3956
the above pic shows that the Image is created without Tag and Name ,for this
sudo docker commit de90dedc3956 final_image:2
Now the customized final_image is to be launched as Container
sudo docker run -it --name always_try final_image:2 /bin/bash
now we are inside the container and try to check the image
compare the containers ,both will be same,
How to push the image to docker registry
For this we need account in docker hub
now try
sudo docker push {name_of_the_image:Tag}
it throws the error Access to the resource is denied
so we need to login to our docker registry by,
sudo docker login
paste the code in browser pop up
and it ask username and passworrd ,enter it then see the command prompt
by mistaenly I have created the tag wrongly with docker hub registry name, the correct one
is ,
Now login to docker hub and check it will have our pushed images to the repository as below
Docker File
1. What is Dockerfile?
A Dockerfile is a text document containing instructions for building a Docker
image. It outlines the steps needed to create a containerized environment, including the
base image, dependencies, and application code.
2. Why Dockerfile is required and What is the use of it?
A Dockerfile acts as a blueprint, providing instructions to Docker on how to create
an image, including the base image to use, commands to execute, and the default
command to run when the container starts.
3. How to Create Dockerfile?
Docker file consist of Instructions and arguments
Dockerfile Explained with Examples
FROM
FROM instruction used to specify the valid docker image name. So specified
Docker Image will be downloaded from docker hub registry if it is not exists
locally.
Examples:
FROM docker.io/centos:latest
FROM docker.io/centos:6
If tag "6" is not specfied, FROM instruction will use the latest tag (version).
This is a mandatory instruction in dockerfile, rest all are optional and those
can be used based on the requirement.
MAINTAINER
MAINTAINER instruction is used to specify about the author who creates this
new docker image for the support.
Examples:
MAINTAINER Administrator
MAINTAINER admin @ sdbpixels.net
MAINTAINER Devops Engineer(admin @ sdbpixels.net)
LABEL
LABEL instruction is used to specify metadata informations to an image. A
LABEL is a key-value pair.
Example:
LABEL "Application_Environment"="Development"
EXPOSE
EXPOSE instruction is used to inform about the network ports that the
container listens on runtime. Docker uses this information to interconnect
containers using links and to set up port redirection on docker host system.
Examples:
EXPOSE 80 443
EXPOSE 80/tcp 8080/udp
ADD
ADD instruction is used to copy files, directories and remote URL files to the
destination (docker container) within the filesystem of the Docker Images.
Add instruction also has two forms - Shell Form and Executable Form.
Examples:
Shell Form - ADD src dest
ADD /root/testfile /data/
Executable Form - ADD ["src","dest"]
ADD /root/testfile /data/
If the "src" argument is a compressed file (tar, gzip, bzip2, etc) then it will
extract at the specified "dest" in the container's filesystem.
COPY
COPY instruction is used to copy files, directories and remote URL files to the
destination within the filesystem of the Docker Images. COPY instruction
also has two forms - Shell Form and Executable Form.
Examples:
Shell Form
COPY src dest
COPY /root/testfile /data/
Executable Form
COPY ["src","dest"]
COPY /root/testfile /data/
If the "src" argument is a compressed file (tar, gzip, bzip2, etc), then it will
copy exactly as a compressed file and will not extract.
RUN
RUN instruction is used to executes any commands on top of the current
image and this will create a new layer. RUN instruction has two forms - Shell
Form and Executable Form.
Examples:
Shell form:
RUN yum update
RUN systemctl start httpd
Executable form:
RUN ["yum","update"]
RUN ["systemctl","start","httpd"]
CMD
CMD instruction is used to set a command to be executed when running a
container. There must be only one CMD in a Dockerfile. If more than one
CMD is listed, only the last CMD takes effect.
CMD instruction has two forms - Shell Form and Executable Form.
Example :
Shell form:
CMD ping google.com
CMD python myapplication.py
Executable form:
CMD ["ping","google.com"]
CMD ["python","myapplication.py"]
ENTRYPOINT
ENTRYPOINT instruction is used to configure and run a container as an
executable. ENTRYPOINT instruction also has two forms - Shell Form and
Executable Form.
Examples:
Shell form:
ENTRYPOINT ping google.com
ENTRYPOINT python myapplication.py
Executable form:
ENTRYPOINT ["ping","google.com"]
ENTRYPOINT ["python","myapplication.py"]
If user specifies any arguments (commands) at the end of "docker run"
command, the specified commands override the default in CMD instruction,
But ENTRYPOINT instruction are not overwritten by the docker run
command and ENTRYPOINT instruction will run as it is.
So Docker CMD and ENTRYPOINT commands are used for same purpose,
but both has some different functionality.
VOLUME
VOLUME instruction is used to create or mount a volume to the docker
container from the docker host filesystem.
Examples:
VOLUME /data
VOLUME /appdata:/appdata
USER
USER instruction is used to set the username,group name, UID and GID for
running subsequent commands. Else root user will be used.
Examples:
USER webadmin
USER webadmin:webgroup
USER 1008
USER 1008:1200
WORKDIR
WORKDIR instruction is used to set the working directory.
Examples:
WORKDIR /app/
WORKDIR /java_dst/
ENV
ENV instruction is used to set environment variables with key and value. Lets
say, we want to set variables APP_DIR and app_version with the values /data
and 2.0 respectively. These variables will be set during the image build also
available after the container launched.
Examples:
ENV APP_DIR /data/
ENV app_version 2.0
ARG
ARG instruction is also used to set environment variables with key and value,
but this variables will set only during the image build not on the container.
Examples:
ARG TMP_NAME mycustom_image
ARG TMP_VER 2.0
So both Docker ENV and ARG commands are used to set environment
variables, but there are some differences in functionality, refer this link to
understand the differences between Docker ENV and ARG Command with
examples.
ONBUILD
ONBUILD instruction is used to specify a command that runs when the
image in the Dockerfile is used as a base image for another image.
Examples:
ONBUILD ADD . /app/data
ONBUILD RUN yum install httpd