Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Manage Containers: Build a Container From a Containerfile

A Containerfile, often referred to as a Dockerfile in the context of Docker containers, is a plain text configuration file used to define the specifications and instructions for building a containerized application environment. It serves as a blueprint for creating container images by specifying the base image, desired software dependencies, configuration settings, and executable commands.

Containerfile

Although creating your own Containerfile is not an exam objective, we will be creating one here so that you can practice the exam objective of building a container from a Containerfile.

Below is a basic Containerfile for building a container image using the Red Hat Universal Base Image (UBI) as the base layer, and installing the nginx web server on top of this:

# Use the Red Hat Universal Base Image (UBI) as the base image.
FROM registry.access.redhat.com/ubi8/ubi

# Label the image with your name or any other relevant information.
LABEL maintainer="DTV Linux Student"

# Update the package repository and install Nginx.
RUN yum update -y && yum install -y nginx && yum clean all

# Expose port 80 to allow incoming HTTP traffic.
EXPOSE 80

# Start Nginx as the main process when a container based on this image is run.
CMD ["nginx", "-g", "daemon off;"]

The following provides some information on what each line in the Containerfile does:

  • FROM registry.access.redhat.com/ubi8/ubi: Specifies the base image to use, in this case, Red Hat UBI version 8.
  • LABEL maintainer="DTV Linux Student": Optional metadata to label the image with the maintainer's information.
  • RUN yum update -y && yum install -y nginx && yum clean all: This line updates the package repository, installs Nginx using the yum package manager, and then cleans up the package cache to reduce the image size.
  • EXPOSE 80: The container will listen on port 80, allowing you to map it to the host system when you run a container.
  • CMD ["nginx", "-g", "daemon off;"]: Specifies the command to run when a container is started. In this case, it starts the Nginx web server and keeps it running in the foreground.

Building a Container

To create a container start by ensuring you have a Containerfile. If you haven't already, generate a file named Containerfile in your current directory and populate it with the example content provided above.

The below exercise will get you building a container image:

Build an image called my-container using the Containerfile and tag it with version 1.0.0:

The final . in the command below ensures the current directory is used when looking for the Containerfile.

podman build -t my-container:1.0.0 .

You will see the build process occur on screen, with each STEP corresponding to an entry in the Containerfile:

STEP 1/5: FROM registry.access.redhat.com/ubi8/ubi
STEP 2/5: LABEL maintainer="Your Name <your@email.com>"
--> 271b88e8d1d
STEP 3/5: RUN yum update -y &&     yum install -y nginx &&     yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
Subscription Manager is operating in container mode.
Red Hat Enterprise Linux 8 for x86_64 - BaseOS   18 MB/s |  63 MB     00:03    
Red Hat Enterprise Linux 8 for x86_64 - AppStre  12 MB/s |  59 MB     00:05    
Red Hat Universal Base Image 8 (RPMs) - BaseOS  1.5 MB/s | 716 kB     00:00    
Red Hat Universal Base Image 8 (RPMs) - AppStre 5.5 MB/s | 2.9 MB     00:00    
Red Hat Universal Base Image 8 (RPMs) - CodeRea 257 kB/s |  99 kB     00:00    
Dependencies resolved.
Nothing to do.
Complete!
Updating Subscription Management repositories.
Unable to read consumer identity
Subscription Manager is operating in container mode.
Last metadata expiration check: 0:00:11 ago on Sat Sep  2 11:10:31 2023.
Dependencies resolved.
======================================================================================================================
 Package                       Arch    Version                                 Repository                         Size
======================================================================================================================
Installing:
 nginx                         x86_64  1:1.14.1-9.module+el8.0.0+4108+af250afe rhel-8-for-x86_64-appstream-rpms  570 k
