Cyberithub

How to Build Docker Image from DockerFile in CentOS 8 with Best Example

Advertisements

In the previous article, we have seen the steps to build docker image from dockerfile in CentOS 7 with Best Example and managing ports in docker. Here we will look into the steps to build docker image from dockerfile in CentOS 8. As you might be aware by now that you can easily create a docker image by using simple set of instructions through dockerfile. So we will move ahead and see the instructions and steps required to build docker image from dockerfile in CentOS 8.

What is Docker Image

A docker image is more like a template which is used to create a runtime called container that runs on underlying docker platform. To build docker image from dockerfile, it requires read only set of instructions.

What is Docker Container

A docker container is a lightweight runtime of docker image which contains all the resources required to run applications in it. One container is isolated from another container and it contains its own libraries and configuration files.

How to Build Docker Image from DockerFile in CentOS 8 with Best Example

Steps to Build Docker Image from DockerFile in CentOS 8

Also Read: How to Manage Ports in Docker(v1.13)

Step 1: Prerequisites

a)You should have a running CentOS 8 Server.

b)You should have dnf and docker utility installed in the Server. You can check 31 Useful DNF Command Examples for RPM Package Management in Fedora/RHEL/CentOS to know more about dnf command.

c)You must have root access or sudo access to run privileged commands. Please Check How to Add User to Sudoers to know more about providing sudo access to the User.

Step 2: Write Your Docker File

In the first step to build docker image from dockerfile you need to first identify the steps that you need to take. In this example, we are first updating and then upgrading the CentOS 8 Server and then installing epel-release package as nginx is available from EPEL Repository. In the next sequence of steps, we are installing nginx package and informing docker to assign Port 80 for container runtime to Listen. Since we are creating our dockerfile1 in /home/centos directory so we need to first change our directory using cd /home/centos command.

[root@localhost ~]# cd /home/centos
[root@localhost centos]# vi dockerfile1
FROM centos:8

LABEL maintainer="admin@example1.com"
RUN dnf update -y
RUN dnf upgrade -y
RUN dnf install epel-release -y
RUN dnf install nginx -y
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

FROM : This instruction initializes a new build stage and sets the Base Image for subsequent instructions.

LABEL : This instruction adds metadata to a Image.

RUN : This instruction will execute any commands in a new layer on top of the current image and commit the results.

EXPOSE : This instruction informs Docker that the container listens on the specified network ports at runtime.

CMD : It is used to provide the default arguments for ENTRYPOINT instruction. More on docker official documentation.

Step 3: Build Your Docker Image

Now to build the above docker file you need to run docker build -t nginx-image:1.0.0 -f dockerfile1 /home/centos command. You can also specify .(dot) when using dockerfile from current directory but if it is in some other directory then you need to specify the complete path. Also -f option is only required when you are not using the default dockerfile name.

[root@localhost centos]# docker build -t nginx-image:1.0.0 -f dockerfile1 /home/centos
STEP 1: FROM centos:8
Getting image source signatures
Copying blob 7a0437f04f83 done
Copying config 300e315adb done
Writing manifest to image destination
Storing signatures
STEP 2: LABEL maintainer="admin@example1.com"
--> 00c6e435bbd
STEP 3: RUN dnf update -y
CentOS Linux 8 - AppStream 3.7 MB/s | 6.3 MB 00:01
CentOS Linux 8 - BaseOS 2.1 MB/s | 2.3 MB 00:01
CentOS Linux 8 - Extras 17 kB/s | 8.6 kB 00:00
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Upgrading:
curl x86_64 7.61.1-14.el8_3.1 baseos 353 k
gnutls x86_64 3.6.14-7.el8_3 baseos 1.0 M
iptables-libs x86_64 1.8.4-15.el8_3.3 baseos 106 k
kexec-tools x86_64 2.0.20-34.el8_3.1 baseos 496 k
libcurl-minimal x86_64 7.61.1-14.el8_3.1 baseos 285 k
openssl-libs x86_64 1:1.1.1g-12.el8_3 baseos 1.5 M
systemd x86_64 239-41.el8_3.1 baseos 3.5 M
systemd-libs x86_64 239-41.el8_3.1 baseos 1.1 M
systemd-pam x86_64 239-41.el8_3.1 baseos 456 k
systemd-udev x86_64 239-41.el8_3.1 baseos 1.3 M
tzdata noarch 2020f-1.el8 baseos 473 k
Installing dependencies:
kbd-legacy noarch 2.0.4-10.el8 baseos 481 k
kbd-misc noarch 2.0.4-10.el8 baseos 1.5 M
openssl x86_64 1:1.1.1g-12.el8_3 baseos 707 k
trousers-lib x86_64 0.3.14-4.el8 baseos 169 k
xkeyboard-config noarch 2.28-1.el8 appstream 782 k
Installing weak dependencies:
diffutils x86_64 3.6-6.el8 baseos 358 k
kbd x86_64 2.0.4-10.el8 baseos 390 k
libxkbcommon x86_64 0.9.1-1.el8 appstream 116 k
openssl-pkcs11 x86_64 0.4.10-2.el8 baseos 66 k
trousers x86_64 0.3.14-4.el8 baseos 153 k

