第 7 章 Adding software to a UBI container
Red Hat Universal Base Images (UBIs) are built from a subset of the RHEL content. UBIs also provide a subset of RHEL packages that are freely available to install for use with UBI. To add or update software to a running container, you can use the DNF repositories that include RPM packages and updates. UBIs provide a set of pre-built language runtime container images such as Python, Perl, Node.js, Ruby, and so on.
To add packages from UBI repositories to running UBI containers:
-
On UBI init and UBI standard images, use the
dnfcommand -
On UBI minimal images, use the
microdnfcommand
Installing and working with software packages directly in running containers adds packages temporarily. The changes are not saved in the container image. To make package changes persistent, see section Building an image from a Containerfile with Buildah.
7.1. Using the UBI init images 复制链接链接已复制到粘贴板!
You can build a container by using a Containerfile that installs and configures a Web server (httpd) to start automatically by the systemd service (/sbin/init) when the container is run on a host system. The podman build command builds an image by using instructions in one or more Containerfiles and a specified build context directory. The context directory can be specified as the URL of an archive, Git repository or Containerfile. If no context directory is specified, then the current working directory is considered as the build context, and must contain the Containerfile. You can also specify a Containerfile with the --file option.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Create a
Containerfilewith the following contents to a new directory:FROM registry.access.redhat.com/ubi10/ubi-init RUN dnf -y install httpd; dnf clean all; systemctl enable httpd; RUN echo "Successful Web Server Test" > /var/www/html/index.html RUN mkdir /etc/systemd/system/httpd.service.d/; echo -e '[Service]\nRestart=always' > /etc/systemd/system/httpd.service.d/httpd.conf EXPOSE 80 CMD [ "/sbin/init" ]The
Containerfileinstalls thehttpdpackage, enables thehttpdservice to start at boot time, creates a test file (index.html), exposes the Web server to the host (port 80), and starts thesystemdinit service (/sbin/init) when the container starts.Build the container:
# podman build --format=docker -t mysysd .Optional: If you want to run containers with
systemdand SELinux is enabled on your system, you must set thecontainer_manage_cgroupboolean variable:# setsebool -P container_manage_cgroup 1Run the container named
mysysd_run:# podman run -d --name=mysysd_run -p 80:80 mysysdThe
mysysdimage runs as themysysd_runcontainer as a daemon process, with port 80 from the container exposed to port 80 on the host system.注意In rootless mode, you have to choose host port number >= 1024. For example:
$ podman run -d --name=mysysd -p 8081:80 mysysdTo use port numbers < 1024, you have to modify the
net.ipv4.ip_unprivileged_port_startvariable:# sysctl net.ipv4.ip_unprivileged_port_start=80Check that the container is running:
# podman ps a282b0c2ad3d localhost/mysysd:latest /sbin/init 15 seconds ago Up 14 seconds ago 0.0.0.0:80->80/tcp mysysd_runTest the web server:
# curl localhost/index.html Successful Web Server Test