Bookmark this page

Lab: Run Containers

In this lab, you configure on your server a container that provides a MariaDB database service, stores its database on persistent storage, and starts automatically with the server.

Outcomes

  • Create detached containers.

  • Configure port redirection and persistent storage.

  • Configure systemd for containers to start 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-review

Instructions

  1. On serverb, install the container tools package.

    1. Log in to serverb as the student user.

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

      [student@serverb ~]$ sudo dnf install container-tools
      [sudo] password for student: student
      ...output omitted...
      Is this ok [y/N]: y
      ...output omitted...
      Complete!
  2. The container image registry at registry.lab.example.com stores the rhel8/mariadb-103 image with several tags. Use the podsvc user to list the available tags and note the tag with the lowest version number. Use the admin user and redhat321 password to authenticate to the registry. Use the /tmp/registries.conf file as a template for the registry configuration.

    1. Return to the workstation machine as the student user.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.
      [student@workstation ~]$
    2. Log in to serverb as the podsvc user.

      [student@workstation ~]$ ssh podsvc@serverb
      ...output omitted...
      [podsvc@serverb ~]$
    3. Configure access to the registry.lab.example.com classroom registry in your home directory. Use the /tmp/registries.conf file as a template.

      [podsvc@serverb ~]$ mkdir -p ~/.config/containers/
      [podsvc@serverb ~]$ cp /tmp/registries.conf \
      ~/.config/containers/
    4. Log in to the container registry with the podman login command.

      [podsvc@serverb ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!

      Note

      The repository that contains the mariadb container image is not a public repository, and so the podman search mariadb command returns no results. Review the note in the podman-search (1) man page about the unreliability of using podman-search to determine the existence of an image.

    5. View information about the registry.lab.example.com/rhel8/mariadb-103 image.

      [podsvc@serverb ~]$ skopeo inspect \
      docker://registry.lab.example.com/rhel8/mariadb-103
      {
          "Name": "registry.lab.example.com/rhel8/mariadb-103",
          "Digest": "sha256:a95b...4816",
          "RepoTags": [
              "1-86",
              "1-102",
              "latest"
          ],
      ...output omitted...

      The lowest version tag is the 1-86 version.

  3. Create the /home/podsvc/db_data directory, and configure the directory so that containers have read/write access. Then, create the inventorydb detached container. Use the rhel8/mariadb-103 image from the registry.lab.example.com registry, and specify the tag with the lowest version number on that image, which you found in a preceding step. Map port 3306 in the container to port 13306 on the host. Mount the /home/podsvc/db_data directory on the host as /var/lib/mysql/data in the container. Declare the following variable values for the container:

    VariableValue
    MYSQL_USER operator1
    MYSQL_PASSWORD redhat
    MYSQL_DATABASE inventory
    MYSQL_ROOT_PASSWORD redhat

    You can copy and paste these parameters from the /home/podsvc/containers-review/variables file on serverb. Execute the /home/podsvc/containers-review/testdb.sh script to confirm that the MariaDB database is running.

    1. Start the db_01 detached container to obtain the mysql UID and GID.

      [podsvc@serverb ~]$ podman run -d --name db_01 -p 13306:3306 \
      -e MYSQL_USER=operator1 \
      -e MYSQL_PASSWORD=redhat \
      -e MYSQL_DATABASE=inventory \
      -e MYSQL_ROOT_PASSWORD=redhat \
      registry.lab.example.com/rhel8/mariadb-103:1-86
      ...output omitted...
      c33f85d177dc8c51a303e231e6be63c1f251b9d426b4ccb56498603ab72d4219
    2. Create the /home/podsvc/db_data directory.

      [podsvc@serverb ~]$ mkdir /home/podsvc/db_data
    3. Obtain the mysql UID and GID from the db_01 container, and then remove the db01 container.

      [podsvc@serverb ~]$ podman exec -it db_01 grep mysql /etc/passwd
      mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
      [podsvc@serverb ~]$ podman stop db_01
      db_01
      [podsvc@serverb ~]$ podman rm db_01
      c33f85d177dc8c51a303e231e6be63c1f251b9d426b4ccb56498603ab72d4219
    4. Use the podman unshare command to set the user namespace UID and GID of 27 as the owner of the directory.

      [podsvc@serverb ~]$ podman unshare chown 27:27 /home/podsvc/db_data
    5. Create the container.

      [podsvc@serverb ~]$ podman run -d --name inventorydb -p 13306:3306 \
      -e MYSQL_USER=operator1 \
      -e MYSQL_PASSWORD=redhat \
      -e MYSQL_DATABASE=inventory \
      -e MYSQL_ROOT_PASSWORD=redhat \
      -v /home/podsvc/db_data:/var/lib/mysql/data:Z \
      registry.lab.example.com/rhel8/mariadb-103:1-86
      ...output omitted...
    6. Confirm that the database is running.

      [podsvc@serverb ~]$ ~/containers-review/testdb.sh
      Testing the access to the database...
      SUCCESS
  4. Configure the systemd daemon so that the inventorydb container starts automatically when the system boots.

    1. If you used sudo or su to log in as the podsvc user, then exit serverb and use the ssh command to log in directly to serverb as the podsvc user. Remember, the systemd daemon requires the user to open a direct session from the console or through SSH. Omit this step if you already logged in to the serverb machine as the podsvc user by using SSH.

      [student@workstation ~]$ ssh podsvc@serverb
      ...output omitted...
      [podsvc@serverb ~]$
    2. Create the ~/.config/systemd/user/ directory.

      [podsvc@serverb ~]$ mkdir -p ~/.config/systemd/user/
    3. Create the systemd unit file from the running container.

      [podsvc@serverb ~]$ cd ~/.config/systemd/user/
      [podsvc@serverb user]$ podman generate systemd --name inventorydb --files --new
      /home/podsvc/.config/systemd/user/container-inventorydb.service
    4. Stop and then delete the inventorydb container.

      [podsvc@serverb user]$ podman stop inventorydb
      inventorydb
      [podsvc@serverb user]$ podman rm inventorydb
      0d28f0e0a4118ff019691e34afe09b4d28ee526079b58d19f03b324bd04fd545
    5. Instruct the systemd daemon to reload its configuration, and then enable and start the container-inventorydb service.

      [podsvc@serverb user]$ systemctl --user daemon-reload
      [podsvc@serverb user]$ systemctl --user enable --now container-inventorydb.service
      Created symlink /home/podsvc/.config/systemd/user/default.target.wants/container-inventorydb.service → /home/podsvc/.config/systemd/user/container-inventorydb.service.
    6. Confirm that the container is running.

      [podsvc@serverb user]$ ~/containers-review/testdb.sh
      Testing the access to the database...
      SUCCESS
      [podsvc@serverb user]$ podman ps
      CONTAINER ID  IMAGE                                            COMMAND     CREATED         STATUS             PORTS                    NAMES
      3ab24e7f000d  registry.lab.example.com/rhel8/mariadb-103:1-86  run-mysqld  47 seconds ago  Up 46 seconds ago  0.0.0.0:13306->3306/tcp  inventorydb
    7. Run the loginctl enable-linger command for the user services to start automatically when the server starts.

      [podsvc@serverb ~]$ loginctl enable-linger
    8. 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

This concludes the section.

Revision: rh199-9.0-4fecb06