Saturday, August 2, 2025

Docker Part2

Docker Part2

Class 80th DOCKER Part2 August 2nd

What is Containerization 

Creating Container 

Step1:

root@ip-10-0-2-61:~# hostnamectl set-hostname dockerhost

root@ip-10-0-2-61:~# sudo -i

--Remove if already exist 

root@dockerhost:~# docker rm container1
container1
--Create new container -it going inside the container1
root@dockerhost:~# docker run -it --name container1  ubuntu
root@adc881f1c093:/#

Install the application packages in the container 
--apt update in container
root@adc881f1c093:/# apt update
-- Tomcat apache installed in container 

root@adc881f1c093:/# apt install apache2 -y
root@adc881f1c093:/# apache2 -v
Server version: Apache/2.4.58 (Ubuntu)
Server built:   2025-07-14T16:22:22

--Tree install 
root@adc881f1c093:/# apt install tree -y

Creating Some files 1 to 5 
root@adc881f1c093:/# touch file{1..5}
-rw-r--r--   1 root root    0 Aug 10 18:23 file5
-rw-r--r--   1 root root    0 Aug 10 18:23 file4
-rw-r--r--   1 root root    0 Aug 10 18:23 file3
-rw-r--r--   1 root root    0 Aug 10 18:23 file2
-rw-r--r--   1 root root    0 Aug 10 18:23 file1

root@adc881f1c093:/# cat >file1
This is my container1 file1
Step2:

 Custom Image Container backup 
We  have only one image
root@dockerhost:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    65ae7a6f3544   3 weeks ago   78.1MB
We have only one Container
root@dockerhost:~# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
adc881f1c093   ubuntu    "/bin/bash"   12 minutes ago   Up 12 minutes                         container1
Docker commit
Step3: Create new image using container1( container1 has all package information)

root@dockerhost:~# docker commit container1 ubuntupkh_img:v1
sha256:327dee052d80405da2565ef6e042f8e2be3eb5226af23af4217408c8b0d57921

root@dockerhost:~# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
ubuntupkh_img   v1        327dee052d80   43 seconds ago   245MB
ubuntu          latest    65ae7a6f3544   3 weeks ago      78.1MB

Using New image creating new containers 
Step4: see here automatically came on packages and files information in container2 

root@dockerhost:~# docker run -it --name container2 ubuntupkh_img:v1
root@b95d921f2476:/# ls -lrt
dr-xr-xr-x  13 root root    0 Aug 10 18:22 sys
-rw-r--r--   1 root root    0 Aug 10 18:23 file5
-rw-r--r--   1 root root    0 Aug 10 18:23 file4
-rw-r--r--   1 root root    0 Aug 10 18:23 file3
-rw-r--r--   1 root root    0 Aug 10 18:23 file2
-rw-r--r--   1 root root   28 Aug 10 18:24 file1
drwxr-xr-x   1 root root 4096 Aug 10 18:38 etc
dr-xr-xr-x 181 root root    0 Aug 10 18:38 proc
drwxr-xr-x   5 root root  360 Aug 10 18:38 dev

See file and packages exists 
root@b95d921f2476:/# tree --version
tree v2.1.1 (c) 1996 - 2023 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro
root@b95d921f2476:/# apache2 -v
Server version: Apache/2.4.58 (Ubuntu)
Server built:   2025-07-14T16:22:22

We can called Containerization ,this is called os level virtualization, it main purpose reusability 

DOCKERFILE:
it is an automation way to create image.
here we use components to create image.
in Dockerfile D must be Capital.
Components also capital.
To write our instructions we need to use components in Dockerfile
This Dockerfile will be Reuseable.
here we can create image directly without container help.
Name: Dockerfile 


COMPONENTS:

FROM : used to base image
RUN : used to run linux commands (During image creation)
CMD : used to run linux commands (After container creation)
ENTRYPOINT : high priority than cmd
COPY : to copy local files to container
ADD : to copy internet files to container
WORKDIR : to open req directory
LABEL : to add labels for docker images
ENV : to set env variables (inside container)
ARGS : to pass env variables (outside containers)
EXPOSE : to indicate port number
VOLUME
NETWORK

Step5: for all instruction you need components 

Dockerfile -->image-->container

Kill the container using below command 

