Build containers that run system services by using the UBI init image. You can configure applications, such as web servers, to start automatically in the container using systemd.
Before you begin
- The
container-tools meta-package is installed.
About this task
You can build a container by using a Containerfile. When the container is running on a host system, it installs and configures a Web server (httpd) to start automatically by the systemd service (/sbin/init). The podman build command builds an image by using instructions in one or more Containerfile 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.
Procedure
-
Create a
Containerfile with 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 Containerfile installs the httpd package, enables the httpd service to start at boot time, creates a test file (index.html), exposes the Web server to the host (port 80), and starts the systemd init service (/sbin/init) when the container starts.
-
Build the container:
# podman build --format=docker -t mysysd .
-
Optional: If you want to run containers with
systemd and SELinux is enabled on your system, you must set the container_manage_cgroup boolean variable:
# setsebool -P container_manage_cgroup 1
-
Run the container named
mysysd_run:
# podman run -d --name=mysysd_run -p 80:80 mysysd
The mysysd image runs the mysysd_run container as a daemon process, with port 80 from the container exposed to port 80 on the host system.
Note
In rootless mode, you have to choose host port number >= 1024. For example:
$ podman run -d --name=mysysd -p 8081:80 mysysd
To use port numbers < 1024, you have to modify the net.ipv4.ip_unprivileged_port_start variable:
# sysctl net.ipv4.ip_unprivileged_port_start=80
-
Check that the container is running:
a282b0c2ad3d localhost/mysysd:latest /sbin/init 15 seconds ago Up 14 seconds ago 0.0.0.0:80->80/tcp mysysd_run
-
Test the web server:
# curl localhost/index.html
Successful Web Server Test