Docker
Docker
1. Provisioning
It’s a method to providing VMs with some application installation. It's a requirement of
field company.
How we can optimize the server provisioning cost?
In present we have one VM for one application.
1. Library conflict
2. Env conflict
3. Soft dependency
Provisioning Method:
2. Virtualization
3. OS Virtualization
Docker Architecture
Docker Page 1
Redhat also provide their customize docker solution.
1. Atomic host>>rhel7
2. podman>>rhel7.4 Later
2. Docker Client
Client machine from where we can provision containers. This is just like your client workstation and
generally its not recommended to take docker client.
3. Docker Images
Its light weight image for particular application. To spin the container you should have at least
available. It has minimal configuration to deploy any container.
1. Community based
There have some communities available in market and they are providing free of cost images.
For example: docker hub
Free available: CE
2. Custom Image
We can configure our offline Docker registry and we can import some ready to use images.
4. Docker Registry
5. Docker Container
Docker Page 2
Lecture-2
Monday, October 5, 2020
13:52
Docker CE:
Step2:
# docker images
# docker ps
# docker search mysql
# docker pull mysql:latest
# docker pull mysql:5.6
# docker images
# docker pull nginx:latest
# ls -ld /var/lib/docker
# docker pull centos:7
# docker images
# docker history b5b4d78bc90c [to check the history of docker image]
1. With Service
2. Without service
Docker Page 3
/]# exit
#docker ps [you will not see any container listed here]
#docker ps -a [to list all containers]
# docker ps
# docker top test [to check the all process of specific container]
# docker start test
# docker attach test [to access shell of the container]
# docker exec -it test /bin/bash [this will start the new process]
# docker ps [on docker host]
# docker images
# docker run -d --name=webserver nginx:latest [we can also use image ID]
# docker top webserver
# docker exec -it webserver /bin/bash
# docker inspect webserver | grep -i ipaddress
# curl 172.17.0.2
Note: Here centos is raw image because no service is installed inside this image.
nginx and mysql are service images.
Docker Page 4
[root@ip-172-31-13-80 ~]# docker start con1
con1
[root@ip-172-31-13-80 ~]# docker attach con1
[root@7960a29d6eaf /]#
Note: Whenever we give restart to docker server on docker hosts all container will go shutdown if we will not set this option .
Container will come up automatically.
Docker Page 5
Container will come up automatically.
#docker start $(docker ps -q -a) [To start all container in one go]
#docker images
#docker run -d -p 8080:80 --name webserver1 nginx:latest
#docker ps
#netstat -tunlp | grep -w 8080
Note: Now try to open public IP of EC2 server in browser. <public ip:8080>
#docker ps
#docker run -d -p 8083:80 -p 8084:80 --name=webserver4 nginx:latest
#docker ps
#docker stop $(docker ps -q -a)
#docker rm $(docker ps -q -a)
#docker run -d -P --name=webserver5 nginx:latest [To allocate automatic port]
#docker image
Note: Launch new container using the same image and check run.sh file inside new container, you will not get file inside the
Docker Page 6
Note: Launch new container using the same image and check run.sh file inside new container, you will not get file inside the
container because image is totally isolate.
Note: New image has dependency on base image that's why can't delete.
#docker info
#ls /var/lib/docker/overlay2
#docker pull centos:7
#ls /var/lib/docker/overlay2
#docker images inspect docker.io/centos:7 | less
#docker run -it --name=test centos:7
/]#ls
Note: Create one file on image overlay path and check the file inside container.
/]#touch xyz
"OverlayFS"
#df -TH /
Note: Inside overlay2 we have two layer path one for image and one for container.
Create one file inside container and check on container layer path inside diff folder and file should be available. But will not
available inside the image layer path.
Docker Page 7
Lecture-3
Monday, October 5, 2020
13:52
###Docker Volume###
#docker rm -f test
#mkdir /volume
#getenforce
#setenforce 0
#docker run -d -v /volume<docker host path>:/var/lib/mysql<container path> --name=db -e MYSQL_ROOT_PASSWORD=redhat
mysql:5.6
#docker ps
#docker exec -it db /bin/bash
#mysql -u root -p
:redhat
>create database test;
>exit;
#ls /volume
#docker rm -f db
#ls /volume
#docker run -d -v /volume:/var/lib/mysql --name=db -e MYSQL_ROOT_PASSWORD=redhat mysql:5.6
#docker exec -it db /bin/bash
/]#mysql -u root -p
:redhat
>show databases;
>exit;
#setenforce 1
#getenforce
#mkdir /data
#ls -ld /data
#docker run -it --name=con1 centos:7
/]#ls -ldZ /opt
/]#exit
#docker rm -f con1
#ls -ldZ /data
#chcon -Rt container_share_t /data
#docker run -it --name=con1 -v /data:/opt centos:7
/]#touch /opt/abc
Docker Page 8
/]#touch /opt/abc
/]#exit
#ls /data [context should be match]
#ls /var/lib/docker/volumes
#docker rm -f db
#docker volume ls
#docker system df
#docker volume create vol1
#docker volume ls
#lsblk
#setenforce 0
#docker run -it -v /vol1:/opt --name=testcon centos:7
/]#cd /opt
opt]#dd if=/dev/zero of=abc1 bs=1m count=4000
opt]#exit
#docker volume ls
#docker system df
#docker system prune
[y]
Docker Page 9
#docker stop $(docker ps -q -a)
and
rm
Docker Page 10
Lecture-4
Monday, October 5, 2020
13:53
1. Manual
2. Docker File
#docker images
Note: On google search "nginx docker image" and copy the GIT URL of the image and download it over docker host.
#mkdir test
#cd test
#echo helloword > run.sh
#vim DockerFile
FROM centos:7
MAINTAINER [email protected] "it’s a practice image"
RUN yum install httpd -y
ADD https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm /tmp
COPY run.sh /tmp
Docker Page 11
:wq!
#mkdir src
#echo website > src/index.html
#vim DockerFile
FROM centos:7
ENV a 10
RUN echo $a > /var/tmp/abc
ENTRYPOINT "abc.sh"
MAINTAINER [email protected] "it’s a practice image"
RUN yum install httpd -y && echo "Helloworld" > /tmp/report \
&& date && cal \
WORKDIR /var/www/html
COPY ./src/ .
RUN 'sed -i "s/Listen 8080/Listen 8080/g" /etc/httpd/conf/httpd.conf' \
&& chown -R apache:root /var/log/httpd /var/run/httpd \
&& chmod -R 777 /var/log/httpd /var/run/httpd
EXPOSE 8080
USER apache
ADD https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm /tmp
COPY run.sh /tmp
CMD ["/usr/sbin/httpd", "-D", "FORGROUND"] [we can not provision container with -d option without this parameter
you need to -it option]
VOLUME /volume [container mounting point]
:wq!
Docker Page 12
#docker run -d -p --name=webserver nginx
#docker ps
Note: You will get default port 80 which is already defined inside the docker file.
Docker Page 13
Lecture-5
Monday, October 19, 2020
23:05
###Docker Network###
# docker network ls
Note: But all container communicate via bridge within docker host.
# ifconfig docker0
# docker run -it --name=con1 centos:7
/]# ctrl+pq
# docker inspect con1 | grep -i ipaddress
# ping 172.17.0.2
# docker run -it --name=con2 cento:7
/]# ping 172.17.0.2
/]# ping google.com
Note: By default containers uses NAT feature to reach internet via docker host network.
Docker Page 14
/]# ctrl+pq
# docker network inspect bridge
1. Bridge network: Container used either default or custom bridge for the communication.
2. Host network: Container doesn't create network namespace. It directly used docker host adapter.
Docker Page 15
Lecture-6
Monday, November 9, 2020
8:04 PM
#####Docker Compose#####
version: '3'
services:
web:
image: nginx
ports:
- 8080:80
:wq!
# docker-compose up -d
# docker ps
# vim docker-compose.yaml
version: '3'
services:
web:
image: nginx
ports:
- 8080:80
apache:
image:nginx
ports:
- 8081:80
:wq!
# docker-compose up -d
# docker ps
# docker-compose ps
# docker-compose stop [Need to execute command from the compose file path]
# docker-compose rm
# docker-compose ls
#cd ; mkdir wordpress
# cd wordpress
# vim docker-compose.yaml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
Docker Page 16
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
:wq!
# docker-compose up -d
# docker network ls
# docker-compose ls
# docker-compose volume ls
# docker-compose stop
# docker-compose rm
# docker inspect network wordpress_default | grep -i subnet
# docker-compose kill [Shutdown and kill]
# docker network ls
# docker network rm wordpress_default
# docker-compose down [Everything will be deleted]
# vim docker-compose.yaml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- abc
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
Docker Page 17
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- abc
volumes:
db_data: {}
xyz: {}
networks:
abc:
driver: bridge
:wq!
# docker-compose up -d
# docker network ls
# vim Docker-File
FROM centos:latest
RUN yum install httpd -y
RUN sed -i "s/Listen 80/Listen 8080/g" /etc/httpd/conf/httpd.conf
COPY src/ /var/www/html
RUN chown apache:apache /var/run/httpd /var/log/httpd
RUN chmod -R 777 /var/run/httpd /var/log/httpd
EXPOSE 8080
USER apache
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
:wq!
# mkdir src
# echo helloworld > src/index.html
# ls
# vim docker-compose.yaml
version: '3'
services:
webserver:
build: .
port:
- 8089:80
:wq!
# docker-compose up -d
# docker ps
Note: If you have change the website content then this option is useful.
# curl 127.0.0.1:8089
Docker Page 18
Docker Page 19
Lecture-7
Monday, November 9, 2020
9:51 PM
##### Docker-Registry#####
Registry Servers.
Note: Without password we can pull the image but can not push.
# docker images
# docker tag <image-ID> docker.io/ram123/jboss:latest
# docker images
# docker push ram123/jboss:latest
# docker login
Username: ram123
Password:
Docker Page 20
There have two ways to setup offline docker registry server.
1. Complete VM reserve
2. Container based registry server
# hostname registry-server
# bash
# yum install docker-distribution -y
# systemctl start docker-distribution
# systemctl enable docker-distribution
# netstat -tunlp | grep -w 5000
# ls -ld /var/lib/registry
# ifconfig eth0
On Docker host
Note: You will get the error because of unsecure registry which is running on by default http based.
# vim /etc/docker/daemon.json
{"insecure-registry": ["<ip-registry-server>:5000"]}
:wq!
On Registry-Server
# ls /var/lib/registry/docker/registry/v2/repositories
# ls
On Docker Host
On Registry-Server
On Docker Host
On Registry Server
Note: This is non-persistent storage if you container will loss, images will also delete.
Docker Page 22