Transaction Summary
========================================================================================================================================================================
Install 10 Packages
Upgrade 11 Packages

Total download size: 15 M


.............................................................................................................................................................................................................................................

Installed:
dejavu-fonts-common-2.35-6.el8.noarch dejavu-sans-fonts-2.35-6.el8.noarch
fontconfig-2.13.1-3.el8.x86_64 fontpackages-filesystem-1.44-22.el8.noarch
freetype-2.9.1-4.el8_3.1.x86_64 gd-2.2.5-7.el8.x86_64
jbigkit-libs-2.1-14.el8.x86_64 libX11-1.6.8-3.el8.x86_64
libX11-common-1.6.8-3.el8.noarch libXau-1.0.9-3.el8.x86_64
libXpm-3.5.12-8.el8.x86_64 libjpeg-turbo-1.5.3-10.el8.x86_64
libpng-2:1.6.34-5.el8.x86_64 libtiff-4.0.9-18.el8.x86_64
libwebp-1.0.0-1.el8.x86_64 libxcb-1.13.1-1.el8.x86_64
libxslt-1.1.32-5.el8.x86_64 nginx-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-all-modules-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch
nginx-mod-http-image-filter-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 nginx-mod-http-perl-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-mod-http-xslt-filter-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 nginx-mod-mail-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-mod-stream-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 perl-Carp-1.42-396.el8.noarch
perl-Errno-1.28-416.el8.x86_64 perl-Exporter-5.72-396.el8.noarch
perl-File-Path-2.15-2.el8.noarch perl-IO-1.38-416.el8.x86_64
perl-PathTools-3.74-1.el8.x86_64 perl-Scalar-List-Utils-3:1.49-2.el8.x86_64
perl-Socket-4:2.027-3.el8.x86_64 perl-Text-Tabs+Wrap-2013.0523-395.el8.noarch
perl-Unicode-Normalize-1.25-396.el8.x86_64 perl-constant-1.33-396.el8.noarch
perl-interpreter-4:5.26.3-416.el8.x86_64 perl-libs-4:5.26.3-416.el8.x86_64
perl-macros-4:5.26.3-416.el8.x86_64 perl-parent-1:0.237-1.el8.noarch
perl-threads-1:2.21-2.el8.x86_64 perl-threads-shared-1.58-2.el8.x86_64

Complete!
--> 5e26eec610a
STEP 7: EXPOSE 80
--> a3a569d83c9
STEP 8: CMD ["nginx", "-g", "daemon off;"]
STEP 9: COMMIT nginx-image:1.0.0
--> 9997cf74ec8
9997cf74ec8a82112f5138577473bd9172bb6a6cad46d4e07ef04dfa3fe9eb35

Step 4: Check Your Build Image

If everything goes well then you can check and verify the images by using docker images command. You can see all the image related information like Repository name, tag, Image ID, size on the output.

[root@localhost centos]# docker images
REPOSITORY                TAG     IMAGE ID      CREATED      SIZE
localhost/nginx-image    1.0.0  9997cf74ec8a  3 minutes ago 855 MB
docker.io/library/centos   8    300e315adb2f  6 weeks ago   217 MB

Step 5: Delete Your Image

Once you build docker image from dockerfile you might want to just use it and then remove later on. You can easily delete all the build images using its Image ID. Here we are deleting Image ID 9997cf74ec8a and 300e315adb2f using docker image rm 9997cf74ec8a 300e315adb2f command as shown below.

[root@localhost centos]# docker image rm 9997cf74ec8a 300e315adb2f
a3a569d83c9f8d45b73c41f1f61f7a47be3262417f80b9baaacafcb0b07c16ff
5e26eec610ad82e2cef1760bc4bd76ccf5661da8590e795b6f9f8a673ccefea7
042a2b482562d52a8d561bb184bc72d3abe8fab86277f7b33497e664774906a3
69aa39c42cebad8367eefe4dbbfb76b6b73aaef811a25e31997e03a6681dd32f
825d3e7542efa5e03e187cbe07cf0b1976b74a0077c7f4bacb55c0b092ac5a7d
00c6e435bbdc21fe2aea98777d11196f4e12d3c22e75a54555c834ff0189929e
Untagged: localhost/nginx-image:1.0.0
Untagged: docker.io/library/centos:8
Deleted: 9997cf74ec8a82112f5138577473bd9172bb6a6cad46d4e07ef04dfa3fe9eb35
Deleted: 300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55

If you check the images again by using docker images command then you can see that all the images are deleted now.

[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

Leave a Comment