Bookmark this page

Lab: Run Containers

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 systemd service and use the systemctl command 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

  1. On the serverb machine, configure the podsvc user with redhat as the password. Configure the registry.lab.example.com registry as the remote registry. Use admin as the user and redhat321 as the password to authenticate.

    1. Log in to the serverb machine as the student user.

      [student@workstation ~]$ ssh student@serverb
      ...output omitted...
      [student@serverb ~]$
    2. Create the podsvc user and set redhat as the password for the user. Use student as the password for creating the user by using the sudo command.

      [student@serverb ~]$ sudo useradd podsvc
      [sudo] password for student: student
      [student@serverb ~]$ sudo passwd podsvc
      Changing password for user podsvc.
      New password: redhat
      BAD PASSWORD: The password is shorter than 8 characters
      Retype new password: redhat
      passwd: all authentication tokens updated successfully.
    3. Return to the workstation machine as the student user.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.
      [student@workstation ~]$
    4. Log in to the serverb machine as the podsvc user. Use redhat as the password.

      [student@workstation ~]$ ssh podsvc@serverb
      ...output omitted...
      [podsvc@serverb ~]$
  2. Configure the registry.lab.example.com classroom registry in your home directory. Log in to the container registry with admin as the user and redhat321 as the password.

    1. Create the /home/podsvc/.config/containers directory.

      [podsvc@serverb ~]$ mkdir -p /home/podsvc/.config/containers
    2. Create the /home/podsvc/.config/containers/registries.conf file with the following contents:

      unqualified-search-registries = ['registry.lab.example.com']
      
      [[registry]]
      location = "registry.lab.example.com"
      insecure = true
      blocked = false
    3. Log in to the classroom registry.

      [podsvc@serverb ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
  3. Use the /home/student/webserver/html/ directory as persistent storage for the web server container. Create the index.html test page with the Welcome to the webserver container content.

    1. Create the ~/webserver/html/ directory.

      [podsvc@serverb ~]$ mkdir -p ~/webserver/html/
    2. Create the index.html file and add the Welcome to the webserver container content.

      [podsvc@serverb ~]$ echo "Welcome to the webserver container" > \
      ~/webserver/html/index.html
    3. Verify that the permission for others is set to r-x in the webserver/html directory, and is set to r-- in the index.html file. The container uses a non-privileged user that must be able to read the index.html file.

      [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
  4. Create a container that runs an Apache HTTP server in the background. Use the registry.lab.example.com/rhel9/httpd-24 image to run a detached container called webserver. Redirect the 8080 port on the local host to the container 8080 port. Mount the ~/webserver directory from the host to the /var/www directory in the container.

    1. Execute the podman run command to create the detached container. Use the registry.lab.example.com/rhel9/httpd-24 image to run a detached container called webserver. Use the -p option to map the 8080 port on the serverb machine to the 8080 port in the container. Use the -v option to mount the ~/webserver directory on the serverb machine to the /var/www directory in the container. Use the Z option 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...
      d970ff062f002a45702b96c0a51d632d93d78ccf63a3af1a01abf70bc4c46616
    2. Verify that the container is running.

      [podsvc@serverb ~]$ podman ps
      CONTAINER 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  webserver
    3. Verify that the web service is working on the 8080 port.

      [podsvc@serverb ~]$ curl http://localhost:8080
      Welcome to the webserver container
  5. Create the systemd service file to manage the webserver container with systemctl commands. Configure the systemd service so that when you start the service, the systemd daemon creates a container. The systemd daemon expects that the container does not exist initially.

    1. Create and change to the ~/.config/systemd/user/ directory.

      [podsvc@serverb ~]$ mkdir -p ~/.config/systemd/user/
      [podsvc@serverb ~]$ cd ~/.config/systemd/user
    2. Create the unit file for the webserver container. Use the --new option so that the systemd service 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.service
    3. Stop and then delete the webserver container.

      [podsvc@serverb user]$ podman stop webserver
      webserver
      [podsvc@serverb user]$ podman rm webserver
      webserver
      [podsvc@serverb user]$ podman ps -a
      CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES
  6. Reload the systemd daemon configuration, and then enable and start your new container-webserver user service. Verify that the webserver container is started and running.

    1. Reload the configuration to recognize the new unit file.

      [podsvc@serverb user]$ systemctl --user daemon-reload
    2. Enable and start the container-webserver service.

      [podsvc@serverb user]$ systemctl --user enable --now container-webserver
      Created symlink /home/podsvc/.config/systemd/user/default.target.wants/container-webserver.service → /home/podsvc/.config/systemd/user/container-webserver.service.
    3. Verify that the container is running.

      [podsvc@serverb user]$ podman ps
      CONTAINER 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
  7. Ensure that the services for the podsvc user start at system boot.

    1. Run the loginctl enable-linger command.

      [podsvc@serverb user]$ loginctl enable-linger
    2. Confirm that the Linger option is set for the podsvc user.

      [podsvc@serverb user]$ loginctl show-user podsvc
      ...output omitted...
      Linger=yes
  8. Verify that the web service is working on the 8080 port and that the content is accessible.

    [podsvc@serverb user]$ curl http://localhost:8080
    Welcome to the webserver container
  9. Return to the workstation machine as the student user.

    [podsvc@serverb ~]$ exit
    logout
    Connection to serverb closed.
    [student@workstation ~]$

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade containers-review

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish containers-review

Revision: rh199-9.3-8dd73db