root@dockerhost:~# docker ps -a
CONTAINER ID   IMAGE              COMMAND       CREATED          STATUS          PORTS     NAMES
b95d921f2476   ubuntupkh_img:v1   "/bin/bash"   14 minutes ago   Up 14 minutes             container2
adc881f1c093   ubuntu             "/bin/bash"   36 minutes ago   Up 36 minutes             container1
root@dockerhost:~# docker ps -aq
b95d921f2476
adc881f1c093
root@dockerhost:~# docker kill $(docker ps -aq)
b95d921f2476
adc881f1c093
root@dockerhost:~# docker ps -a
CONTAINER ID   IMAGE              COMMAND       CREATED          STATUS                       PORTS     NAMES
b95d921f2476   ubuntupkh_img:v1   "/bin/bash"   15 minutes ago   Exited (137) 8 seconds ago             container2
adc881f1c093   ubuntu             "/bin/bash"   37 minutes ago   Exited (137) 8 seconds ago             container1
 
Remove containers

root@dockerhost:~# docker rm $(docker ps -aq)
b95d921f2476
adc881f1c093
root@dockerhost:~# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Remove images 

root@dockerhost:~# docker images -q
327dee052d80
65ae7a6f3544
root@dockerhost:~# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
ubuntupkh_img   v1        327dee052d80   21 minutes ago   245MB
ubuntu          latest    65ae7a6f3544   3 weeks ago      78.1MB
root@dockerhost:~# docker rmi $(docker images -q)
Untagged: ubuntupkh_img:v1
Deleted: sha256:327dee052d80405da2565ef6e042f8e2be3eb5226af23af4217408c8b0d57921
Deleted: sha256:4a286e29abe731e078404a9bdc200ff3cec139e5d69ec2456c3aa0a465973fc1
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:a08e551cb33850e4740772b38217fc1796a66da2506d312abe51acda354ff061
Deleted: sha256:65ae7a6f3544bd2d2b6d19b13bfc64752d776bc92c510f874188bfd404d205a3
Deleted: sha256:107cbdaeec042e6154640c94972c638f4e2fee795902b149e8ce9acbd03d59d7

Step6: Docker file creating steps 

ENTRYPOINT VS CMD:

FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["1000"]


root@dockerhost:~# vim Dockerfile
root@dockerhost:~# cat Dockerfile
FROM ubuntu
RUN apt update -y
RUN apt install apache2 -y
RUN apt install python3 -y
CMD apt install mysql-server -y

Step7: Build docker image, As see my sql not installed , it is cmd prompt ,so it will run whenevery container creating time

root@dockerhost:~# docker build -t dockerimage:v1 .
Step 5/5 : CMD apt install mysql-server -y
 ---> Running in 72d94de11f19
 ---> Removed intermediate container 72d94de11f19
 ---> 162f726580bf

Successfully built 162f726580bf
Successfully tagged dockerimage:v1

root@dockerhost:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
dockerimage   v1        162f726580bf   34 seconds ago   281MB
ubuntu        latest    65ae7a6f3544   3 weeks ago      78.1MB

Step8: Create container,now see mysql installing 
root@dockerhost:~# docker run -it --name containerdocker1 dockerimage:v1
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
Setting up libcgi-fast-perl (1:2.17-1) ...
Setting up mysql-server (8.0.42-0ubuntu0.24.04.2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.5) ...

One more image COPY command existing file copy to /tmp folder ADD command for dowload software from intenet and placing in /tmp folder
Step9: copy and add 

root@dockerhost:~# vi Dockerfile
root@dockerhost:~# cat Dockerfile
FROM ubuntu
COPY index.html /tmp
ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.10/src/apache-tomcat-11.0.10-src.zip /tmp


Step10: now again build new image 

root@dockerhost:~# docker build -t dockercpimage:v2 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  30.72kB
Step 1/3 : FROM ubuntu
 ---> 65ae7a6f3544
Step 2/3 : COPY index.html /tmp
 ---> 0087e026e831
Step 3/3 : ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.10/src/apache-tomcat-11.0.10-src.zip /tmp
Downloading [==================================================>]  11.44MB/11.44MB

 ---> 23b4041c4bf6
Successfully built 23b4041c4bf6
Successfully tagged dockercpimage:v2

root@dockerhost:~# docker images
REPOSITORY      TAG       IMAGE ID       CREATED              SIZE
dockercpimage   v2        23b4041c4bf6   About a minute ago   89.6MB
dockerimage     v1        162f726580bf   14 minutes ago       281MB
ubuntu          latest    65ae7a6f3544   3 weeks ago          78.1MB


