Bookmark this page

Lab: Run Containers

Note

If you plan to take the RHCSA exam, then use the following approach to maximize the benefit of this Comprehensive Review: attempt each lab without viewing the solution buttons or referring to the course content. Use the grading scripts to gauge your progress as you complete each lab.

Create rootless detached containers.

Outcomes

  • Create rootless detached containers.

  • Configure port mapping and persistent storage.

  • Configure a container as a systemd service and use systemctl commands to manage it.

If you did not reset your workstation and server machines at the end of the last chapter, then save any work that you want to keep from earlier exercises on those machines, and reset them now.

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 rhcsa-compreview4

Specifications

  • On serverb, configure the podmgr user with redhat as the password, and set up the appropriate tools for the podmgr user to manage the containers for this comprehensive review. Configure registry.lab.example.com as a remote registry. Use admin as the user and redhat321 as the password to authenticate to the registry. You can use the /tmp/review4/registries.conf file to configure the registry.

  • The /tmp/review4/container-dev directory contains two directories with development files for the containers in this comprehensive review. Copy the two directories under the /tmp/review4/container-dev directory to the podmgr home directory. Configure the /home/podmgr/storage/database subdirectory so that you can use it as persistent storage for a container.

  • Create the db-app01 detached container based on the registry.lab.example.com/rhel9/mariadb-105 container image. Use the /home/podmgr/storage/database directory as persistent storage for the /var/lib/mysql/data directory of the db-app01 container. Map the 13306 port on the local machine to the 3306 port in the container. Use the values of the following table to set the environment variables to create the containerized database:

    VariableValue
    MYSQL_USER developer
    MYSQL_PASSWORD redhat
    MYSQL_DATABASE inventory
    MYSQL_ROOT_PASSWORD redhat
  • Create a systemd service file to manage the db-app01 container. Configure the systemd service so that when you start the service, the systemd daemon keeps the original container. Start and enable the container as a systemd service. Configure the db-app01 container to start at system boot.

  • Copy the /home/podmgr/db-dev/inventory.sql script into the /tmp directory of the db-app01 container, and execute the script inside the container. If you executed the script locally, then you would use the mysql -u root inventory < /tmp/inventory.sql command.

  • Use the container file in the /home/podmgr/http-dev directory to create the http-app01 detached container. The container image name must be http-client. Map the 8080 port on the local machine to the 8080 port in the container.

  • Use the curl command to query the content of the http-app01 container. Verify that the output of the command shows the container name of the client and that the status of the database is up.

  1. On serverb, configure the podmgr user with redhat as the password and set up the appropriate tools for the podmgr user to manage the containers for this comprehensive review. Configure the registry.lab.example.com as the remote registry. Use admin as the user and redhat321 as the password to authenticate. You can use the /tmp/review4/registries.conf file to configure the registry.

    1. Log in to serverb as the student user.

      [student@workstation ~]$ ssh student@serverb
      ...output omitted...
      [student@serverb ~]$
    2. Install the container-tools meta-package.

      [student@serverb ~]$ sudo dnf install container-tools
      [sudo] password for student: student
      ...output omitted...
      Is this ok [y/N]: y
      ...output omitted...
      Complete!
    3. Create the podmgr user and set redhat as the password for the user.

      [student@serverb ~]$ sudo useradd podmgr
      [student@serverb ~]$ sudo passwd podmgr
      Changing password for user podmgr.
      New password: redhat
      BAD PASSWORD: The password is shorter than 8 characters
      Retype new password: redhat
      passwd: all authentication tokens updated successfully.
    4. Exit the student user session. Log in to the serverb machine as the podmgr user. If prompted, use redhat as the password.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.
      [student@workstation ~]$ ssh podmgr@serverb
      ...output omitted...
      [podmgr@serverb ~]$
    5. Create the ~/.config/containers directory.

      [podmgr@serverb ~]$ mkdir -p ~/.config/containers
    6. Copy the /tmp/review4/registries.conf file to the container configuration directory in the home directory.

      [podmgr@serverb ~]$ cp /tmp/review4/registries.conf ~/.config/containers/
    7. Log in to the registry to verify the configuration.

      [podmgr@serverb ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
  2. The /tmp/review4/container-dev directory contains two directories with development files for the containers in this comprehensive review. Copy the two directories in the /tmp/review4/container-dev directory to the podmgr home directory. Configure the /home/podmgr/storage/database subdirectory so that you can use it as persistent storage for a container.

    1. Copy the content of the /tmp/review4/container-dev directory to the podmgr home directory.

      [podmgr@serverb ~]$ cp -r /tmp/review4/container-dev/*-dev .
      [podmgr@serverb ~]$ ls -l
      total 0
      drwxr-xr-x. 2 podmgr podmgr 27 Jun 18 12:00 db-dev
      drwxr-xr-x. 3 podmgr podmgr 42 Jun 18 12:00 http-dev
    2. Create the /home/podmgr/storage/database directory in the podmgr home directory. Set the appropriate permissions on the directory for the container to mount it as persistent storage.

      [podmgr@serverb ~]$ mkdir -p storage/database
      [podmgr@serverb ~]$ chmod 0777 storage/database
      [podmgr@serverb ~]$ ls -l storage/
      total 0
      drwxrwxrwx. 2 podmgr podmgr 6 Jun 18 12:05 database
  3. Create the db-app01 detached container based on the registry.lab.example.com/rhel9/mariadb-105 container image. Use the /home/podmgr/storage/database directory as persistent storage for the /var/lib/mysql/data directory of the db-app01 container. Map the 13306 port on the local machine to the 3306 port in the container. Use the values of the following table to set the environment variables to create the containerized database:

    VariableValue
    MYSQL_USER developer
    MYSQL_PASSWORD redhat
    MYSQL_DATABASE inventory
    MYSQL_ROOT_PASSWORD redhat
    1. Inspect the registry.lab.example.com/rhel9/mariadb container image.

      [podmgr@serverb ~]$ skopeo inspect \
      docker://registry.lab.example.com/rhel9/mariadb-105
      {
          "Name": "registry.lab.example.com/rhel9/mariadb-105",
          "Digest": "sha256:2bf8...fab8",
          "RepoTags": [
              "latest"
          ],
      ...output omitted...
    2. Use the registry.lab.example.com/rhel9/mariadb-105 container image to create the detached db-app01 container. Use the /home/podmgr/storage/database directory as persistent storage for the container. Map the 13306 port to the 3306 container port. Use the data in the table to set the environment variables for the container.

      [podmgr@serverb ~]$ podman run -d --name db-app01 \
      -e MYSQL_USER=developer \
      -e MYSQL_PASSWORD=redhat \
      -e MYSQL_DATABASE=inventory \
      -e MYSQL_ROOT_PASSWORD=redhat \
      -p 13306:3306 \
      -v /home/podmgr/storage/database:/var/lib/mysql/data:Z \
      registry.lab.example.com/rhel9/mariadb-105
      ...output omitted...
      0959ba5df57dec2a478504df633a8cdb54aaad5c98a160de34d1d1b65f8435a6
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                              COMMAND     CREATED         STATUS         PORTS                    NAMES
      0959ba5df57d  registry.lab.example.com/rhel9/mariadb-105:latest  run-mysqld  16 seconds ago  Up 15 seconds  0.0.0.0:13306->3306/tcp  db-app01
  4. Create a systemd service file to manage the db-app01 container. Configure the systemd service so that when you start the service, the systemd daemon keeps the original container. Start and enable the container as a systemd service. Configure the db-app01 container to start at system boot.

    1. Create the ~/.config/systemd/user/ directory for the container unit file.

      [podmgr@serverb ~]$ mkdir -p ~/.config/systemd/user/
    2. Create the systemd unit file for the db-app01 container, and move the unit file to the ~/.config/systemd/user/ directory.

      [podmgr@serverb ~]$ podman generate systemd --name db-app01 --files
      /home/podmgr/container-db-app01.service
      [podmgr@serverb ~]$ mv container-db-app01.service ~/.config/systemd/user/
    3. Stop the db-app01 container.

      [podmgr@serverb ~]$ podman stop db-app01
      db-app01
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                              COMMAND     CREATED        STATUS                    PORTS                    NAMES
      0959ba5df57d  registry.lab.example.com/rhel9/mariadb-105:latest  run-mysqld  2 minutes ago  Exited (0) 2 seconds ago  0.0.0.0:13306->3306/tcp  db-app01
    4. Reload the user systemd service to use the new service unit.

      [podmgr@serverb ~]$ systemctl --user daemon-reload
    5. Start and enable the systemd unit for the db-app01 container.

      [podmgr@serverb ~]$ systemctl --user enable --now container-db-app01
      Created symlink /home/podmgr/.config/systemd/user/default.target.wants/container-db-app01.service → /home/podmgr/.config/systemd/user/container-db-app01.service.
      [podmgr@serverb ~]$ systemctl --user status container-db-app01
      ● container-db-app01.service - Podman container-db-app01.service
           Loaded: loaded (/home/podmgr/.config/systemd/user/container-db-app01.service; enabled; preset: disabled)
           Active: active (running) since Fri 2024-06-18 12:18:45 EDT; 13s ago
      ...output omitted...
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                              COMMAND     CREATED        STATUS        PORTS                    NAMES
      0959ba5df57d  registry.lab.example.com/rhel9/mariadb-105:latest  run-mysqld  2 minutes ago  Up 2 minutes  0.0.0.0:13306->3306/tcp  db-app01
    6. Use the loginctl command to configure the db-app01 container to start at system boot.

      [podmgr@serverb ~]$ loginctl enable-linger
  5. Copy the /home/podmgr/db-dev/inventory.sql script into the /tmp directory of the db-app01 container, and execute the script inside the container. If you executed the script locally, then you would use the mysql -u root inventory < /tmp/inventory.sql command.

    1. Copy the /home/podmgr/db-dev/inventory.sql script into the /tmp directory of the db-app01 container.

      [podmgr@serverb ~]$ podman cp /home/podmgr/db-dev/inventory.sql \
      db-app01:/tmp/inventory.sql
    2. Execute the inventory.sql script in the db-app01 container.

      [podmgr@serverb ~]$ podman exec -it db-app01 sh -c 'mysql -u root \
      inventory < /tmp/inventory.sql'
  6. Use the container file in the /home/podmgr/http-dev directory to create the http-app01 detached container. The container image name must be http-client. Map the 8080 port on the local machine to the 8080 port in the container.

    1. Create the http-client:9.0 image with the container file in the /home/podmgr/http-dev directory.

      [podmgr@serverb ~]$ podman build -t http-client:9.0 http-dev/
      STEP 1/7: FROM registry.lab.example.com/rhel9/php-82:1-15
      ...output omitted...
    2. Create the http-app01 detached container. Map the 8080 port from the local machine to the 8080 port in the container.

      [podmgr@serverb ~]$ podman run -d --name http-app01 \
      -p 8080:8080 localhost/http-client:9.0
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                            COMMAND     CREATED         STATUS             PORTS                    NAMES
      ba398d080e00  registry.lab.example.com/rhel9/mariadb-105:1-86  run-mysqld  20 minutes ago  Up 20 seconds ago  0.0.0.0:13306->3306/tcp  db-app01
      ee424df19621  localhost/http-client:9.0                        /bin/sh -c   4 seconds ago  Up 4 seconds ago   0.0.0.0:8080->8080/tcp   http-app01
  7. Query the content of the http-app01 container. Verify that it shows the container name of the client and that the status of the database is up.

    1. Verify that the http-app01 container responds to http requests.

      [podmgr@serverb ~]$ curl 127.0.0.1:8080
      This is the server http-app01 and the database is up
  8. Return to the workstation machine as the student user.

    [podmgr@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 rhcsa-compreview4

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 rhcsa-compreview4
Revision: rh199-9.3-8dd73db