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.

Outcomes

  • Create rootless detached containers.

  • Configure port mapping and persistent storage.

  • Configure systemd for a container to manage it with systemctl commands.

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 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.

  • 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 production DNS-enabled container network. Use the 10.81.0.0/16 subnet and 10.81.0.1 as the gateway. Use this container network for the containers that you create in this comprehensive review.

  • Create the db-app01 detached container based on the registry.lab.example.com/rhel8/mariadb-103 container image with the lowest tag number in the production network. 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 in the production network. The container image name must be http-client with the 9.0 tag. 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/* .
      [podmgr@serverb ~]$ ls -l
      total 0
      drwxr-xr-x. 2 podmgr podmgr 27 May 10 21:52 db-dev
      drwxr-xr-x. 2 podmgr podmgr 44 May 10 21:52 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 May 10 21:55 database
  3. Create the production DNS-enabled container network. Use the 10.81.0.0/16 subnet and 10.81.0.1 as the gateway. Use this container network for the containers that you create in this comprehensive review.

    1. Create the production DNS-enabled container network. Use the 10.81.0.0/16 subnet and 10.81.0.1 as the gateway.

      [podmgr@serverb ~]$ podman network create --gateway 10.81.0.1 \
      --subnet 10.81.0.0/16 production
      production
    2. Verify that the DNS feature is enabled in the production network.

      [podmgr@serverb ~]$ podman network inspect production
      [
           {
                "name": "production",
      ...output omitted...
                "subnets": [
                     {
                          "subnet": "10.81.0.0/16",
                          "gateway": "10.81.0.1"
                     }
                ],
      ...output omitted...
                "dns_enabled": true,
      ...output omitted...
  4. Create the db-app01 detached container based on the registry.lab.example.com/rhel8/mariadb-103 container image with the lowest tag number in the production network. 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. Search for the earliest version tag number of the registry.lab.example.com/rhel8/mariadb container image.

      [podmgr@serverb ~]$ skopeo inspect \
      docker://registry.lab.example.com/rhel8/mariadb-103
      {
          "Name": "registry.lab.example.com/rhel8/mariadb-103",
          "Digest": "sha256:a95b678e52bb9f4305cb696e45c91a38c19a7c2c5c360ba6c681b10717394816",
          "RepoTags": [
              "1-86",
              "1-102",
              "latest"
      ...output omitted...
    2. Use the earliest version tag number from the output of the previous step to create the detached db-app01 container in the production network. 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 \
      --network production -p 13306:3306 \
      -v /home/podmgr/storage/database:/var/lib/mysql/data:Z \
      registry.lab.example.com/rhel8/mariadb-103:1-86
      ...output omitted...
      ba398d080e00ba1d52b1cf4f5959c477681cce343c11cc7fc39e4ce5f1cf2384
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                            COMMAND     CREATED         STATUS             PORTS                    NAMES
      ba398d080e00  registry.lab.example.com/rhel8/mariadb-103:1-86  run-mysqld  20 seconds ago  Up 20 seconds ago  0.0.0.0:13306->3306/tcp  db-app01
  5. 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
      ba398d080e00  registry.lab.example.com/rhel8/mariadb-103:1-86  run-mysqld  About an hour ago  Exited (0) 3 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; disabled; vendor preset: disabled)
           Active: active (running) since Tue 2022-05-10 22:16:23 EDT; 7s ago
      ...output omitted...
      [podmgr@serverb ~]$ podman ps -a
      CONTAINER ID  IMAGE                                            COMMAND     CREATED         STATUS                 PORTS                    NAMES
      ba398d080e00  registry.lab.example.com/rhel8/mariadb-103:1-86  run-mysqld  59 seconds ago  Up About a minute ago  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
  6. 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'
  7. Use the container file in the /home/podmgr/http-dev directory to create the http-app01 detached container in the production network. The container image name must be http-client with the 9.0 tag. 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/rhel8/php-74:1-63
      ...output omitted...
    2. Create the http-app01 detached container in the production network. Map the 8080 port from the local machine to the 8080 port in the container.

      [podmgr@serverb ~]$ podman run -d --name http-app01 \
      --network production -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/rhel8/mariadb-103: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
  8. 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
  9. 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

This concludes the section.

Revision: rh134-9.0-fa57cbe