Step11: Creating Container, now see cp coming local , tomcat coming from internet website
    
root@dockerhost:~# docker run -it --name containercp1 dockercpimage:v2
root@e9429dfc244e:/# cd /tmp
root@e9429dfc244e:/tmp# ls
apache-tomcat-11.0.10-src.zip  index.html


Step12: WORKDIR,LABEL ,build again creating new image 
WORKERDIR it will goto specific path 
LABEL give specific name to image

root@dockerhost:~# docker build -t dockercpimage1:v3 .
root@dockerhost:~# docker build -t dockercpimage1:v3 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  31.23kB
Step 1/5 : FROM ubuntu
 ---> 65ae7a6f3544
Step 2/5 : COPY index.html /tmp
 ---> Using cache
 ---> 0087e026e831
Step 3/5 : ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.10/src/apache-tomcat-11.0.10-src.zip /tmp
Downloading [==================================================>]  11.44MB/11.44MB

 ---> Using cache
 ---> 23b4041c4bf6
Step 4/5 : WORKDIR /tmp
 ---> Running in 290cb2b88cd2
 ---> Removed intermediate container 290cb2b88cd2
 ---> aed410d8af71
Step 5/5 : LABEL author vakatisubbu
 ---> Running in 471636289f7c
 ---> Removed intermediate container 471636289f7c
 ---> e2720128a90d
Successfully built e2720128a90d
Successfully tagged dockercpimage1:v3

Step13: it is going automatically temp directory, docker inspect label author name checking with ur name

root@dockerhost:~# docker run -it --name containerwrk2 dockercpimage1:v3
root@2afd2fdc76f3:/tmp#

exit from container ctrl+P+Q
root@2afd2fdc76f3:/tmp# docker inspect root@dockerhost:~#
 

  "Labels": {
                "author": "vakatisubbu",
                "org.opencontainers.image.ref.name": "ubuntu",
                "org.opencontainers.image.version": "24.04"
            }

Step14: ENV for environment variables EXPOSE for container which port to run 

root@dockerhost:~# cat Dockerfile
FROM ubuntu
ENV user vakatisubbu
ENV client swiggy
EXPOSE 8080

Step15: Dockerbuild and create container 
root@dockerhost:~# docker build -t dockerendimage2:v4 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  31.23kB
Step 1/4 : FROM ubuntu
 ---> 65ae7a6f3544
Step 2/4 : ENV user vakatisubbu
 ---> Running in c9329776bcde
 ---> Removed intermediate container c9329776bcde
 ---> 1433b1258b7f
Step 3/4 : ENV client swiggy
 ---> Running in 3cfea1ee6546
 ---> Removed intermediate container 3cfea1ee6546
 ---> eeac0fd1965a
Step 4/4 : EXPOSE 8080
 ---> Running in aeb3d9efcf03
 ---> Removed intermediate container aeb3d9efcf03
 ---> 79148bd0f0ce
Successfully built 79148bd0f0ce
Successfully tagged dockerendimage2:v4

root@dockerhost:~# docker images
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
dockerendimage2   v4        79148bd0f0ce   22 seconds ago   78.1MB
dockercpimage1    v3        e2720128a90d   9 minutes ago    89.6MB
dockercpimage     v2        23b4041c4bf6   18 minutes ago   89.6MB
dockerimage       v1        162f726580bf   31 minutes ago   281MB
ubuntu            latest    65ae7a6f3544   3 weeks ago      78.1MB

Step16:

root@dockerhost:~# docker run -it --name containerenv2 dockerendimage2:v4
root@fc37e72ef91a:/#

username came with your name 
root@fc37e72ef91a:/# echo $user
vakatisubbu
root@fc37e72ef91a:/# echo $client
swiggy

docker running which port 
root@dockerhost:~# docker ps
CONTAINER ID   IMAGE                COMMAND       CREATED              STATUS              PORTS      NAMES
fc37e72ef91a   dockerendimage2:v4   "/bin/bash"   About a minute ago   Up About a minute   8080/tcp   containerenv2
2afd2fdc76f3   dockercpimage1:v3    "/bin/bash"   11 minutes ago       Up 11 minutes                  containerwrk2
e9429dfc244e   dockercpimage:v2     "/bin/bash"   18 minutes ago       Up 18 minutes                  containercp1


