RHCSA Rapid Track
Course update
An updated version of this course is available that uses a newer version of Red Hat Enterprise Linux in the lab environment. Therefore, the RHEL 9.0 version of the lab environment will retire on December 31, 2024. Please complete any work in this lab environment before it is removed on December 31, 2024. For the most up-to-date version of this course, we recommend moving to the RHEL 9.3 version.
In this exercise, you configure a container to manage it as a systemd service, and use systemctl commands to manage that container so that it automatically starts when the host machine starts.
Outcomes
Create
systemdservice files to manage a container.Configure a container so you can manage it with
systemctlcommands.Configure a user account for
systemduser services to start a container when the host machine starts.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start containers-services
Instructions
Log in to the
serveramachine as thestudentuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$Create a user account called
contsvcand useredhatas the password. Use this user account to run containers assystemdservices.Create the
contsvcuser. Setredhatas the password for thecontsvcuser.[student@servera ~]$
sudo useradd contsvc[sudo] password for student:student[student@servera ~]$sudo passwd contsvcChanging password for user contsvc. New password:redhatBAD PASSWORD: The password is shorter than 8 characters Retype new password:redhatpasswd: all authentication tokens updated successfully.To manage the
systemduser services with thecontsvcaccount, you must log in directly as thecontsvcuser. You cannot use thesuandsudocommands to create a session with thecontsvcuser.Return to the
workstationmachine as thestudentuser, and then log in as thecontsvcuser.[student@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$ssh contsvc@servera...output omitted... [contsvc@servera ~]$
Configure access to the
registry.lab.example.comclassroom registry in your home directory. Use the/tmp/containers-services/registries.conffile as a template.Create the
~/.config/containers/directory.[contsvc@servera ~]$
mkdir -p ~/.config/containers/The
labscript prepares theregistries.conffile in the/tmp/containers-services/directory. Copy that file to the~/.config/containers/directory.[contsvc@servera ~]$
cp /tmp/containers-services/registries.conf \ ~/.config/containers/Verify that you can access the
registry.lab.example.comregistry. If everything works as expected, then the command should list some images.[contsvc@servera ~]$
podman search ubiNAME DESCRIPTION registry.lab.example.com/ubi7/ubi registry.lab.example.com/ubi8/ubi registry.lab.example.com/ubi9-beta/ubi
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 Aug 28 04:56 webcontent/html/ [contsvc@servera ~]$ls -l webcontent/html/index.html-rw-r--r--. 1 contsvc contsvc 12 Aug 28 04:56 webcontent/html/index.html
Use the
registry.lab.example.com/rhel8/httpd-24:1-163image to run a container calledwebappin detached mode. Redirect the 8080 port on the local host to the container 8080 port. Mount the~/webcontentdirectory from the host to the/var/wwwdirectory in the container.Log in to the
registry.lab.example.comregistry as theadminuser withredhat321as the password.[contsvc@servera ~]$
podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!Use the
registry.lab.example.com/rhel8/httpd-24:1-163image to run a container calledwebappin detached mode. Use the-poption to map the8080port onserverato the8080port in the container. Use the-voption to mount the~/webcontentdirectory onserverato the/var/wwwdirectory in the container.[contsvc@servera ~]$
podman run -d --name webapp -p 8080:8080 -v \~/webcontent:/var/www:Z registry.lab.example.com/rhel8/httpd-24:1-163750a681bd37cb6825907e9be4347eec2c4cd79550439110fc6d41092194d0e06 ...output omitted...Verify that the web service is working on port 8080.
[contsvc@servera ~]$
curl http://localhost:8080Hello 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 --name webapp --files --new/home/contsvc/.config/systemd/user/container-webapp.serviceStop and then delete the
webappcontainer.[contsvc@servera user]$
podman stop webappwebapp [contsvc@servera user]$podman rm webapp750a681bd37cb6825907e9be4347eec2c4cd79550439110fc6d41092194d0e06 [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:8080Hello WorldVerify that the container is running.
[contsvc@servera user]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e996db98071registry.access.redhat.com/ubi8/httpd-24:1-163 /usr/bin/run-http... 3 minutes ago Up 3 minutes ago 0.0.0.0:8080->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 NAMES4584b4df514cregistry.access.redhat.com/ubi8/httpd-24:1-163 /usr/bin/run-http... 6 seconds ago Up 7 seconds ago 0.0.0.0:8080->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 restartservera.[contsvc@servera user]$
su -Password:redhatLast login: Fri Aug 28 07:43:40 EDT 2020 on pts/0 [root@servera ~]#systemctl rebootConnection to servera closed by remote host. Connection to servera closed. [student@workstation ~]$
When the
serveramachine is up again, log in toserveraas thecontsvcuser. Verify that thesystemddaemon started thewebappcontainer, and that the web content is available.Log in to
serveraas thecontsvcuser.[student@workstation ~]$
ssh contsvc@servera...output omitted...Verify that the container is running.
[contsvc@servera ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c325bf49f84 registry.access.redhat.com/ubi8/httpd-24:1-163 /usr/bin/run-http... 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->8080/tcp webappAccess the web content.
[contsvc@servera ~]$
curl http://localhost:8080Hello WorldReturn to the
workstationmachine as thestudentuser.[contsvc@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.