In this exercise, you pass environment variables to a container during creation, mount persistent storage to a container, create and connect multiple container networks, and expose container ports from the host machine.
Outcomes
Create container networks and connect them to containers.
Troubleshoot failed containers.
Pass environment variables to containers during creation.
Create and mount persistent storage to containers.
Map host ports to ports inside containers.
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-resources
Instructions
Log in to the servera machine as the student user.
[student@workstation ~]$ ssh student@servera
...output omitted...
[student@servera ~]$Create the frontend container network.
Create the db_client and db_01 containers and connect them to the frontend network.
Use the podman network create command --subnet and --gateway options to create the frontend network with the 10.89.1.0/24 subnet and the 10.89.1.1 gateway.
[student@servera ~]$podman network create --subnet 10.89.1.0/24 \--gateway 10.89.1.1 frontendfrontend
Log in to the registry.lab.example.com registry.
[student@servera ~]$podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!
Start a container named db_client in the background, and connect it to the frontend network.
To be able to install packages in the db_client container, mount the /etc/yum.repos.d DNF repositories directory at the /etc/yum.repos.d container path.
Run the sleep infinity command in the db_client container to prevent the container from exiting.
Use the registry.lab.example.com/ubi9-beta/ubi image.
[student@servera ~]$podman run -d --name db_client \--network frontend \-v /etc/yum.repos.d:/etc/yum.repos.d \registry.lab.example.com/ubi9-beta/ubi \sleep infinitye20dfed7e392abe4b7bea3c25e9cb17ef95d16af9cedd50d68f997a663ba6c15
Start in the background a container named db_01 that is connected to the frontend network.
Use the registry.lab.example.com/rhel8/mariadb-105 image.
[student@servera ~]$podman run -d --name db_01 --network frontend \registry.lab.example.com/rhel8/mariadb-1053e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405
View all containers.
[student@servera ~]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e20dfed7e392 registry.lab.example.com/ubi8/ubi:latest sleep infinity 56 seconds ago Up 56 seconds ago db_client
3e767ae6eea4 registry.lab.example.com/rhel8/mariadb-105:latest run-mysqld 1 second ago Exited (1) 1 second ago db_01Troubleshoot the db_01 container and determine why it is not running.
Re-create the db_01 container by using the required environment variables.
View the container logs and determine why the container exited.
[student@servera ~]$ podman container logs db_01
...output omitted...
You must either specify the following environment variables:
MYSQL_USER (regex: '^[a-zA-Z0-9_]+$')
MYSQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
MYSQL_DATABASE (regex: '^[a-zA-Z0-9_]+$')
Or the following environment variable:
MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
Or both.
...output omitted...Remove the db_01 container and create it again with environment variables.
Provide the required environment variables.
[student@servera ~]$podman rm db_013e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405 [student@servera ~]$podman run -d --name db_01 \--network frontend \-e MYSQL_USER=dev1 \-e MYSQL_PASSWORD=devpass \-e MYSQL_DATABASE=devdb \-e MYSQL_ROOT_PASSWORD=redhat \registry.lab.example.com/rhel8/mariadb-105948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273b
View the current running containers.
[student@servera ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e20dfed7e392 registry.lab.example.com/ubi8/ubi:latest sleep infinity 56 seconds ago Up 56 seconds ago db_client
948c4cd767b5 registry.lab.example.com/rhel8/mariadb-105:latest run-mysqld 11 seconds ago Up 12 seconds ago db_01Create persistent storage for the containerized MariaDB service, and map the local machine 13306 port to the 3306 port in the container.
Allow traffic to the 13306 port on the servera machine.
Create the /home/student/databases directory on the servera machine.
[student@servera ~]$ mkdir /home/student/databasesObtain the mysql UID and GID from the db_01 container, and then remove the db01 container.
[student@servera ~]$podman exec -it db_01 grep mysql /etc/passwdmysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin [student@servera ~]$podman stop db_01db_01 [student@servera ~]$podman rm db_01948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273b
Run the chown command inside the container namespace, and set the user and group owner to 27 on the /home/student/database directory.
[student@servera ~]$podman unshare chown 27:27 /home/student/databases/[student@servera ~]$ls -l /home/student/total 0 drwxr-xr-x. 2 100026 100026 6 May 9 17:40 databases
Create the db_01 container, and mount the /home/student/databases directory from the servera machine to the /var/lib/mysql directory inside the db_01 container.
Use the Z option to apply the required SELinux context.
[student@servera ~]$podman run -d --name db_01 \--network frontend \-e MYSQL_USER=dev1 \-e MYSQL_PASSWORD=devpass \-e MYSQL_DATABASE=devdb \-e MYSQL_ROOT_PASSWORD=redhat \-v /home/student/databases:/var/lib/mysql:Z \-p 13306:3306 \registry.lab.example.com/rhel8/mariadb-105
Install the mariadb package in the db_client container.
[student@servera ~]$ podman exec -it db_client dnf install -y mariadb
...output omitted...
Complete!Create the crucial_data table in the dev_db database in the db_01 container from the db_client container.
[student@servera ~]$podman exec -it db_client mysql -u dev1 -p -h db_01Enter password:devpass...output omitted... MariaDB [(none)]>USE devdb;Database changed MariaDB [devdb]>CREATE TABLE crucial_data(column1 int);Query OK, 0 rows affected (0.036 sec) MariaDB [devdb]>SHOW TABLES;+-----------------+ | Tables_in_devdb | +-----------------+ | crucial_data | +-----------------+ 1 row in set (0.001 sec) MariaDB [devdb]>quitBye
Allow port 13306 traffic in the firewall on the servera machine.
[student@servera ~]$sudo firewall-cmd --add-port=13306/tcp --permanent[sudo] password for student:studentsuccess [student@servera ~]$sudo firewall-cmd --reloadsuccess
Open a second terminal on the workstation machine and use the MariaDB client to connect to the servera machine on port 13306, to show tables inside the db_01 container that are stored in the persistent storage.
[student@workstation ~]$mysql -u dev1 -p -h servera --port 13306 \devdb -e 'SHOW TABLES';Enter password:devpass+-----------------+ | Tables_in_devdb | +-----------------+ | crucial_data | +-----------------+
Create a second container network called backend, and connect the backend network to the db_client and db_01 containers.
Test network connectivity and DNS resolution between the containers.
Create the backend network with the 10.90.0.0/24 subnet and the 10.90.0.1 gateway.
[student@servera ~]$podman network create --subnet 10.90.0.0/24 \--gateway 10.90.0.1 backendbackend
Connect the backend container network to the db_client and db_01 containers.
[student@servera ~]$podman network connect backend db_client[student@servera ~]$podman network connect backend db_01
Obtain the IP addresses of the db_01 container.
[student@servera ~]$ podman inspect db_01
...output omitted...
"Networks": {
"backend": {
"EndpointID": "",
"Gateway": "10.90.0.1",
"IPAddress": "10.90.0.3",
...output omitted...
"frontend": {
"EndpointID": "",
"Gateway": "10.89.1.1",
"IPAddress": "10.89.1.5",
...output omitted...Install the iputils package in the db_client container.
[student@servera ~]$ podman exec -it db_client dnf install -y iputils
...output omitted...
Complete!Ping the db_01 container name from the db_client container.
[student@servera ~]$ podman exec -it db_client ping -c4 db_01
PING db_01.dns.podman (10.90.0.3) 56(84) bytes of data.
...output omitted...
--- db_01.dns.podman ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3048ms
rtt min/avg/max/mdev = 0.043/0.049/0.054/0.004 msExit the servera machine.
[student@servera ~]$ exit
logout
Connection to servera closed.
[student@workstation ~]$This concludes the section.