Build a container
To package your application and its required dependencies into a single, portable unit, you can build a new container in Red Hat Enterprise Linux. You define image instructions in a Containerfile to automate software installation and configuration.
Before you begin
- The
container-toolsmeta-package is installed.
Procedure
-
Install Container Tools: Ensure the necessary container tools are installed on your RHEL system. The container-tools module provides Buildah, Podman, and Skopeo.
$ sudo dnf install container-tools -
Create a Containerfile: A Containerfile defines the instructions for building your container image. This file specifies the base image, any software to install, configurations to apply, and the application to run. For example:
FROM registry.redhat.io/ubi10/ubi-minimal RUN microdnf -y update && microdnf -y install COPY index.html /var/www/html/ EXPOSE 80 CMD ["httpd", "-DFOREGROUND"] -
Build the container image with Buildah: Use
buildah bud(orpodman build) to build the image after you navigate to the directory containing your Container file.$ cd /<path_to_container_file> $ buildah bud -t your_image_name:tag .your_image_name: The name for your image.tag: The tag for your image (for example, latest, 1.0)..: Indicates that the Containerfile is in the current directory.
-
Run the container: After you build the image, you can run a container from it using the
podman runcommand.$ podman run -d -p 8080:80 my-web-app-d: Runs the container in detached mode (in the background).-p 8080:80: Maps port 8080 on the host to port 80 inside the container.my-web-app: The name of the image to run.- The heredocs syntax in container buildings
You can use the
heredocsyntax in Containerfile, with a Red Hat Enterprise Linux base image, ensuring you enableBuildKit. If the commands containheredocsyntax, the Containerfile considers the next lines, until the line only contains a heredoc delimiter, as part of the same command.You can embed multi-line strings directly within instructions such as,
RUNorCOPYin Containerfile by usingheredocs. This is useful with RHEL-based images, as it removes the need to create separate script files for simple tasks and improves readability and maintainability.For example, a common use case is running multiple shell commands in a single
RUNinstruction to create a single image layer, avoiding the&& \syntax:# syntax=container/containerfile:1.4 FROM registry.redhat.io/ubi10/ubi-minimal # Use a heredoc to perform a multi-line RUN command: RUN <<EOF microdnf -y update microdnf -y install nginx microdnf clean all echo "Nginx installed and packages updated" EOF
RUN <<EOF: The<<signals the start of the heredoc, andEOFis the user-defined delimiter.- The lines between the
<<EOFand the finalEOFare treated as a single script run by the shell. - The entire block is a single
RUNinstruction, which is more efficient and easier to read.