10c. Build a container from a Containerfile
...

This topic includes:

  • Building a pre-written container
  • Writing a simple Containerfile
  • Buildah explanation

Commands:

  • podman build
  • podman run

Build commands
...

To build a container from an existing Containerfile, all that is needed is specifying the directory and a tag, -t, to name the build by. In the following example, we assume the container runs a server on port 80.

1
[user@localhost ~]$ ls
2
# myServer
3

4
[user@localhost ~]$ podman build -t serverImage myServer/
5

6
[user@localhost ~]$ podman images
7
# REPOSITORY TAG IMAGE ID CREATED SIZE
8
# localhost/serverImag latest a463eb6e3e4f 19 minutes ago 249 MB

This image can then be run through a docker run command. It is advisable to specify a name, though the same name cannot be used twice.

1
[user@localhost ~]$ podman run -d -p 8080:80 --name serverInstance serverImage:latest
2

3
[user@localhost ~]$ curl localhost:8080
4
# <h1>Server response</h1>

Writing a Containerfile
...

Red Hat provides a universal basic image to base your containers from. It is recommended to use it for any container-related activities within RHEL and RHCSA.

Below is a simple containerfile which pulls a standard UBI and installs a httpd server to it.

Environment:

1
ls ~/myServer/
2
index.html Containerfile
3

4
cat ~/myServer/index.html
5
# Hello World
Docker
1
FROM registry.access.redhat.com/ubi9/ubi
2
RUN dnf install -y httpd
3
EXPOSE 80
4

5
WORKDIR /var/www/html
6
COPY index.html /var/www/html/index.html
7

8
CMD httpd -D FOREGROUND

You can now build and run the Containerfile as shown above.

📝 NOTE: A Containerfile is a configuration file that automates the steps of creating a container image. It is similar to a Makefile. This flexibility allows developers to build different containers which achieve the same thing. For instance, instead of copying index.html to the workdir it could be printed directly; or the httpd execution command could be written in a different way. Whichever syntax you find most comfortable is likely fine to use.

A word on Buildah
...

Buildah is a utility which pre-dates Docker and Containerfiles. It works on a lower-level than podman build, providing finer-grained control. You can experiment with it, but for the levels of detail needed in the RHCSA learning podman and Containerfiles is likely enough.

An interesting apsect of Buildah is that it can interactively build a container line-by-line - allowing for trial and error. In production, however, these buildah command are written into a shell script and look similar to a Containerfile anyway. A section of this article is dedicated to Buildah and explains it well.