Red Hat System Administration II
Manage the lifecycle of a container that runs an Apache HTTP Server.
Outcomes
You should be able to:
Get detailed information about a container.
Stop containers.
Restart a stopped container.
Delete containers.
Create and mount persistent storage to containers.
Create systemd service files to manage a container.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
[student@workstation ~]$ lab start containers-lifecycle
Instructions
Your organization requires that a web service that runs in a container should be a service that is managed by systemd.
Configure a user account for systemd user services to start the container when the host machine starts.
Log in to the
serveramachine as thecontsvcuser. Useredhatas password.To manage the
systemduser services with thecontsvcaccount, you must log in directly as thecontsvcuser. You cannot use thesuandsudocommands to create a session with thecontsvcuser.[student@workstation ~]$
ssh contsvc@servera...output omitted... [contsvc@servera ~]$
Configure the
registry.lab.example.comclassroom registry in your home directory. Log in to the container registry withadminas the user andredhat321as the password.Create the
/home/contsvc/.config/containersdirectory.[contsvc@servera ~]$
mkdir -p /home/contsvc/.config/containersCreate the
/home/contsvc/.config/containers/registries.conffile with the following contents:unqualified-search-registries = ['registry.lab.example.com'] [[registry]] location = "registry.lab.example.com" insecure = true blocked = false
Log in to the classroom registry.
[contsvc@servera ~]$
podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!
Use the
/home/contsvc/webcontent/html/directory as persistent storage for the web server container. Create theindex.htmltest page with theHello Worldline inside the directory.Create the
~/webcontent/html/directory.[contsvc@servera ~]$
mkdir -p ~/webcontent/html/Create the
index.htmlfile and add theHello Worldline.[contsvc@servera ~]$
echo "Hello World" > ~/webcontent/html/index.htmlVerify that the permission for others is set to
r-xin thewebcontent/htmldirectory, and is set tor--in theindex.htmlfile. The container uses a non-privileged user that must be able to read theindex.htmlfile.[contsvc@servera ~]$
ls -ld webcontent/html/drwxr-xr-x. 2 contsvc contsvc 24 Jun 6 19:12 webcontent/html/ [contsvc@servera ~]$ls -l webcontent/html/index.html-rw-r--r--. 1 contsvc contsvc 12 Jun 6 19:12 webcontent/html/index.html
Create a container that runs an Apache HTTP server in the background. Use the
registry.lab.example.com/rhel9/httpd-24image to run a container calledwebappin detached mode. Redirect the8090port on the local host to the container8080port. Mount the~/webcontentdirectory from the host to the/var/www directoryin the container.Execute the
podman runcommand to create the container. Use theregistry.lab.example.com/rhel9/httpd-24image to run a container calledwebappin detached mode. Use the-poption to map the8090port on servera to the8080port in the container. Use the-voption to mount the~/webcontentdirectory onserverato the/var/wwwdirectory in the container. Use theZoption to set the SELinux context to the mounted directory.[contsvc@servera ~]$
podman run -d --name webapp -p 8090:8080 \ -v ~/webcontent:/var/www:Z registry.lab.example.com/rhel9/httpd-24Trying to pull registry.lab.example.com/rhel9/httpd-24:latest... ...output omitted... 79ed4591cb59bdfba1d38badcf52289ee70213bafd7a1a2aefa4d2963cf88e29Verify that the container is running. Use
podman psto list all the running containers.[contsvc@servera ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79ed4591cb59 registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... 3 minutes ago Up 3 minutes 0.0.0.0:8090->8080/tcp webappUse the
podman inspectcommand to get theStatusfield, which indicates whether the container is running.[contsvc@servera ~]$
podman inspect --format='{{.State.Status}}' webapprunningVerify that the container is running by using the
Runningfield.[contsvc@servera ~]$
podman inspect --format='{{.State.Running}}' webapptrueVerify that the web service is working on port
8090.[contsvc@servera ~]$
curl http://localhost:8090Hello World
Before creating a systemd unit file for the service container, test the functionality of the
webappcontainer.Stop the container. Use the container name to stop the container.
[contsvc@servera ~]$
podman stop webappwebappVerify that the container is not running.
[contsvc@servera ~]$
podman inspect --format='{{.State.Status}}' webappexited [contsvc@servera ~]$podman inspect --format='{{.State.Running}}' webappfalseRestart the container. Use the
podman restartcommand to restart the container.[student@workstation ~]$
podman restart webappwebappVerify that the container is running again.
[contsvc@servera ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79ed4591cb59 registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... 20 minutes ago Up 29 seconds 0.0.0.0:8090->8080/tcp webapp [contsvc@servera ~]$curl http://localhost:8090Hello World
Create a
systemdservice file to manage thewebappcontainer withsystemctlcommands. Configure thesystemdservice so that when you start the service, thesystemddaemon creates a container. After you finish the configuration, stop and then delete thewebappcontainer. Remember that thesystemddaemon expects that the container does not exist initially.Create and change to the
~/.config/systemd/user/directory.[contsvc@servera ~]$
mkdir -p ~/.config/systemd/user/[contsvc@servera ~]$cd ~/.config/systemd/userCreate the unit file for the
webappcontainer. Use the--newoption so thatsystemdcreates a container when starting the service, and deletes the container when stopping the service.[contsvc@servera user]$
podman generate systemd --new --files --name webapp/home/contsvc/.config/systemd/user/container-webapp.serviceStop and then delete the
webappcontainer.[contsvc@servera user]$
podman stop webappwebapp [contsvc@servera user]$podman rm webappwebapp [contsvc@servera user]$podman ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Reload the
systemddaemon configuration, and then enable and start your newcontainer-webappuser service. Verify thesystemdservice configuration, stop and start the service, and display the web server response and the container status.Reload the configuration to recognize the new unit file.
[contsvc@servera user]$
systemctl --user daemon-reloadEnable and start the
container-webappservice.[contsvc@servera user]$
systemctl --user enable --now container-webappCreated symlink /home/contsvc/.config/systemd/user/default.target.wants/container-webapp.service → /home/contsvc/.config/systemd/user/container-webapp.service.Verify that the web server responds to requests.
[contsvc@servera user]$
curl http://localhost:8090Hello WorldVerify that the container is running.
[contsvc@servera user]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf6b209f0c915registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... About a minute ago Up About a minute 0.0.0.0:8090->8080/tcp webappUse the container ID information to confirm that the
systemddaemon creates a container when you restart the service.Stop the
container-webappservice, and confirm that the container no longer exists. When you stop the service, thesystemddaemon stops and then deletes the container.[contsvc@servera user]$
systemctl --user stop container-webapp[contsvc@servera user]$podman ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESStart the
container-webappservice, and then confirm that the container is running.The container ID is different, because the
systemddaemon creates a container with the start instruction, and deletes the container with the stop instruction.[contsvc@servera user]$
systemctl --user start container-webapp[contsvc@servera user]$podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf9488c4e4c9eregistry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... 5 seconds ago Up 6 seconds 0.0.0.0:8090->8080/tcp webapp
Ensure that the services for the
contsvcuser start at system boot. When done, restart theserveramachine.Run the
loginctl enable-lingercommand.[contsvc@servera user]$
loginctl enable-lingerConfirm that the
Lingeroption is set for thecontsvcuser.[contsvc@servera user]$
loginctl show-user contsvc...output omitted...Linger=yesSwitch to the
rootuser, and then use thesystemctl rebootcommand to restart theserveramachine.[contsvc@servera user]$
su -Password:redhatLast login: Wed Apr 17 07:44:06 EDT 2024 on tty2 [root@servera ~]#systemctl rebootConnection to servera closed by remote host. Connection to servera closed. [student@workstation ~]$
When the
serveramachine is running again, log in toserveraas thecontsvcuser. Verify that thesystemddaemon started thewebappcontainer, and that the web content is available.Log in to the
serveramachine as thecontsvcuser.[student@workstation ~]$
ssh contsvc@serveraVerify that the container is running.
[contsvc@servera ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 694eb00c7583 registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... 2 minutes ago Up 2 minutes 0.0.0.0:8090->8080/tcp webappAccess the web content.
[contsvc@servera ~]$
curl http://localhost:8090Hello WorldReturn to the
workstationmachine as thestudentuser.[contsvc@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$