Docker
Docker Containerization:-
While Docker is a container runtime, Kubernetes is a platform for running and managing containers from many container runtimes. Kubernetes supports numerous container runtimes including Docker, containerd, CRI-O, and any implementation of the Kubernetes CRI (Container Runtime Interface).
- What is the difference between Docker and Kubernetes?
In summary, Docker and Kubernetes are both important tools in the containerization ecosystem. Docker is used for creating and running containers, while Kubernetes is used for managing and automating the deployment, scaling, and operation of containers across clusters of hosts.
- What is the use of Docker and Kubernetes?
Docker and Kubernetes are two different technologies with different use cases. You use Docker Desktop to run, edit and manager container development. You use Kubernetes to run production grade applications at scale
- What is Docker used for?
Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime.
—————————————————————————————————————
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Practice is more important:
$ apt-get update — Update Linux System first
#Check The Kernel_Version and OS_Name:-
$ uname -a
$ uname -r
$ lsb_release -a
$ lsb_release -cs
$ cat /etc/os_release
#Installations steps:-
$ sudo apt-get install curl -y
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ ls
$ sudo chmod 777 get-docker.sh
$ sudo sh get-docker.sh
#Installations check:-
$ which docker
$ docker version / docker --version
#Add user to docker group & check user is added to group
$ sudo useradd -aG docker raghunath
$ cat /etc/group | grep docker
$ grep docker /etc/group — We’ll see like this ——> docker:x:997:raghunath
$ reboot
$ vim /etc/group
#Uninstalling Docker:-
$ sudo apt-get purge docker-ce docker-ce-cli containerd.io
$ sudo rm -rf /var/lib/docker — To remove all Containers and Images.
$
#Create containers exit and Login again:-
$ docker run -it ubuntu /bin/bash
$ exit
$ docker ps -a
$ docker start <docker_name>
$ docker attach <docker_name>
$ exit
$ cat /etc/hosts — To check container details.
$ hostname -i
$ ps -ef
$ pwd
$ ls
$ docker container ls -a — It will show you all containers running and stopped both.
$ docker ps -a
$ docker ps — It will show you only running containers.
$ docker rm <container_name> or <container_id> — To delete container
$ docker ps -l — It will show you recent created containers.
$ docker ps -n2 — Check latest 2 containers.
#Note:-
-it ——> Provides interactive shell or terminal
/bin/bash ——> “shell program” that will be installed in terminal
#Inspect the new containers:-
$ hostname
$ cat /etc/hosts
$ hostname -i
$ ps -ef
$ cd /&& pwd && ls
$ docker run -it centos
$ hostname
$ hostname -i
$ cat /etc/hosts
$ exit
$ cat /etc/os_release
#Removing docker images:-
$ docker images
$ docker rmi centos:latest — To remove images
$ docker rmi <img_id>
$ docker rmi -f <img_id> — It will delete images forcefully.
$ docker start -ai <container_name>
$ ctrl + p+q — Came out from running containers in background mode or below command.
$ ctrl + d
#Creating container with own name:-
$ docker run -it --name tomcat-server ubuntu
$ ps -ef
$ exit
$ docker ps -a
#Renaming containers:-
$ docker rename tomcat-server oracle-server
$ docker ps -a
$ docker rm <container_id>
$ docker images
#List running containers only:-
$ docker container ls — It will show you only running containers.
$ docker ps
#SSH Setup for containers:- By default containers won’t be having SSH installations.
— Create new container
$ docker run -it ubuntu /bin/ash
#Install SSH in the container.
$ apt-get update
$ apt-get install ssh -y — This will install both SSH Client and server.
$ apt-get install openssh-server -y
$ service ssh start
$ service ssh status
$ service ssh stop
$ service ssh restart
#Enable root user over ssh:-
Add below line under
“# Authentication:” in “ /etc/ssh/sshd_config”
PermitRootLogin yes
#Create an user and set-up password:-
$ useradd -m -d /home/raghu -s /bin/bash Raghu
$ passwd raghu
#Connect to the container:-
$ ssh raghu@172.17.02
#List stopped containers only:-
$ docker container ls -a -f status=exited
$ docker container ls -f status=exited
$ docker ps -a — It will show you as below: 👇
— image name from which container is created.
— id container can be identify using short UUID.
— Longer UUID or name.
— Status of the container (Up/Exited)
— Name of the containers
#List the last container which you have created (stopped/running)
$ sudo docker container ls -l
#Note two containers can’t have the same name.
#Deleting a container by giving it’s name or ID:-
$ docker rm ID/Name
#Deleting all containers at once (running/stopped):-
$ docker rm -f $(docker container ls -a -q)
$ docker rm -f $(docker ps -a -q)
#Starting a stopped container:-
$ sudo docker start <container_name>
$ sudo docker stop <container_name>
$ sudo docker restart <container_name>
#Attaching to a running container:
$ docker attach <container_name>
$ docker attach <container_id>
#Run a linux command remotely in a containers:-
$ docker exec -it tomcat-server ps -ef
#Get an independent terminal from a container remotely (from hosts)
$ docker exec -it tomcat-server /bin/bash
#Shortcut Keys:-
$ ctrl + p+q — Push a running container in background mode.
$ ctrl + d — Shortcut to ‘stop’ a containers.
#Create a container in a background mode:- Without terminal access
$ docker run -it -d ubuntu/bin/bash
#Inspecting the containers processes from host machine.
$ docker inspect <container_name>
$ docker top <container_name>
#Show last 4 containers (stopped/running):-
$ docker ps -n4
#Find more about container:-
$ docker inspect <container_name>
$ docker inspect -f ‘{{.Config.Hostname}}’ tomcat-server1
$ docker inspect -f ‘{{.NetworkSettings.Networks.bridge.IPAddress}}’ tomcat-server1
#Note:- Use --formate or -f
#List all containers names:-
$ docker inspect --format “{{.Name}}” $(docker ps -a -q) | tr -d ‘/‘
#STATS: Display usage statistics of a container:-
$ docker stats --no-stream <container_name>
$ docker stats --no-stream --all
$ docker stats --no-stream --format {{.MemUsage}} Sleepy_shannon
$ docker stats --no-stream --format {{.CPUPerc}} Sleepy_shannon
#Allocating memory for a containers:-
$ docker run -it --name tomcat-server -m 1g ubuntu /bin/bash
$ docker run -it --name tomcat-server -m 1024m ubuntu /bin/bash
#Updating memory of an existing container:-
$ docker update -m 2024m tomcat-server
#CPU Allocations:-
$ docker run -it --cpu=2 --name db-server ubuntu /bin/bash
$ docker update --cpu=2 db-server
#Push a container in sleep mode:-
$ docker pause db-server1
$ docker unpause db-server1
$ docker ps -a -q — To check all containers id only.
#Practice:-
$ docker ps -a
$ docker rm -f $(docker ps -a -q)
$ docker run -it ubuntu /bin/bash
$ exit
$ docker ps -a
$ docker start <container_name>
$ docker attach <container_name>
$ apt-get update
$ apt-get install openssh-server -y
$ service ssh status
$ service ssh start
$ service ssh stop
$ service ssh restart
$ useradd -md /home/Arun -s /bin/bash Arun
$ passwd Arun
$ id Arun
$ hostname -i
$ hostname
$ pwd
$ cd /
$ ls
$ cd
$ ssh Arun@172.17.0.2 Now install anything here.
#Create 3 containers and press ctrl + p+q
$ docker ps -a -f status=exited
$ docker ps -a -f status=running
$ docker stop <container_name>
$ docker ps -a
$ docker rm <container_name>
$ docker ps -a -q — To check all containers id only
$ docker rm $(docker ps -a -q)
$ docker ps -a
#Create 5 containers
$ docker ps -a -q
$ docker ps -a -q | xargs docker rm — Remove all
$ docker run -it ubuntu /bin/bash — Create 3 containers
$ ctrl + p+q
$ docker rm -f <container_id>
$ docker ps -a
$ docker rm -f $(docker ps -a -q) — It will remove running/stop both containers
#Create 3 containers and exit without stoping ctrl + p+q
$ docker run -it ubuntu/bin/bash
$ docker ps -a
$ docker start/stop/restart/attach
$ docker exec -it <container_name> /bin/bash — After exiting the terminal container will still be running.
$ docker exec -it <container_name> /bin/bash ls
$ docker exec -it <container_name> /bin/bash top — We’ll see the details without login into remote machine using above both.
$ which $SHELL
$ docker exec -it <container_name> cat /etc/os_release — To check OS details remotely.
$ docker exec -it <container_name> /bin/bash ps -ef
$ docker stop <container_name>
$ docker run -it -d ubuntu/bin/bash — -d means detached mode meaning is without terminal or background mode.
$ docker run -it -d --name pg-server ubuntu /bin/bash
$ docker exec -it <container_name> /bin/bash
$ docker run -it --name db-server1 ubuntu /bin/bash
$ docker inspect <container_name> — To check more about containers
$ docker ps -a
$ docker update -m 1024m <container_name> — If it is not works then check 👇
$ docker update --memory-swap 1024m -m 1024m <container_name>
#To allocate memory & RAM:-
$ docker inspect <container_name>
$ docker update --memory-swap 2g -m 2g db-server1
$ docker inspect <container_name>
$ docker run -it -m 1024m --name tomcat-server1 ubuntu /bin/bash — Create container with memory allocated.
$ docker inspect <container_name>
$ docker update --memory-swap 1024m -m 2g <container_name>
#Creating with CPU allocation:-
$ docker run -it --cpu=2 --name cart-server ubuntu /bin/bash
$ docker inspect cart-server
$ docker update --cpus=1 cart-server
$ docker inspect cart-server
#To check CPU and RAM utilisation:-
$ docker ps -a
$ docker stats cart-server
$ docker stats --no-stream --all
$ docker stats --no-stream cart-server
$ docker stats --no-stream --format {{.CPUPerc}} cart-server
$ docker stats --no-stream --format {{.MemUsage}} cart-server
#SSH complete process step-by-step:-
$ docker run -it --name Jenkins-server ubuntu /bin/bash
$ hostname -i
$ useradd -md /home/Arun -s /bin/bash Arun
$ passwd Arun
$ exit
$ docker start/stop/restart Jenkins-server
$ docker attach Jenkins-server
$ docker exec -it Jenkins-server /bin/bash
$ apt-get update
$ apt-get install sudo && apt-get install vim -y
$ which vim
$ which sudo
$ vim /etc/sudoers — To add user here
$ apt-get install openssh-server -y
$ service ssh start/status
#Come to host machine:-
$ ssh arun@172.17.0.2 <password> — Now we can do anything here over ssh remotely.
#Project-1
#Building our own images ‘with 2 ways’ :-
1. Docker commit
2. Docker build
#Creating docker image using ‘docker commit’ command.
Project-1
Goal:- Create the docker image to ship the application code along with nginx configurations.
$ docker run -it --name nginx-container ubuntu /bin/bash
$ apt-get update
$ apt-get install nginx -y
$ apt-get install vim -y
$ cd /var/www/html
$ pwd
$ ls
$ touch index.html
#Deploy an applications code into given ‘/var/www/html’ this is deployment path for nginx.
$ rm -rf index.html-debian.html
E.g. deploy below index.html as a code:
<html>
<body>
<h1 style= “color:red;”> IT Infosoft </h1>
</body>
</html>
$ vim index.html — Paste the above code here.
#Create docker image from the container or convert docker container as docker image:
$ docker commit nginx-container iamraghu/nginx-img
$ docker images ls
$ docker images
#Push the image to docker hub after creating an account.
$ docker login (Id, pass)
$ docker push iamraghu/nginx-img
$ docker rmi <image_name>
#Create container from that image which is created earlier using ‘docker commit’ command.
$ docker run -it iamraghu/nginx-img /bin/bash
$ sudo service nginx start
$ sudo service nginx stop
$ sudo service nginx restart
$ sudo service nginx status
$ hostname -i
#Launch the applications to test if application is configured along with dependencies.
$ 172.17.0.2:80
$ sudo apt-get purge nginx nginx-common
#Project-2
#Creating docker image using ‘docker build’ command.
$ mkdir Project
$ cd Project
$ touch Dockerfile
$ touch index.html
— The ‘Project’ directory is called ‘context’ or ‘build context’ it contains the code, file or other data that you want to include in the image.
#Write Dockerfile:-
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html
RUN service nginx start or
ENTRYPOINT service nginx start && bash
#Index.html
<html>
<body style=“background-colour:powderblue;”>
<h1 style= “color:red;”> IT Infosoft </h1>
</body>
</html>
$ docker images
$ cd Project
$ docker rmi <img_name>
#Building docker image:-
$ cd Project
$ docker build -t iamraghu/nginx-img .
$ docker images
$ git commit --allow-empty -n -m “Initial commit” — If you are getting errors while building images then use it.
$
#Note :- Building the image if ‘Dockerfile’ has different name then Use below👇
“-f <your_Dockerfile_name>” option
E.g:-
$ docker build -f MyDockerfile -t =“iamraghu/nginx-img” .
#Listig Docker images:-
$ docker images ls
$ docker images
#Pushing custom image to docker repository:-
$ docker login
$ docker push iamraghu/nginx-img
#Testing image:- Remove the local image so that it will be downloaded from Docker Hub.
$ docker rmi iamraghu/nginx-img or
$ docker image rm iamraghu/nginx-img
#Creating a new container from our image:-
$ docker run -it --name nginx-container iamraghu/nginx-img /bin/bash
$ service nginx start
$ service nginx status
$ service nginx stop
$ service nginx restart
$ service nginx status
#Verify if nginx is running from the container:-
$ hostname -i
#Once login into container which is created from your own image.
$ docker images
$ docker start -ai <container_name>
$ cd /var/www/html
$ ls
$ pwd
$ cat index.html
$ cat /etc/os-release
$ ls
$ service nginx start/status
$ hostname -i
#Go to browser again for testing purpose and paste 172.17.0.2:80 and check applications
$ exit
$ clear
#User Image Syntax:-
$ iamraghu/nginx-img —— User_name / Image_name.
#Official Image Syntax:-
$ ubuntu
#Specify Image via tags
$ ubuntu:16.04
— ubuntu is image name
— 16.04 is called tag
#Pulling the images from DockerHub:-
$ docker pull tomcat
#Searching docker images in docker hub:-
$ docker search puppet
$ docker search jJenkins
#Deleting an Images:-
$ docker rmi $(docker images -q)
#Checking the history of docker image:-
$ docker history <Image-id>
#Build image without using build cache:-
$ docker build --no-cache -t =“user_name/image_name” .
#Volumes:- [VOLUMES]:-
$ docker volume ls
$ docker volume create <vol-name>
$ docker inspect <vol-name> — To Check the mount point directory
#Mount Volume to a new container:-
$ docker run -it -v <vol-name>:/<vol-name> ubuntu /bin/bash
$ docker run -it -v <vol-name>:/<vol-name>:ro ubuntu /bin/bash
$ docker volume rm <vol-name>
$ docker volume prune
$ docker run -it -v /home/raghunath/Distros:/Distors ubuntu /bin/bash
$ docker ps -a --filter volume=<vol-name>
$ docker volume ls — List all volumes available in host machine.
$ docker volume create <vol-name> — Create new volume.
$ docker inspect <vol-name> — Check Mount point directory.
#Mount volume to a new Container:-
$ docker run -it -v <vol-name>:/<vol-name> ubuntu /bin/bash
#Create ‘Read-only’ Volumes:-
$ docker run -it -v <vol-name>:/<vol-name>:ro ubuntu /bin/bash
$ docker volume rm <vol-name> — To remove volume
$ docker volume prune — Remove all unused volume.
#Creating Volume with existing directory in the host machines:-
$ docker run -it -v Distros:/Distros
$ docker run -it -v /home/raghunath/Distros:/Distros ubuntu /bin/bash
#List down all containers which are using a particular volumes:-
$ docker ps -a --filter volume=<vol-name>
#Volumes Examples:-
$ docker volume ls
$ docker volume rm <vol-name>
$ docker volume create user-data
$ docker volume ls
$ docker inspect user-data
$ sudo su
$ cd /var/lib/docker
$ ls
$ pwd
$ ls
$ cd user-data
$ ls
$ cd _data/
$ pwd
$ cd ~
$ docker volume rm user-data
$ docker volume create user-data
$ docker inspect user-data
$ sudo su
$ cd /var/lib/docker
$ ls
$ cd volumes/
$ cd user-data
$ cd _data
$ pwd
$ docker ps -a
$ docker rm <container_name>
$ docker run -it --name tomcat-server -v user-data:/user-data ubuntu /bin/bash
$ ls
$ cd user-data
$ pwd
$ touch user.info
$ ls
$ touch items.db
$ ls
— Now go to the host machine inside /var/lib/docker/volumes/user-data and see the files. Make sure you are in root.
$ exit
$ docker run -it --name tomcat-server2 -v user-data:/user-data:ro ubuntu /bin/bash
$ cd user-data/
$ ls
$ pwd
$ touch cart.info — You can’t able to create any files because its read only volume.
$ exit
$ docker volume create cart-data
$ docker volume ls
$ sudo su
$ cd /var/lib/docker/volumes/cart-data/_data/
$ ls
$ touch cart.info
$ ls
$ exit
$ docker run -it --name tomcat-server3 -v user-data:/user-data:ro -v cart-data:/cart-data ubuntu /bin/bash
$ ls
$ cd cart-data
$ ls
$ exit
$ docker volume ls
$ docker volume create session-data
$ docker volume create region-data
$ docker volume ls
$ docker volume prune -y
$ docker volume ls
$ df -kh .
$ docker ps -a --filter volume=cart-data
$ docker rm tomcat-server3
$ docker volume rm cart-data
$ cd Distros
$ ls
$ pwd
$ docker run -it --name db-server -v /home/raghunath/Distors:/Distros ubuntu /bin/bash
$ ls
$ cd Distros
$ ls
$ cd source
$ ls
$ cd source
$ ls
$ git clone https://github.com/rg000149/xxxxxxxxxx.git
$ cd xxxxxxxxxx
$ ls
$ mvn install
$ ls target/
$ docker build -t iamraghu/xxxxxxxxxx-img .
$ docker images
$ docker login
$ docker push iamraghu/xxxxxxxxxx-img
$ docker ps -a -q
$ docker rm $(docker ps -a -q)
$ docker ps -a
$ docker run -it --name tomcat-server-qa -h tomcat-server-qa iamraghu/xxxxxxxxxx-img /bin/bash
$ hostname -i
— Go to browser and paste ip address along with artifact-id & run it.
——————————————————————————————————-
#Dockerfile
#Create-env.sh
$ vi create-env.sh
#!/bin/bash
cont_count=$1
echo “creating $cont_count containers. .”
sleep 2;
for i in ‘seq $cont_count’
do
echo “ = = = = = = = = = = = = “
echo “ Creating tomcat-server$i container. .”
sleep 1
docker run -it -d --name tomcat-server$i iamraghu/raghu-img
echo “ tomcat-server$i container has been created!”
echo “ = = = = = = = = = = = = “
done
docker inspect --format {{.NetworkSettings.Networks.bridge.IPAddress}} ‘docker ps -q’ >IPs.txt
#vim Dockerfile
FROM ubuntu:22.04
MAINTAINER “info@raghu.com
RUN apt-get update
RUN apt-get install openjdk-8-jdk -y
ENV JAVA_HOME /usr
ADD apache-tomcat-8.5.38.tar.gz /root
COPY target/amazon.war /root/apache-tomcat-8.5.38/webapps
ENTRYPOINT /root/apache-tomcat-8.5.38/bin/startup.sh && bash
#Dockerfile
FROM ubuntu:22.04
MAINTAINER “info@raghu.com
RUN apt-get update
RUN apt-get install openjdk-8-jdk -y
COPY apache-tomcat-8.5.38.tar.gz /root
RUN tar -zxvf /root/apache-tomcat-8.5.38.tar.gz
COPY target/amazon.war /root/apache-tomcat-8.5.38/webapps
RUN rm -rf /root/apache-tomcat-8.5.38.tar.gz
RUN /root/apache-tomcat-8.5.38/bin/startup.sh
$ docker build -t iamraghu/sears-img .
$ docker images
$ docker run -it --name jenkin-server iamraghu/sears-img /bin/bash
$ cat /etc/os-release
$ java -version
$ cd root/
$ cd apache-tomcat-8.5.38
$ cd webapps
$ ls
$ ps -ef | grep tomcat
$ ls
$ cd apache-tomcat-8.5.38/bin
$ ./startup.sh
$ hostname -i
$ exit
— Go to browser and run the applications
$ rm -rf amazon
$ ls
$ git clone https://github.com/rg000149/amazon.git
$ cd amazon
$ ls
$ mvn install
$ ls target/
$ docker build -t iamraghu/sears-img2 .
$ docker login
$ docker push iamraghu/sears-img2
$ docker ps -a
$ docker rm $(docker ps -a -q)
$ docker run -it --name tomcat-server1 iamraghu/sears-img2 /bin/bash
$ hostname -i
— ip:80/amazon/
$ ls
$ vim Dockerfile
FROM ubuntu:16.04
MAINTAINER “info@sears.com”
RUN apt-get update
RUN apt-get install openjdk-8-jdk -y
ENV JAVA_HOME /usr
ADD apache-tomcat-8.5.38.tar.gz /root
COPY target/sears.war /root/apache-tomcat-8.5.38/webapps
ENTRYPOINT /root/apache-tomcat-8.5.38/bin/startup.sh && bash
$ ls && pwd
$ cat Dockerfile
$ exit
$ ./create-env.sh
$ docker ps
$ docker inspect tomcat-server1
$ docker inspect --format {{.Network.Settings.Network.bridge.IPAddress}} tomcat-server1
$ docker ps
$ docker inspect --format {{.Network.Settings.Network.bridge.IPAddress}} $(docker ps -a -q)
— Go to browser and run.
$ vim create-env.sh — write script for container creations:-
$ cd source
$ rm -rf amazon
$ ls
$ git clone htttps://github.com/rg000149/amazon.git
$ docker rm $(docker ps -a -q)
$ cd amazon
$ ls
$ mvn install
$ docker built -t iamraghu/final-img .
$ docker login
$ docker push iamraghu/final-img
$ ./create-env.sh 5 — Enter the number of containers which you want to create from that scripts
$ cat IPs.txt — Go to browser and check
$ ls
$ docker inspect iamraghu/final-img
$ docker run -it --name Jenkins iamraghu/final-img /bin/bash
$ hostname -i
$ env
$$———————————————————————————$$
Thanks 🙏
Docker - The Container Virtualisation Tool
==================================================
###############
#
Diff between..
- Physical server
- Virtual machine
- Docker container
VM, Docker, usage in DevOps.
#
what is docker? why docker?
#
Supported Platforms -
- Docker is supported on
- Linux platforms
Ubuntu, RHEL, CentOs..etc.
- Windows
- OS X
- Cloud Platforms
Amazon EC2
Rackspace Cloud
Google compute Engine..etc.
Azure
Note:
Linux containers can be created on Windows and OS X.
How?- Windows & Mac Docker installers contain a tiny Linux virtual machine.
So, Docker creates linux container on top of this tiny Linux VM.
Requirements:
- 64-bit architecture
- Linux 3.8 or later Kernel versions
#
Requirements Check:
- Check Kernel version
$ uname -a
$ uname -r
- Check OS name:
$ lsb_release -a / -cs
$ cat /etc/os-release
Installation Steps:
=====================
# To install the Docker, Run below commands
Reference: https://docs.docker.com/engine/install/ubuntu
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
#
Installation Check
sudo docker --version
#
If you would like to use Docker as a non-root user, you should add your user to the “docker” group with below command.
sudo usermod -aG docker <your-user>
check if the user is added to the group
$ cat /etc/group | grep docker
docker:x:998:Raghunath
Note - restart the machine once the user is added to the group.
#
uninstall docker:
$ sudo apt-get purge docker-ce docker-ce-cli containerd.io
$ sudo rm -rf /var/lib/docker (Removes all containers and images)
#
Managing docker containers
===============================
# Create a new container using below command
$ docker run -it ubuntu /bin/bash
------------------
- "docker run" command provides all lanuching capabilities for docker to create a container.
- we use `docker run` to create new containers.
-i : opens STDIN from the container
-t : tells docker to assign a terminal to the container.
-it : provides interactive shell
ubuntu : Is an image and also called as "stock image" or "base image".
This image will be downloaded from Docker hub when we run 'docker run'
command first time.
/bin/bash: 'shell program' that will be installed in the terminal.
# Inspect the new container.. Let's believe that it's separate machine
1.
hostname
2.
cat /etc/hosts
3. hostname -i
5. ps -ef
6. cd / && pwd && ls
##########################
# SSH setup for containers
By default, containers won't be having SSH installation. But, SSH almost mandatory in order to connect to a remote machine of if remote machine wants to connect to the container. Let's setup SSH in the container.
- Create a new container
$ docker run -it ubuntu /bin/bash [run ssh command. It's missing!]
- Install SSH in the container
$ apt-get update
$ apt-get install ssh [This installs both SSH client and Server]
- Start the SSH server
$ service ssh start (status/stop/restart)
- Create an user and set up password
$ useradd -m -d /home/raghu -s /bin/bash raghu
$ passwd raghu
- Connect to the container using below command from the host machine.
$ ssh raghu@172.17.0.3
#
Shutdown a container
"exit" to stop the container
#
Login to a stopped container
$ docker start
$ docker attach
$ docker start -ai mystifying_tu
# List all containers(stopped and running)
$ docker cotainer ls -a
$ docker ps -a
# List given no. of containers
$ docker ps -n1
# List running containers only
$ sudo docker cotainer ls
$ docker ps
# List Stopped containers only
$ docker container ls -f status=exited (Where Status can be exited/running)
# "docker ps -a" command output shows
- Image name from which container is created
- ID - container can be identified using short UUID, longer UUID Or name.
- Status of the container (Up / Exited)
- Name of the container
# show the last container which you have created (stopped/running)
sudo docker container ls -l
# Naming the container
docker run --name raghu-it ubuntu /bin/bash
Note: Two containers can't have the same name.
# Rename a container
$ docker rename db-server3 db-server-name3
# Deleting a container by giving it's name or ID
$ docker rm ID/name
# Delete all (running/stoped) containers at once
$ docker rm -f $(docker container ls -a -q)
$ docker rm -f $(docker ps -a -q)
# Delete running containers only
$ docker rm -f $(docker container ls -q)
$ docker rm -f $(docker ps -q)
# list stopped containers only
$ docker container ls -a -f status=exited
# Starting a stopped container
sudo docker start raghu
sudo docker stop <container-name>
sudo docker restart raghu
# Attaching to a running container
docker attach raghu
docker attach b1b1c8dc4545
# Run a linux command remotely in a container
$ docker exec -it tomcat-server ps -ef
#
Get an independent terminal from a container remotely (from Host)
$ docker exec -it tomcat-server /bin/bash
# Shortcut Keys
Ctrl + p + q - push a running container in background mode.
Ctrl + d - short cut to 'stop' a container.
# Create a container in a background mode ( without termial access )
$ docker run -it -d ubuntu /bin/bash
STATS:
==========
# Display usage statistics of a container
$ docker stats <container-name>
$ docker stats --no-stream <container-name>
$ docker stats --no-stream --all
$ docker stats --no-stream --format {{.MemUsage}} sleepy_shannon
$ docker stats --no-stream --format {{.CPUPerc}} sleepy_shannon
#
Allocating memory for a container (below command allocates 1 GB RAM)
$ docker run -it --name tomcat-server -m 1g ubuntu /bin/bash
$ docker run -it --name tomcat-server -m 512m ubuntu /bin/bash
#
Updating memory of an existing container
$ docker update -m 2024m tomcat-server
# CPU Allocation
$ docker run -it --cpus="2" --name jenkins-server ubuntu /bin/bash
$ docker update --cpus="2" jenkins-server
# Docker Images
=================
Agenda:
- Understand docker Images and application
- Advantages of Docker Images
- Create docker Image for your application
- Share/publish your Image
- Examine Docker repositories that hold images
- Docker images are the building blocks for creating container
- From images, we launch containers.
# Advantages of Images in Build and Deployments OR DevOps world!
a. Works In my machine problem.
b. Developers can quickly setup local development environments as we can include all dependencies in the image and create containers.
c. Is there an Issue? don't spend time to troubleshoot it. Just throw the machine which has the issue away and create new instantly.
d. Auto scale your environment very easilly.
e. No need to live with complex, redundant configurations. You can create disposable environments.
f. You can leverage/utilizes local machines's computing power when you need to test your code on multiple machines, instead of waiting for DevOps team to supply or wasting extra computing power. you already have 500GB, 16GB RAM right? are you utilising it? NO! then why again you need extra hardware?
g. You can create new environments within few minutes (ex: create new performance testing environment within few minutes before the release)
# Listing docker images
- $ docker image ls
- Images live in '/var/lib/docker/image/overlay2/imagedb/content/sha256'
- Containers live in '/var/lib/docker/containers'
# Building our own Image
We have 2 Ways to create docker image:
1. docker "commit"
2. docker "build" cmd & Dockerfile
# Creating docker image using "docker commit" command
===========================================================
Goal: Create the docker image to ship the application code along with nginx configurations.
- Create container
$ docker run -it --name nginx-container ubuntu /bin/bash
- Install nginx manually
$ apt-get update
$ apt-get install -y nginx
- Deploy some application code into '/var/www/html' (this is deployment path for nginx)
ex: deploy below index.html as a code
=======
<html>
<body>
<h1 style="color:red;">IT Infosoft Learning Solutions</h1>
</body>
</html>
=======
- Create docker image from the container (OR)
- Convert docker container as docker image..
$ docker commit nginx-container nageshvkn/nginx-img
Syntax: $ docker commit <container-name> <image-name>
- Check if image has been created
$ docker image ls
- Push the newly created image to docker hub
- Create an account in 'https://hub.docker.com/'.
$ docker login
$ docker push rg000149/nginx-img
Note: Now you have succussfully containerized your application and published the iamge to DockerHub. Customers can spin millions of new containers using the above docker image.
Note: To verify your image as an user, create a container as shown below. Remove existing image that you have created so that you can abserve image download from Docker hub clearly. (to remove the image.. $ docker rmi nageshvkn/nginx-img)
$ docker run -it rg000149/nginx-img /bin/bash
- Launch the application to test if application is configured along with dependencies.
http://172.17.0.2:80
Note:
start/stop/restart nginx server:
===========\===========\===========
$ sudo service nginx start
$ sudo service nginx stop
$ sudo service nginx restart
$ sudo service nginx status
Note:
uninstall nginx using below comamnd
$ sudo apt-get purge nginx nginx-common
Container status - explain create, pause
=============
# Creating docker image using "docker build" command
=================
- mkdir sears
- cd sears
- touch Dockerfile
--> 'sears' directory is called "context" or "build context".
It contains the code, files or other data that you want to include in the
image.
- Write Dokckerfile:
FROM ubuntu:16.04
MAINTAINER "info@sears.com"
RUN apt-get update
RUN apt-get install -y nginx
COPY index.html /var/www/html
ENTRYPOINT service nginx start && bash
index.html:
=======
<html>
<body style="background-color:powderblue;">
<h1 style="color:red;">Sears Online E-Commerce Portal</h1>
</body>
</html>
# Building docker image:
$ cd sears
$ docker build -t "rg000149/nginx-img" .
Note: Building the image if 'Dockerfile' has different name.
Use "-f <YourDockerfileName>" option.
Example: $ docker build -f MyDockerfile -t="rg000149/nginx-img" .
# Listing docker image
$ docker image ls
# Create an account in docker hub
# Pushing custom images to docker repository
$ docker login
$ docker push rg000149/nginx-image
#
Testing Image
1. Remove the local image so that it will be downloaded from Docker Hub.
$ docker rmi rg000149/nginx-image (OR)
$ docker image rm rg000149/nginx-image
2. Creating a new container from our image
$ docker run -it --name nginx-container rg000149/nginx-img /bin/bash
Note: start the nginx server manually as it's not fixed yet. It will be fixed in the next topic.
3. Verify if nginx is running from the container.
$ http://172.17.0.2:80
#
User Images Syntax:
rg000149/nginx-img (username/imagename)
Official Images Syntax:
ubuntu
# Specifying Image via tags
- ubuntu:16.04
ubuntu- is image name
16.04 - is called tag
# Deleting an Image
- docker rmi raghu/nginx
# Deleting all Images
- docker rmi $(docker images -q)
# Container creation process - Deep dive
How contaner is created:
Writable Layer
-
Sears application
Apache image
nginx image
-
Ubuntu Base Image(rootfs)
-
Bootfs:
cgroups, namespace, lxc, devicemapper/aufs/overlay..etc.
Kernel
Volumes:
===============
# List all volumes available in host machine
$ docker volume ls
# Create a new Named Volume
$ docker volume create deployment_code
# Check Mount point directory
$ docker inspect deployment_code
# Mount Volume(deployment_code) to a new container
$ docker run -it -v deployment_code:/deployment_code ubuntu:16.04 /bin/bash
# Create 'Read-only' Volumes
$ docker run -it -v deployment_code:/deployment_code:ro ubuntu:16.04 /bin/bash
# Removing a Volume
$ docker volume rm deployment_code
# Remove all unused Volumes
$ docker volume prune
# Note:
Creating host Volume with your own directory in host machine (OR)
Creating host Volume with existing directory in the host machine
$ docker run -it -v /home/raghu/Distros:/Distros ubuntu /bin/bash
# bind mount
$ docker run -it --mount type=bind,source=/home/raghu/Distros,target=/Distros ubuntu /bin/bash
# List down all containers which are using a particular volume
$ docker ps -a --filter volume=deployment_code
Real-time Sears application
============================
Agenda:
How do you containerize or dockerize your application?
Can you explain how you have implememnted Docker for your application?
1. Clone the source code from Git or any other V.C.S
$ git clone https://github.com/rg000149/amazon.git
2. Build the code using your favourate build tool Maven/ANT
$ mvn install
3. Create docker image for the application(it-academy2) with
war file, tomcat,jdk...etc using below Dockerfile.
Dockerfile:
-------------
FROM ubuntu:16.04
MAINTAINER "info@infosoft.com"
RUN apt-get update
RUN apt-get install -y openjdk-8-jdk
ENV JAVA_HOME /usr
ADD apache-tomcat-8.5.38.tar.gz /root
COPY target/sears.war /root/apache-tomcat-8.5.38/webapps
ENTRYPOINT /root/apache-tomcat-8.5.38/bin/startup.sh && bash
4. Build the Image using below command
$ docker build -t "rg000149/sears-img" .
4A. Push the image to docker hub.
$ docker push rg000149/sears-img
5. Run below shell script to create an environment with give no. containers
$ ./create-env.sh 10
6. Observer all containers created using above script ($ docker ps)
7. Launch the sears application from all containers.
$ http://IP:8080/itinfosoft
#
Docker and Jenkins Integration
1. Create a new Free style project in Jenkins
2. Configure Git, Maven, Docker Image creation & Environment creation using below.
- Configure Git URL under "Source code Management"
- Provide Maven's 'install' command under build section
- Open 'Execute shell' and type below commands for creating Image and Environment
- docker build -t "rg000149/sears-img" . (Note: don't forget "." at the end)
- ./create-env.sh 10
----------
1. How to download image from private repo
2.
---------------------------------------Script to create multiple containers-------------------@@
[create-env.sh ]
#!/bin/bash
cont_count=$1
echo "creating $cont_count containers.."
sleep 2;
for i in `seq $cont_count`
do
echo "=============================="
echo "Creating tomcat-server$i container.."
sleep 1
docker run -it -d --name tomcat-server$i rg000149/sears-img
echo "tomcat-server$i container has been created!"
echo "=============================="
done
docker inspect --format {{.NetworkSettings.Networks.bridge.IPAddress}} `docker ps -q` > IPs.txt
————————————————————————————————————-
Thanks 🙏
ConversionConversion EmoticonEmoticon