Red Hat System Administration II
Use podman to pull a container image from a registry, and use the image to run a detached container.
Outcomes
Create rootless detached containers.
Configure a container image registry and create a container from an existing image.
Configure port mapping and persistent storage.
Configure a container as a
systemdservice and use thesystemctlcommand to manage it.
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-review
Instructions
On the
serverbmachine, configure thepodsvcuser withredhatas the password. Configure theregistry.lab.example.comregistry as the remote registry. Useadminas the user andredhat321as the password to authenticate.Log in to the
serverbmachine as thestudentuser.[student@workstation ~]$
ssh student@serverb...output omitted... [student@serverb ~]$Create the
podsvcuser and setredhatas the password for the user. Usestudentas the password for creating the user by using thesudocommand.[student@serverb ~]$
sudo useradd podsvc[sudo] password for student:student[student@serverb ~]$sudo passwd podsvcChanging password for user podsvc. New password:redhatBAD PASSWORD: The password is shorter than 8 characters Retype new password:redhatpasswd: all authentication tokens updated successfully.Return to the
workstationmachine as thestudentuser.[student@serverb ~]$
exitlogout Connection to serverb closed. [student@workstation ~]$Log in to the
serverbmachine as thepodsvcuser. Useredhatas the password.[student@workstation ~]$
ssh podsvc@serverb...output omitted... [podsvc@serverb ~]$
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/podsvc/.config/containersdirectory.[podsvc@serverb ~]$
mkdir -p /home/podsvc/.config/containersCreate the
/home/podsvc/.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.
[podsvc@serverb ~]$
podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!
Use the
/home/student/webserver/html/directory as persistent storage for the web server container. Create theindex.htmltest page with theWelcome to the webserver containercontent.Create the
~/webserver/html/directory.[podsvc@serverb ~]$
mkdir -p ~/webserver/html/Create the
index.htmlfile and add theWelcome to the webserver containercontent.[podsvc@serverb ~]$
echo "Welcome to the webserver container" > \ ~/webserver/html/index.htmlVerify that the permission for others is set to
r-xin thewebserver/htmldirectory, and is set tor--in theindex.htmlfile. The container uses a non-privileged user that must be able to read theindex.htmlfile.[podsvc@serverb ~]$
ls -ld ~/webserver/html/drwxr-xr-x. 2 podsvc podsvc 24 Jul 10 05:42 /home/podsvc/webserver/html/ [podsvc@serverb ~]$ls -l ~/webserver/html/index.html-rw-r--r--. 1 podsvc podsvc 21 Jul 10 05:42 /home/podsvc/webserver/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 detached container calledwebserver. Redirect the8080port on the local host to the container8080port. Mount the~/webserverdirectory from the host to the/var/wwwdirectory in the container.Execute the
podman runcommand to create the detached container. Use theregistry.lab.example.com/rhel9/httpd-24image to run a detached container calledwebserver. Use the-poption to map the8080port on theserverbmachine to the8080port in the container. Use the-voption to mount the~/webserverdirectory on theserverbmachine to the/var/wwwdirectory in the container. Use theZoption to set the SELinux context to the mounted directory.[podsvc@serverb ~]$
podman run -d --name webserver -p 8080:8080 \ -v ~/webserver:/var/www:Z registry.lab.example.com/rhel9/httpd-24...output omitted... d970ff062f002a45702b96c0a51d632d93d78ccf63a3af1a01abf70bc4c46616Verify that the container is running.
[podsvc@serverb ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d970ff062f00 registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp webserverVerify that the web service is working on the
8080port.[podsvc@serverb ~]$
curl http://localhost:8080Welcome to the webserver container
Create the
systemdservice file to manage thewebservercontainer withsystemctlcommands. Configure thesystemdservice so that when you start the service, thesystemddaemon creates a container. Thesystemddaemon expects that the container does not exist initially.Create and change to the
~/.config/systemd/user/directory.[podsvc@serverb ~]$
mkdir -p ~/.config/systemd/user/[podsvc@serverb ~]$cd ~/.config/systemd/userCreate the unit file for the
webservercontainer. Use the--newoption so that thesystemdservice creates a container when starting the service, and deletes the container when stopping the service.[podsvc@serverb user]$
podman generate systemd --new --files --name webserver/home/podsvc/.config/systemd/user/container-webserver.serviceStop and then delete the
webservercontainer.[podsvc@serverb user]$
podman stop webserverwebserver [podsvc@serverb user]$podman rm webserverwebserver [podsvc@serverb user]$podman ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Reload the
systemddaemon configuration, and then enable and start your newcontainer-webserveruser service. Verify that thewebservercontainer is started and running.Reload the configuration to recognize the new unit file.
[podsvc@serverb user]$
systemctl --user daemon-reloadEnable and start the
container-webserverservice.[podsvc@serverb user]$
systemctl --user enable --now container-webserverCreated symlink /home/podsvc/.config/systemd/user/default.target.wants/container-webserver.service → /home/podsvc/.config/systemd/user/container-webserver.service.Verify that the container is running.
[podsvc@serverb user]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4425565b3192 registry.lab.example.com/rhel9/httpd-24:latest /usr/bin/run-http... 23 seconds ago Up 23 seconds 0.0.0.0:8080->8080/tcp webserver
Ensure that the services for the
podsvcuser start at system boot.Verify that the web service is working on the
8080port and that the content is accessible.Return to the
workstationmachine as thestudentuser.