Installing dependencies:
 dejavu-fonts-common           noarch  2.35-7.el8                              rhel-8-for-x86_64-baseos-rpms      74 k
 dejavu-sans-fonts             noarch  2.35-7.el8                              rhel-8-for-x86_64-baseos-rpms     1.5 M
 fontconfig                    x86_64  2.13.1-4.el8                            rhel-8-for-x86_64-baseos-rpms     274 k
 ...
 perl-podlators                noarch  4.11-1.el8                              rhel-8-for-x86_64-baseos-rpms     118 k
 perl-threads                  x86_64  1:2.21-2.el8                            rhel-8-for-x86_64-baseos-rpms      61 k
 perl-threads-shared           x86_64  1.58-2.el8                              rhel-8-for-x86_64-baseos-rpms      48 k
Installing weak dependencies:
 perl-IO-Socket-IP             noarch  0.39-5.el8                              rhel-8-for-x86_64-appstream-rpms   47 k
 perl-IO-Socket-SSL            noarch  2.066-4.module+el8.3.0+6446+594cad75    rhel-8-for-x86_64-appstream-rpms  298 k
 perl-Mozilla-CA               noarch  20160104-7.module+el8.3.0+6498+9eecfe51 rhel-8-for-x86_64-appstream-rpms   15 k
Enabling module streams:
 nginx                                 1.14                                                                           
 perl                                  5.26                                                                           
 perl-IO-Socket-SSL                    2.066                                                                          
 perl-libwww-perl                      6.34                                                                           

Transaction Summary
======================================================================================================================
Install  69 Packages

Total download size: 20 M
Installed size: 57 M
Downloading Packages:
(1/69): perl-Data-Dumper-2.167-399.el8.x86_64.r 155 kB/s |  58 kB     00:00    
(2/69): perl-Scalar-List-Utils-1.49-2.el8.x86_6 172 kB/s |  68 kB     00:00    
(3/69): perl-PathTools-3.74-1.el8.x86_64.rpm    222 kB/s |  90 kB     00:00    
...
(67/69): libwebp-1.0.0-8.el8_7.x86_64.rpm       1.5 MB/s | 273 kB     00:00    
(68/69): libXpm-3.5.12-9.el8_7.x86_64.rpm       215 kB/s |  58 kB     00:00    
(69/69): libtiff-4.0.9-28.el8_8.x86_64.rpm      1.1 MB/s | 189 kB     00:00    
--------------------------------------------------------------------------------
Total                                           4.2 MB/s |  20 MB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : libjpeg-turbo-1.5.3-12.el8.x86_64                     1/69 
  Installing       : openssl-1:1.1.1k-9.el8_7.x86_64                       2/69 
  Installing       : fontpackages-filesystem-1.44-22.el8.noarch            3/69 
  ...
  Verifying        : libXpm-3.5.12-9.el8_7.x86_64                         67/69 
  Verifying        : libwebp-1.0.0-8.el8_7.x86_64                         68/69 
  Verifying        : libtiff-4.0.9-28.el8_8.x86_64                        69/69 
Installed products updated.

Installed:
  dejavu-fonts-common-2.35-7.el8.noarch                                         
  dejavu-sans-fonts-2.35-7.el8.noarch                                           
  fontconfig-2.13.1-4.el8.x86_64                                                
  ...                             
  perl-podlators-4.11-1.el8.noarch                                              
  perl-threads-1:2.21-2.el8.x86_64                                              
  perl-threads-shared-1.58-2.el8.x86_64                                         

Complete!
Updating Subscription Management repositories.
Unable to read consumer identity
Subscription Manager is operating in container mode.
42 files removed
--> cf38f1d5e71
STEP 4/5: EXPOSE 80
--> ddd71af09a3
STEP 5/5: CMD ["nginx", "-g", "daemon off;"]
COMMIT my-container:1.0.0
--> 89874198298
Successfully tagged localhost/my-container:1.0.0
898741982987067c403a93369c35681a28bf9546a94390b9560a99c57db5ecff

Verify the container image was create successfully:

podman image list my-container

The command output should resemble:

REPOSITORY              TAG         IMAGE ID      CREATED            SIZE
localhost/my-container  1.0.0       27a3d9930475  About an hour ago  302 MB

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.