5.4. Building a container


Buildah is the primary tool for building containers in the Red Hat Enterprise Linux, and you can use it with Podman to manage and run the containers you build.

Prerequisites

  1. The container-tools meta-package is installed.

Procedure

  1. 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
  2. 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"]
  3. Build the container image with Buildah: Use buildah bud (or podman 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 (e.g., latest, 1.0).
    • .: Indicates that the Containerfile is in the current directory.
  4. Run the container: After you build the image, you can run a container from it using the podman run command.

    $ 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 heredoc syntax in Containerfile, with a Red Hat Enterprise Linux base image, ensuring you enable BuildKit. If the commands contain heredoc syntax, 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 like RUN or COPY in Containerfile using heredocs. This is especially useful with RHEL-based images, as it removes the need to create separate script files for simple tasks and thus improves readability and maintainability.

      For Example, a common use case is running multiple shell commands in a single RUN instruction 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, and EOF is the user-defined delimiter.
  • The lines between the <<EOF and the final EOF are treated as a single script executed by the shell.
  • The entire block is a single RUN instruction, which is more efficient and easier to read.
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る