Step17: Below official document more comments for refernces
https://docs.docker.com/reference/dockerfile/



 Build one netflex application 

NETFLIX-DEPLOYMENT:

apt install git -y
git clone https://github.com/RAHAMSHAIK007/netflix-clone.git
cd netflix-clone/

Vi Dockerfile

FROM ubuntu
RUN apt update
RUN apt install apache2 -y
COPY * /var/www/html/
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]


docker build -t netflix:v1 .
docker run -it --name netflix1 -p 80:80 netflix:v1


Step1:

root@dockerhost:~# git clone https://github.com/RAHAMSHAIK007/netflix-clone.git
Cloning into 'netflix-clone'...
remote: Enumerating objects: 130, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 130 (delta 14), reused 1 (delta 1), pack-reused 110 (from 2)
Receiving objects: 100% (130/130), 47.61 KiB | 4.76 MiB/s, done.
Resolving deltas: 100% (75/75), done.
root@dockerhost:~# pwd
/root
root@dockerhost:~# cd netflix-clone/

Step2: Create one docker file here 
root@dockerhost:~/netflix-clone# ls -lrt
total 56
-rw-r--r-- 1 root root  106 Aug 10 19:50 README.md
-rw-r--r-- 1 root root 2829 Aug 10 19:50 style.css
-rw-r--r-- 1 root root  666 Aug 10 19:50 session.js
-rw-r--r-- 1 root root 6706 Aug 10 19:50 script.js
-rw-r--r-- 1 root root 1018 Aug 10 19:50 register.js
-rw-r--r-- 1 root root 2908 Aug 10 19:50 register.html
-rw-r--r-- 1 root root  574 Aug 10 19:50 register.css
-rw-r--r-- 1 root root 1846 Aug 10 19:50 plan.md
-rw-r--r-- 1 root root 1163 Aug 10 19:50 login.js
-rw-r--r-- 1 root root 2672 Aug 10 19:50 login.html
-rw-r--r-- 1 root root  698 Aug 10 19:50 login.css
-rw-r--r-- 1 root root 3609 Aug 10 19:50 index.html
drwxr-xr-x 2 root root 4096 Aug 10 19:50 assets

Step3:CMD ["/usr/sbin/apachectl","FOREGROUND"] ,this command if you exit from the container,
even though apachectl run ,it will  foreground from backend


root@dockerhost:~/netflix-clone# vi Dockerfile
root@dockerhost:~/netflix-clone# cat Dockerfile
FROM ubuntu
RUN apt update -y
RUN apt install apache2 -y
COPY * /var/www/html/
CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]


Step4:

root@dockerhost:~/netflix-clone# docker build -t dockernetflexfinal .

Successfully built 727cd091145b
Successfully tagged dockernetflexfinal:latest
root@dockerhost:~/netflix-clone# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
dockernetflexfinal   latest    727cd091145b   22 seconds ago   245MB
dockernetflex        v4        ae3c756c75a6   11 minutes ago   245MB
dockernetflex        latest    c6219b442e36   22 minutes ago   245MB
dockernetflex        v1        c6219b442e36   22 minutes ago   245MB
ubuntu               latest    65ae7a6f3544   3 weeks ago      78.1MB


Step5: 
root@dockerhost:~/netflix-clone# docker run -it --name containernetfinal -p 83:80  dockernetflexfinal
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message


Step6:



Step7: if you want change any some line code in login page you edit can build create container 
for ex:  NETFLEX V-4.0 changed to NETFLEX V-5.0


Step8:

root@dockerhost:~/netflix-clone# docker build -t dockernetflexfinal1 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  185.3kB
Step 1/5 : FROM ubuntu
 ---> 65ae7a6f3544
Step 2/5 : RUN apt update -y
 ---> Using cache
 ---> f2511e97f53b
Step 3/5 : RUN apt install apache2 -y
 ---> Using cache
 ---> aff58ac566bc
Step 4/5 : COPY * /var/www/html/
 ---> ec7b4377c133
Step 5/5 : CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]
 ---> Running in 1dee218601b8
 ---> Removed intermediate container 1dee218601b8
 ---> 5f0d03a2fc93
Successfully built 5f0d03a2fc93
Successfully tagged dockernetflexfinal1:latest
root@dockerhost:~/netflix-clone# docker run -it --name containernetfinal1 -p 84:80  dockernetflexfinal1

Step9:



--Thanks 

No comments:

Post a Comment