Bookmark this page

Lab: Running Containers

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

Outcomes

You should be able to:

  • Create detached containers.

  • Configure port redirection and persistent storage.

  • Configure systemd for containers to start when the host machine starts.

On the workstation machine, log in as the student user with student as the password.

On the workstation machine, run the lab containers-review start command. This command runs a start script that determines if the serverb machine is reachable on the network. It also installs the MariaDB client and creates the podsvc user account that you use to run a MariaDB container.

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

Procedure 13.6. Instructions

  1. On serverb, install the container tools. Log in to serverb as the student user, and then use the sudo command. The password for the student user is student.

    1. Use the ssh command to log in to serverb as the student user. The systems are configured to use SSH keys for authentication, so a password is not required.

      [student@workstation ~]$ ssh student@serverb
      ...output omitted...
      [student@serverb ~]$ 
    2. Install the container-tools Yum module using the yum command.

      [student@serverb ~]$ sudo yum module 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. On serverb, as the podsvc user, list those tags and take note of the tag with the lowest version number. You will use that image tag to start a container later in this exercise.

    The password for the podsvc user is redhat. To query the registry.lab.example.com registry, use the admin account with redhat321 for the password.

    1. Exit from the student account on serverb.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.
      [student@workstation ~]$ 
    2. Use the ssh command to log in to serverb as the podsvc user. The systems are configured to use SSH keys for authentication, so a password is not required.

      [student@workstation ~]$ ssh podsvc@serverb
      ...output omitted...
      [podsvc@serverb ~]$ 
    3. Log in to the container registry using the podman login command.

      [podsvc@serverb ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
    4. Use the skopeo inspect command to view information about the registry.lab.example.com/rhel8/mariadb-103 image. The following skopeo inspect command is very long and should be entered as a single line.

      [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 tag with the lowest number is 1-86.

  3. On serverb, as the podsvc user, create the /home/podsvc/db_data directory. Prepare the directory so that containers have read/write access. You will use this directory for persistent storage.

    1. Create the /home/podsvc/db_data directory.

      [podsvc@serverb ~]$ mkdir /home/podsvc/db_data
      [podsvc@serverb ~]$ 
    2. Set the access mode of the directory to 777 so that everyone has read/write access.

      [podsvc@serverb ~]$ chmod 777 /home/podsvc/db_data
      [podsvc@serverb ~]$ 
  4. On serverb, as the podsvc user, create a detached MariaDB container named inventorydb. Use the rhel8/mariadb-103 image from the registry.lab.example.com registry, specifying 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:

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

    To confirm that the MariaDB database is running, use the mysql command. You can find this command in the /home/podsvc/containers-review/testdb.sh script. You can also directly run the script to test the database.

    1. Use the podman run command to create the container. The following podman run command is very long and should be entered as a single line.

      [podsvc@serverb ~]$ podman run -d --name inventorydb -p 13306:3306 -v /home/podsvc/db_data:/var/lib/mysql/data:Z -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...
    2. Confirm that the database is running.

      [podsvc@serverb ~]$ ~/containers-review/testdb.sh
      Testing the access to the database...
      SUCCESS
  5. On serverb, as the podsvc user, configure systemd so that the inventorydb container starts automatically with the server.

    1. If you used sudo or su to log in as the podsvc user, then exit serverb and use the ssh command to directly log in to serverb as the podsvc user. Remember, systemd requires that the user open a direct session from the console or through SSH.

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

      [podsvc@serverb ~]$ mkdir -p ~/.config/systemd/user/
      [podsvc@serverb ~]$ 
    3. Use the podman generate systemd command to 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
      0d28f0e0a4118ff019691e34afe09b4d28ee526079b58d19f03b324bd04fd545
      [podsvc@serverb user]$ podman rm inventorydb
      0d28f0e0a4118ff019691e34afe09b4d28ee526079b58d19f03b324bd04fd545
    5. Instruct systemd 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/multi-user.target.wants/container-inventorydb.service → /home/podsvc/.config/systemd/user/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
      [podsvc@serverb ~]$ 
    8. Exit from serverb.

      [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 containers-review grade

Finish

On the workstation machine, run the lab containers-services finish script to complete this exercise.

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

This concludes the lab.

Revision: rh134-8.2-f0a9756