How to Install and Configure Docker
Docker/Podman is an open source containerization platform to create, deploy and run applications. Containers are lightweight counterpart of virtual machines.
Configuring Container Host
[root@original ~]# hostnamectl set-hostname containerhost.srg.com
[root@original ~]# exec bash
[root@containerhost ~]# vim /etc/hosts
---------------------------------
192.168.13.150 containerhost.srg.com
---------------------------------
[root@containerhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
---------------------------------
BOOTPROTO=static
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.13.150
NETMASK=255.255.255.0
GATEWAY=192.168.13.2
DNS1=192.168.13.2
---------------------------------
[root@containerhost ~]# systemctl restart network
[root@containerhost ~]# systemctl restart network
[root@containerhost ~]# hostname
containerhost.srg.com
[root@containerhost ~]# hostname -I
192.168.13.150 192.168.122.1
[root@containerhost ~]# ping google.com
- To make system up to date
[root@containerhost ~]# yum -y update
2. Installing container engine (podman)
[root@containerhost ~]# yum -y install podman
3. Searching a container image in Docker hub: podman search <image name>
[root@containerhost ~]# ping hub.docker.com
[root@containerhost ~]# podman search httpd
4. Download a container image: podman image pull <image name>
[root@containerhost ~]# podman image pull docker.io/library/httpd
[root@containerhost ~]# podman image ls
5. To list downloaded images
[root@containerhost ~]# podman image ls
[root@containerhost ~]# podman images
6. Getting detail information about an image:
podman image inspect <image name>
[root@containerhost ~]# podman image inspect docker.io/library/httpd
[root@containerhost ~]# podman image inspect docker.io/library/httpd | less
---------------------------------
...
"Config": {
"ExposedPorts": {
"80/tcp": {}
},
"WorkingDir": "/usr/local/apache2",
...
7. Listing containers (only active/running containers)
[root@containerhost ~]# podman container ls
8. Listing containers (all containers: both active and inactive)
[root@containerhost ~]# podman ps -a
9. Creating a locally accessible container:
podman run -d — name <container name> <image name>
[root@containerhost ~]# podman run -d --name webserver docker.io/library/httpd
[root@containerhost ~]# podman container ls
[root@containerhost ~]# podman ps -a
10. Accessing local container: podman exec -it <container name> bash
[root@containerhost ~]# podman exec -it webserver bash
root@24ed7af3d7ed:/usr/local/apache2# hostname
24ed7af3d7ed
root@24ed7af3d7ed:/usr/local/apache2# hostname -I
10.88.0.2
11. To view OS details
root@24ed7af3d7ed:/usr/local/apache2# cat /etc/os-release
12. Install net tools package
root@24ed7af3d7ed:/usr/local/apache2# apt-get update
root@24ed7af3d7ed:/usr/local/apache2# apt-get -y install net-tools
root@24ed7af3d7ed:/usr/local/apache2# ifconfig
root@24ed7af3d7ed:/usr/local/apache2# route -n
13. Install ping command package
root@24ed7af3d7ed:/usr/local/apache2# apt-get -y install iputils-ping
root@24ed7af3d7ed:/usr/local/apache2# ping google.com
14. Accessing web page locally
root@24ed7af3d7ed:/usr/local/apache2# cd
root@24ed7af3d7ed:~# find / -name httpd.conf
root@24ed7af3d7ed:~# cat /usr/local/apache2/conf/httpd.conf
root@24ed7af3d7ed:~# grep -i DocumentRoot /usr/local/apache2/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/usr/local/apache2/htdocs"
# access content that does not live under the DocumentRoot.
root@24ed7af3d7ed:~# cd /usr/local/apache2/htdocs
root@24ed7af3d7ed:/usr/local/apache2/htdocs# ls
index.html
root@24ed7af3d7ed:/usr/local/apache2/htdocs# cat index.html
<html><body><h1>It works!</h1></body></html>
root@24ed7af3d7ed:/usr/local/apache2/htdocs# cd
root@24ed7af3d7ed:~# apt-get -y install curl
root@24ed7af3d7ed:~# curl http://localhost
<html><body><h1>It works!</h1></body></html>
root@24ed7af3d7ed:~# exit
exit
[root@containerhost ~]# curl http://10.88.0.2
<html><body><h1>It works!</h1></body></html>
[root@containerhost ~]# ping 10.88.0.2
15. Creating remotely accessible container:
podman run -d — name <container name> -p <host port>:<container port> <image name>
[root@containerhost ~]# podman run -d --name wwwserver -p 8899:80 docker.io/library/httpd:latest
[root@containerhost ~]# podman ps
[root@containerhost ~]# podman ps -a
16. Change the base image’s testing purpose index file: ctrl+d to exit from cat>file_name editor
[root@containerhost ~]# podman exec -it wwwserver bash
root@37559b82ce62:/usr/local/apache2# cd /usr/local/apache2/htdocs/
root@37559b82ce62:/usr/local/apache2/htdocs# pwd
/usr/local/apache2/htdocs
root@37559b82ce62:/usr/local/apache2/htdocs# ls
index.html
root@37559b82ce62:/usr/local/apache2/htdocs# cat index.html
<html><body><h1>It works!</h1></body></html>
root@37559b82ce62:/usr/local/apache2/htdocs# rm -f index.html
root@37559b82ce62:/usr/local/apache2/htdocs# cat >index.html
<h1>This is newly created webpage!</h1>
17. To stop a container: podman container stop <container name>
[root@containerhost ~]# podman container stop wwwserver
[root@containerhost ~]# podman ps
[root@containerhost ~]# podman ps -a
18. To start a container: podman container start <container name>
[root@containerhost ~]# podman container start wwwserver
[root@containerhost ~]# podman ps
19. To delete a container: podman rm <container name>
[root@containerhost ~]# podman stop wwwserver
[root@containerhost ~]# podman rm wwwserver
[root@containerhost ~]# podman ps -a
20. Remove container forcefully without doing stop: podman rm -f <container name>
[root@containerhost ~]# podman rm -f webserver
[root@containerhost ~]# podman ps -a
21. Removing a container image: podman rmi <image name>
[root@containerhost ~]# podman rmi docker.io/library/httpd:latest
[root@containerhost ~]# podman images
22. Creating custom container image (base image + app)
PHP web app → base image (webserver — httpd/nginx) + php app
Java web app → base image (application server — tomcat/jboss/glashfish) + java web app
Associating container with persistent storage
[root@containerhost ~]# mkdir -p /webdoc/html
[root@containerhost ~]# cd /webdoc/html/
[root@containerhost html]# pwd
/webdoc/html
[root@containerhost html]# vi index.html
[root@containerhost html]# cat index.html
<h1>Welcome to portfolio site! </h1>
[root@containerhost html]# podman run -d --name newwebserver -p 3344:80 -v /webdoc/html:/var/www/html:Z docker.io/centos/httpd
[root@containerhost html]# podman ps
[root@containerhost html]# podman exec -it newwebserver bash
[root@293776013d27 /]# cd /var/www/html/
[root@293776013d27 html]# ls
index.html
[root@293776013d27 html]# cat index.html
<h1>Welcome to portfolio site! </h1>
[root@293776013d27 html]# exit
Modify index page content on the host machine; changes will be reflected automatically on the containers.
[root@containerhost ~]# cd /webdoc/html/
[root@containerhost html]# vi index.html
[root@containerhost html]# cat index.html
<h1>Welcome to portfolio site! -Second Version</h1>
[root@containerhost html]# touch file1 file
[root@containerhost html]# podman exec -it newwebserver bash
[root@293776013d27 /]# cd /var/www/html/
[root@293776013d27 html]# ls
file file1 index.html
[root@293776013d27 html]# exit
23. Creating custom container image:
podman build -t <custom img name> <container file path>
[root@containerhost ~]# vi index.html
[root@containerhost ~]# cat index.html
<h1>Website running on container using custom image! </h1>
[root@containerhost ~]# vi Containerfile
---------------------------------
FROM docker.io/centos/httpd
MAINTAINER surya@srg.com
COPY ./index.html /var/www/html
RUN yum -y install net-tools
RUN useradd naba
RUN echo "nabapass" | passwd --stdin naba
RUN touch /tmp/hello
---------------------------------
[root@containerhost ~]# podman build -t customhttpdimg .
[root@containerhost ~]# podman images
[root@containerhost ~]# podman image inspect docker.io/centos/httpd:latest | less
24. Create a container using the custom image
[root@containerhost ~]# podman run -d --name www2server -p 3456:80 customhttpdimg:latest
[root@containerhost ~]# podman ps
25. Modify index page content on the host machine; remove existing custom image and container and create a new custom image and container to reflect the changes.
[root@containerhost ~]# vi index.html
[root@containerhost ~]# cat index.html
<h1>Website running on container using custom image! -Second Version </h1>
[root@containerhost ~]# podman rm -f www2server
[root@containerhost ~]# podman rmi customhttpdimg:latest
[root@containerhost ~]# podman build -t newcustomhttpdimg .
[root@containerhost ~]# podman run -d --name www3server -p 3456:80 newcustomhttpdimg:latest
[root@containerhost ~]# podman exec -it www3server bash
[root@3ad99395c5b9 /]# rpm -q net-tools
[root@3ad99395c5b9 /]# cat /etc/passwd
[root@3ad99395c5b9 /]# su - naba
[naba@3ad99395c5b9 ~]$ pwd
/home/naba