This topic includes:
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# myServer3
4[user@localhost ~]$ podman build -t serverImage myServer/5
6[user@localhost ~]$ podman images7# REPOSITORY TAG IMAGE ID CREATED SIZE8# 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:latest2
3[user@localhost ~]$ curl localhost:80804# <h1>Server response</h1>
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:
1ls ~/myServer/2index.html Containerfile3
4cat ~/myServer/index.html5# Hello World
Docker
1FROM registry.access.redhat.com/ubi9/ubi2RUN dnf install -y httpd3EXPOSE 8045WORKDIR /var/www/html6COPY index.html /var/www/html/index.html78CMD 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.
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.