Day2: 2. Build all these 5 images and run container and observe the use cases of it
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
https://devopsschool.com/tutorial/docker/dockerfile/dockerfile-example-sample-lab.html
Docker file Example and Sample Program – 1
dockerfile1
FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
CMD /usr/sbin/apache2ctl -D FOREGROUND
root@ip-172-31-28-155:/tmp/anoop# docker build -f dockerfile1 -t anoop_docker1 .
Sending build context to Docker daemon 2.048kB
Step 1/7 : FROM ubuntu
---> fb52e22af1b0
Step 2/7 : MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
---> Using cache
---> 01477b86dfc8
Step 3/7 : ENV DEBIAN_FRONTEND=noninteractive
---> Using cache
---> 57a1ad7c1cea
Step 4/7 : RUN apt-get update
---> Using cache
---> 1207d5f945f3
Step 5/7 : RUN apt-get -y install tzdata
---> Using cache
---> e99c418f5399
Step 6/7 : RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
---> Using cache
---> 0adfc67f9737
Step 7/7 : CMD /usr/sbin/apache2ctl -D FOREGROUND
---> Using cache
---> f5b4a10dc064
Successfully built f5b4a10dc064
Successfully tagged anoop_docker1:latest
root@ip-172-31-28-155:/tmp/anoop# docker run -itd --name anoop_docker1_container anoop_docker1
cf665d955cd85fb5c6bcf23a52fca04fdd0457239447a6b7f8d7c3d0e38942de
root@ip-172-31-28-155:/tmp/anoop# docker images | grep anoop_docker1
anoop_docker1 latest f5b4a10dc064 11 minutes ago 259MB
root@ip-172-31-28-155:/tmp/anoop# docker ps | grep anoop_docker1
cf665d955cd8 anoop_docker1 "/bin/sh -c '/usr/sb…" 20 seconds ago Up 19 seconds anoop_docker1_container
root@ip-172-31-28-155:/tmp/anoop# docker history anoop_docker1
IMAGE CREATED CREATED BY SIZE COMMENT
f5b4a10dc064 12 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/usr… 0B
0adfc67f9737 12 minutes ago /bin/sh -c apt-get update && apt-get install… 152MB
e99c418f5399 12 minutes ago /bin/sh -c apt-get -y install tzdata 4.25MB
1207d5f945f3 12 minutes ago /bin/sh -c apt-get update 30.1MB
57a1ad7c1cea 13 minutes ago /bin/sh -c #(nop) ENV DEBIAN_FRONTEND=nonin… 0B
01477b86dfc8 13 minutes ago /bin/sh -c #(nop) MAINTAINER Rajesh Kumar <… 0B
fb52e22af1b0 6 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 hours ago /bin/sh -c #(nop) ADD file:d2abf27fe2e8b0b5f… 72.8MB
root@ip-172-31-28-155:/tmp/anoop# docker exec -it cf665d955cd8 /bin/sh
# ps
PID TTY TIME CMD
74 pts/1 00:00:00 sh
80 pts/1 00:00:00 ps
# top -n1
top - 07:08:42 up 3:23, 0 users, load average: 0.52, 0.65, 0.69
Tasks: 7 total, 1 running, 6 sleeping, 0 stopped, 0 zombie
%Cpu(s): 24.6 us, 0.0 sy, 0.0 ni, 75.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 16011.7 total, 7419.4 free, 1170.5 used, 7421.8 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 14523.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 2608 612 540 S 0.0 0.0 0:00.02 sh
8 root 20 0 2608 1924 1808 S 0.0 0.0 0:00.00 apache2ctl
17 root 20 0 6520 5164 3984 S 0.0 0.0 0:00.02 apache2
18 www-data 20 0 1932064 3976 2456 S 0.0 0.0 0:00.00 apache2
19 www-data 20 0 1932064 3976 2456 S 0.0 0.0 0:00.00 apache2
74 root 20 0 2608 604 536 S 0.0 0.0 0:00.02 sh
81 root 20 0 6112 3468 2904 R 0.0 0.0 0:00.00 top
# git --version
git version 2.25.1
Docker file Example and Sample Program – 2
dockerfile2
FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
CMD echo "Hello world"
root@ip-172-31-28-155:/tmp/anoop# docker build -f dockerfile2 -t anoop_docker2 .
Sending build context to Docker daemon 3.072kB
Step 1/7 : FROM ubuntu
---> fb52e22af1b0
Step 2/7 : MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
---> Using cache
---> 01477b86dfc8
Step 3/7 : ENV DEBIAN_FRONTEND=noninteractive
---> Using cache
---> 57a1ad7c1cea
Step 4/7 : RUN apt-get update
---> Using cache
---> 1207d5f945f3
Step 5/7 : RUN apt-get -y install tzdata
---> Using cache
---> e99c418f5399
Step 6/7 : RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
---> Using cache
---> 0adfc67f9737
Step 7/7 : CMD echo "Hello world"
---> Using cache
---> f3008cf52e3f
Successfully built f3008cf52e3f
Successfully tagged anoop_docker2:latest
root@ip-172-31-28-155:/tmp/anoop# docker images | grep anoop_docker2
anoop_docker2 latest f3008cf52e3f 15 minutes ago 259MB
root@ip-172-31-28-155:/tmp/anoop# docker history anoop_docker2
IMAGE CREATED CREATED BY SIZE COMMENT
f3008cf52e3f 16 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
0adfc67f9737 18 minutes ago /bin/sh -c apt-get update && apt-get install… 152MB
e99c418f5399 19 minutes ago /bin/sh -c apt-get -y install tzdata 4.25MB
1207d5f945f3 19 minutes ago /bin/sh -c apt-get update 30.1MB
57a1ad7c1cea 19 minutes ago /bin/sh -c #(nop) ENV DEBIAN_FRONTEND=nonin… 0B
01477b86dfc8 19 minutes ago /bin/sh -c #(nop) MAINTAINER Rajesh Kumar <… 0B
fb52e22af1b0 6 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 hours ago /bin/sh -c #(nop) ADD file:d2abf27fe2e8b0b5f… 72.8MB
root@ip-172-31-28-155:/tmp/anoop# docker run -itd --name anoop_docker2_container anoop_docker2
416f9e0f092de77c66420278dd5ba611c8c55d07b90f232f130de5fd6b9ccf88
root@ip-172-31-28-155:/tmp/anoop# docker ps | grep anoop_docker2
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker2
416f9e0f092d anoop_docker2 "/bin/sh -c 'echo \"H…" 22 seconds ago Exited (0) 22 seconds ago anoop_docker2_container
root@ip-172-31-28-155:/tmp/anoop# docker logs 416f9e0f092d
Hello world
root@ip-172-31-28-155:/tmp/anoop# docker run -it --name anoop_docker2_container_2 anoop_docker2
Hello world
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker2
75a1162bfb04 anoop_docker2 "/bin/sh -c 'echo \"H…" 8 seconds ago Exited (0) 7 seconds ago anoop_docker2_container_2
416f9e0f092d anoop_docker2 "/bin/sh -c 'echo \"H…" About a minute ago Exited (0) About a minute ago anoop_docker2_container
- Try to change the command (pid1)
root@ip-172-31-28-155:/tmp/anoop# docker run --name anoop_docker2_container_3 anoop_docker2 git --version
git version 2.25.1
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker2
928fe16d452a anoop_docker2 "git --version" 6 seconds ago Exited (0) 6 seconds ago anoop_docker2_container_3
75a1162bfb04 anoop_docker2 "/bin/sh -c 'echo \"H…" 5 minutes ago Exited (0) 5 minutes ago anoop_docker2_container_2
416f9e0f092d anoop_docker2 "/bin/sh -c 'echo \"H…" 7 minutes ago Exited (0) 7 minutes ago anoop_docker2_container
Docker file Example and Sample Program – 3
dockerfile3
FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
ENTRYPOINT ["/bin/echo", "Hello"]
root@ip-172-31-28-155:/tmp/anoop# docker build -f dockerfile3 -t anoop_docker3 .
Sending build context to Docker daemon 4.096kB
Step 1/8 : FROM ubuntu
---> fb52e22af1b0
Step 2/8 : MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
---> Using cache
---> 01477b86dfc8
Step 3/8 : ENV DEBIAN_FRONTEND=noninteractive
---> Using cache
---> 57a1ad7c1cea
Step 4/8 : RUN apt-get update
---> Using cache
---> 1207d5f945f3
Step 5/8 : RUN apt-get update
---> Using cache
---> 4156a194a23b
Step 6/8 : RUN apt-get -y install tzdata
---> Using cache
---> 9b31137fbd43
Step 7/8 : RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
---> Using cache
---> 17c90bab3df8
Step 8/8 : ENTRYPOINT ["/bin/echo", "Hello"]
---> Using cache
---> faf040ddabad
Successfully built faf040ddabad
Successfully tagged anoop_docker3:latest
root@ip-172-31-28-155:/tmp/anoop# docker images | grep anoop_docker3
anoop_docker3 latest faf040ddabad 23 minutes ago 260MB
root@ip-172-31-28-155:/tmp/anoop# docker history anoop_docker3
IMAGE CREATED CREATED BY SIZE COMMENT
faf040ddabad 23 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/echo" "… 0B
17c90bab3df8 23 minutes ago /bin/sh -c apt-get update && apt-get install… 152MB
9b31137fbd43 24 minutes ago /bin/sh -c apt-get -y install tzdata 4.25MB
4156a194a23b 24 minutes ago /bin/sh -c apt-get update 593kB
1207d5f945f3 28 minutes ago /bin/sh -c apt-get update 30.1MB
57a1ad7c1cea 28 minutes ago /bin/sh -c #(nop) ENV DEBIAN_FRONTEND=nonin… 0B
01477b86dfc8 28 minutes ago /bin/sh -c #(nop) MAINTAINER Rajesh Kumar <… 0B
fb52e22af1b0 6 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 hours ago /bin/sh -c #(nop) ADD file:d2abf27fe2e8b0b5f… 72.8MB
root@ip-172-31-28-155:/tmp/anoop# docker run -itd --name anoop_docker3_container anoop_docker3
a8e3c3ea5a1df1bdb2cb5ee2f6479c9048940ad952d511eb1b1695dea6d77906
root@ip-172-31-28-155:/tmp/anoop# docker ps | grep anoop_docker3
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker3
a8e3c3ea5a1d anoop_docker3 "/bin/echo Hello" 16 seconds ago Exited (0) 16 seconds ago anoop_docker3_container
root@ip-172-31-28-155:/tmp/anoop# docker logs a8e3c3ea5a1d
Hello
root@ip-172-31-28-155:/tmp/anoop# docker run -it --name anoop_docker3_container_2 anoop_docker3
Hello
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker3
909714ffe410 anoop_docker3 "/bin/echo Hello" 4 seconds ago Exited (0) 4 seconds ago anoop_docker3_container_2
a8e3c3ea5a1d anoop_docker3 "/bin/echo Hello" About a minute ago Exited (0) About a minute ago anoop_docker3_container
- Tried to change command (pid1), but entrypoint in docker layer considered it as parameter to entry point command
root@ip-172-31-28-155:/tmp/anoop# docker run --name anoop_docker3_container_3 anoop_docker3 git --version
Hello git --version
Docker file Example and Sample Program – 4
dockerfile4
FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
EXPOSE 80
CMD /usr/sbin/apache2ctl -D FOREGROUND
root@ip-172-31-28-155:/tmp/anoop# docker build -f dockerfile4 -t anoop_docker4 .
Sending build context to Docker daemon 5.12kB
Step 1/8 : FROM ubuntu
---> fb52e22af1b0
Step 2/8 : MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
---> Using cache
---> 01477b86dfc8
Step 3/8 : ENV DEBIAN_FRONTEND=noninteractive
---> Using cache
---> 57a1ad7c1cea
Step 4/8 : RUN apt-get update
---> Using cache
---> 1207d5f945f3
Step 5/8 : RUN apt-get -y install tzdata
---> Using cache
---> e99c418f5399
Step 6/8 : RUN apt-get update && apt-get install git -y && apt-get install -yq apache2
---> Using cache
---> 0adfc67f9737
Step 7/8 : EXPOSE 80
---> Using cache
---> d984750330b2
Step 8/8 : CMD /usr/sbin/apache2ctl -D FOREGROUND
---> Using cache
---> ef7c01cfe904
Successfully built ef7c01cfe904
Successfully tagged anoop_docker4:latest
root@ip-172-31-28-155:/tmp/anoop# docker images | grep anoop_docker4
anoop_docker4 latest ef7c01cfe904 29 minutes ago 259MB
root@ip-172-31-28-155:/tmp/anoop#
root@ip-172-31-28-155:/tmp/anoop# docker history anoop_docker4
IMAGE CREATED CREATED BY SIZE COMMENT
ef7c01cfe904 29 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/usr… 0B
d984750330b2 29 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
0adfc67f9737 35 minutes ago /bin/sh -c apt-get update && apt-get install… 152MB
e99c418f5399 35 minutes ago /bin/sh -c apt-get -y install tzdata 4.25MB
1207d5f945f3 35 minutes ago /bin/sh -c apt-get update 30.1MB
57a1ad7c1cea 36 minutes ago /bin/sh -c #(nop) ENV DEBIAN_FRONTEND=nonin… 0B
01477b86dfc8 36 minutes ago /bin/sh -c #(nop) MAINTAINER Rajesh Kumar <… 0B
fb52e22af1b0 6 hours ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 hours ago /bin/sh -c #(nop) ADD file:d2abf27fe2e8b0b5f… 72.8MB
root@ip-172-31-28-155:/tmp/anoop# docker run -itd --name anoop_docker4_container anoop_docker4
acfd8a45977097a5671fb88d7fccd87e940a70ceafb93a5ae5a80ad99c79bb20
root@ip-172-31-28-155:/tmp/anoop# docker ps | grep anoop_docker4
acfd8a459770 anoop_docker4 "/bin/sh -c '/usr/sb…" 8 seconds ago Up 6 seconds 80/tcp anoop_docker4_container
- changed the PID1
root@ip-172-31-28-155:/tmp/anoop# docker exec -it acfd8a459770 /bin/sh
# top -n1
top - 08:00:15 up 4:15, 0 users, load average: 0.02, 0.03, 0.10
Tasks: 7 total, 1 running, 6 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 16011.7 total, 3266.8 free, 1419.5 used, 11325.4 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 14296.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 2608 612 540 S 0.0 0.0 0:00.03 sh
9 root 20 0 2608 1780 1660 S 0.0 0.0 0:00.00 apache2ctl
18 root 20 0 6520 5208 4032 S 0.0 0.0 0:00.08 apache2
19 www-data 20 0 1932064 3816 2300 S 0.0 0.0 0:00.00 apache2
20 www-data 20 0 1997600 3816 2300 S 0.0 0.0 0:00.00 apache2
75 root 20 0 2608 548 476 S 0.0 0.0 0:00.02 sh
81 root 20 0 6112 3284 2716 R 0.0 0.0 0:00.00 top
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker4_container_2
b90d2f687bea anoop_docker4 "git --version" About a minute ago Exited (0) About a minute ago anoop_docker4_container_2
Docker file Example and Sample Program – 5
dockerfile5
FROM ubuntu
MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get install git -y && apt-get install -yq apache2
CMD echo "Hello world"
ENTRYPOINT ["/bin/echo", "Hello"]
root@ip-172-31-28-155:/tmp/anoop# docker build -f dockerfile5 -t anoop_docker5 .
Sending build context to Docker daemon 6.144kB
Step 1/8 : FROM ubuntu
---> fb52e22af1b0
Step 2/8 : MAINTAINER Rajesh Kumar << rajesh@scmgalaxy.com>>
---> Using cache
---> 01477b86dfc8
Step 3/8 : ENV DEBIAN_FRONTEND=noninteractive
---> Using cache
---> 57a1ad7c1cea
Step 4/8 : RUN apt-get update
---> Using cache
---> 1207d5f945f3
Step 5/8 : RUN apt-get -y install tzdata
---> Using cache
---> e99c418f5399
Step 6/8 : RUN apt-get install git -y && apt-get install -yq apache2
---> Using cache
---> a10251de0aa1
Step 7/8 : CMD echo "Hello world"
---> Using cache
---> da7318408c65
Step 8/8 : ENTRYPOINT ["/bin/echo", "Hello"]
---> Using cache
---> 95fd2176a9d8
Successfully built 95fd2176a9d8
Successfully tagged anoop_docker5:latest
- Entry point in the last statement of docker layer is worked first and CMD instrcution is considered as argument
root@ip-172-31-28-155:/tmp/anoop# docker images | grep anoop_docker5
anoop_docker5 latest 95fd2176a9d8 About an hour ago 259MB
root@ip-172-31-28-155:/tmp/anoop# docker run -itd --name anoop_docker5_container anoop_docker5
e48bf86c3251636b15cab89dae573ad8fae8e9ac5f36f61b9f5eaf0dede82f44
root@ip-172-31-28-155:/tmp/anoop# docker ps | grep anoop_docker5_container
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker5_container
e48bf86c3251 anoop_docker5 "/bin/echo Hello /bi…" 17 seconds ago Exited (0) 16 seconds ago anoop_docker5_container
root@ip-172-31-28-155:/tmp/anoop# docker logs e48bf86c3251
Hello /bin/sh -c echo "Hello world"
root@ip-172-31-28-155:/tmp/anoop#
root@ip-172-31-28-155:/tmp/anoop# docker run --name anoop_docker5_container_1 anoop_docker5
Hello /bin/sh -c echo "Hello world"
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker5_container
cd4e95c49666 anoop_docker5 "/bin/echo Hello /bi…" 4 seconds ago Exited (0) 3 seconds ago anoop_docker5_container_1
e48bf86c3251 anoop_docker5 "/bin/echo Hello /bi…" 3 minutes ago Exited (0) 3 minutes ago anoop_docker5_container
- Tried to change pid1 --> CMD in docker layer is replaced with given command as part of "docker run"
root@ip-172-31-28-155:/tmp/anoop# docker run --name anoop_docker5_container_2 anoop_docker5 git --version
Hello git --version
root@ip-172-31-28-155:/tmp/anoop# docker ps -a | grep anoop_docker5_container
d9ffb519588d anoop_docker5 "/bin/echo Hello git…" 48 seconds ago Exited (0) 47 seconds ago anoop_docker5_container_2
cd4e95c49666 anoop_docker5 "/bin/echo Hello /bi…" About a minute ago Exited (0) About a minute ago anoop_docker5_container_1
e48bf86c3251 anoop_docker5 "/bin/echo Hello /bi…" 5 minutes ago Exited (0) 5 minutes ago anoop_docker5_container