Outcomes
Locate and run container images from a container registry.
Inspect remote container images and container logs.
Set environment variables and override entry points for a container.
Access files and directories within a container.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that all resources are available for this exercise.
[student@workstation ~]$ lab start pods-images
Instructions
Log in to the OpenShift cluster and create the
pods-imagesproject.Log in to the OpenShift cluster as the
developeruser with theoccommand.[student@workstation ~]$
oc login -u developer -p developer \ https://api.ocp4.example.com:6443...output omitted...Create the
pods-imagesproject.[student@workstation ~]$
oc new-project pods-images...output omitted...
Authenticate to
registry.ocp4.example.com:8443, which is the classroom container registry. This private registry hosts certain copies and tags of community images from Docker and Bitnami, as well as some supported images from Red Hat. Useskopeoto log in as thedeveloperuser, and then retrieve a list of available tags for theregistry.ocp4.example.com:8443/redhattraining/docker-nginxcontainer repository.Use the
skopeo logincommand to log in as thedeveloperuser with thedeveloperpassword.[student@workstation ~]$
skopeo login registry.ocp4.example.com:8443Username:developerPassword:developerLogin Succeeded!The classroom registry contains a copy and specific tags of the
docker.io/library/nginxcontainer repository. Use theskopeo list-tagscommand to retrieve a list of available tags for theregistry.ocp4.example.com:8443/redhattraining/docker-nginxcontainer repository.[student@workstation ~]$
skopeo list-tags \ docker://registry.ocp4.example.com:8443/redhattraining/docker-nginx{ "Repository": "registry.ocp4.example.com:8443/redhattraining/docker-nginx", "Tags": [ "1.23", "1.23-alpine", "1.23-perl", "1.23-alpine-perl" "latest" ] }
Create a
docker-nginxpod from theregistry.ocp4.example.com:8443/redhattraining/docker-nginx:1.23container image. Investigate any pod failures.Use the
oc runcommand to create thedocker-nginxpod.[student@workstation ~]$
oc run docker-nginx \ --image registry.ocp4.example.com:8443/redhattraining/docker-nginx:1.23pod/docker-nginx createdAfter a few moments, verify the status of the
docker-nginxpod.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE docker-nginx 0/1 Error 0 4s[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE docker-nginx 0/1 CrashLoopBackOff 2 (17s ago) 38sThe
docker-nginxpod failed to start.Investigate the pod failure. Retrieve the logs of the
docker-nginxpod to identify a possible cause of the pod failure.[student@workstation ~]$
oc logs docker-nginx...output omitted... /docker-entrypoint.sh: Configuration complete; ready for start up 2022/12/02 18:51:45 [warn] 1#1: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2 nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2 2022/12/02 18:51:45 [emerg] 1#1:mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied) nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)The pod failed to start because of permission issues for the
nginxdirectories.Create a debug pod for the
docker-nginxpod.[student@workstation ~]$
oc debug pod/docker-nginxStarting pod/docker-nginx-debug ... Pod IP: 10.8.0.72 If you don't see a command prompt, try pressing enter. $From the debug pod, verify the permissions of the
/etc/nginxand/var/cache/nginxdirectories.$
ls -la /etc/ | grep nginxdrwxr-xr-x. 3 root root 132 Nov 15 13:14 nginx$
ls -la /var/cache | grep nginxdrwxr-xr-x. 2 root root 6 Oct 19 09:32 nginxOnly the
rootuser has permission to thenginxdirectories. The pod must therefore run as the privilegedrootuser to work.Retrieve the user ID (UID) of the
docker-nginxuser to determine whether the user is a privileged or unprivileged account. Then, exit the debug pod.$
whoami1000820000$
exitRemoving debug pod ...Your UID value might differ from the previous output.
A UID over
0means that the container's user is anon-rootaccount. Recall that OpenShift default security policies prevent regular user accounts, such as thedeveloperuser, from running pods and their containers as privileged accounts.Confirm that the
docker-nginx:1.23image requires therootprivileged account. Use theskopeo inspect --configcommand to view the configuration for the image.[student@workstation ~]$
skopeo inspect --config \ docker://registry.ocp4.example.com:8443/redhattraining/docker-nginx:1.23...output omitted... "config": { "ExposedPorts": { "80/tcp": {} }, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.23.3", "NJS_VERSION=0.7.9", "PKG_RELEASE=1~bullseye" ], "Entrypoint": [ "/docker-entrypoint.sh" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Labels": { "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e" }, "StopSignal": "SIGQUIT" }, ...output omitted...The image configuration does not define
USERmetadata, which confirms that the image must run as therootprivileged user.The
docker-nginx:1-23container image must run as therootprivileged user. OpenShift security policies prevent regular cluster users, such as thedeveloperuser, from running containers as therootuser. Delete thedocker-nginixpod.[student@workstation ~]$
oc delete pod docker-nginxpod "docker-nginx" deleted
Create a
bitnami-mysqlpod, which uses a copy of the Bitnami community MySQL image. The image is available in theregistry.ocp4.example.com:8443/redhattraining/bitnami-mysqlcontainer repository.A copy and specific tags of the
docker.io/bitnami/mysqlcontainer repository are hosted in the classroom registry. Use theskopeo list-tagscommand to identify available tags for the Bitnami MySQL community image in theregistry.ocp4.example.com:8443/redhattraining/bitnami-mysqlcontainer repository.[student@workstation ~]$
skopeo list-tags \ docker://registry.ocp4.example.com:8443/redhattraining/bitnami-mysql{ "Repository": "registry.ocp4.example.com:8443/redhattraining/bitnami-mysql", "Tags": [ "8.0.31", "8.0.30", "8.0.29", "8.0.28", "latest" ] }Retrieve the configuration of the
bitnami-mysql:8.0.31container image. Determine whether the image requires a privileged account by inspecting image configuration forUSERmetadata.[student@workstation ~]$
skopeo inspect --config \ docker://registry.ocp4.example.com:8443/redhattraining/bitnami-mysql:8.0.31...output omitted... "config": "User":"1001", "ExposedPorts": { "3306/tcp": {} }, ....output omitted...The image defines the
1001UID, which means that the image does not require a privileged account.Create the
bitnami-mysqlpod with theoc runcommand. Use theregistry.ocp4.example.com:8443/redhattraining/bitnami-mysql:8.0.31container image. Then, wait a few moments and then retrieve the pod's status with theoc getcommand.[student@workstation ~]$
oc run bitnami-mysql \ --image registry.ocp4.example.com:8443/redhattraining/bitnami-mysql:8.0.31pod/bitnami-mysql created[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE bitnami-mysql 0/1 CrashLoopBackoff 2 (19s ago) 23sThe pod failed to start.
Examine the logs of the
bitnami-mysqlpod to determine the cause of the failure.[student@workstation ~]$
oc logs bitnami-mysqlmysql 16:18:00.40 mysql 16:18:00.40 Welcome to the Bitnami mysql container mysql 16:18:00.40 Subscribe to project updates by watching https://github.com/bitnami/containers mysql 16:18:00.40 Submit issues and feature requests at https://github.com/bitnami/containers/issues mysql 16:18:00.40 mysql 16:18:00.41 INFO ==> ** Starting MySQL setup ** mysql 16:18:00.42 INFO ==> Validating settings in MYSQL_*/MARIADB_* env vars mysql 16:18:00.42 ERROR ==>The MYSQL_ROOT_PASSWORD environment variable is empty or not set.Set the environment variable ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development.The
MYSQL_ROOT_PASSWORDenvironment variable must be set for the pod to start.Delete and then re-create the
bitnami-mysqlpod. Specifyredhat123as the value for theMYSQL_ROOT_PASSWORDenvironment variable. After a few moments, verify the status of the pod.[student@workstation ~]$
oc delete pod bitnami-mysqlpod "bitnami-mysql" deleted[student@workstation ~]$
oc run bitnami-mysql \ --image registry.ocp4.example.com:8443/redhattraining/bitnami-mysql:8.0.31 \ --env MYSQL_ROOT_PASSWORD=redhat123pod/bitnami-mysql created[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE bitnami-mysql 1/1 Running 0 20sThe
bitnami-mysqlpod successfully started.Determine the UID of the container user in the
bitnami-mysqlpod. Compare this value to the UID in the container image and to the UID range of thepods-imagesproject.[student@workstation ~]$
oc exec -it bitnami-mysql -- /bin/bash -c "whoami && id"1000820000 uid=1000820000(1000820000) gid=0(root) groups=0(root),1000820000[student@workstation ~]$
oc describe project pods-imagesName: pods-images ...output omitted... Annotations: openshift.io/description= ...output omitted... openshift.io/sa.scc.supplemental-groups=1000820000/10000openshift.io/sa.scc.uid-range=1000820000/10000...output omitted...Your values for the UID of the container and the UID range of the project might differ from the previous output.
The container user UID is the same as the specified UID range in the namespace. Notice that the container user UID does not match the
1001UID of the container image. For a container to use the specified UID of a container image, the pod must be created with a privileged OpenShift user account, such as theadminuser.
The private classroom registry hosts a copy of a supported MySQL image from Red Hat. Retrieve the list of available tags for the
registry.ocp4.example.com:8443/rhel9/mysql-80container repository. Compare therhel9/mysql-80container image release version that is associated with each tag.Use the
skopeo list-tagscommand to list the available tags for therhel9/mysql-80container image.[student@workstation ~]$
skopeo list-tags \ docker://registry.ocp4.example.com:8443/rhel9/mysql-80{ "Repository": "registry.ocp4.example.com:8443/rhel9/mysql-80", "Tags": [ "1-237", "1-228", "1-228-source", "1-224", "1-224-source", "latest", "1" ] }Several tags are available:
The
latestand1tags are floating tags, which are aliases to other tags, such as the1-237tag.The
1-228and1-224tags are fixed tags, which point to a build of a container.The
1-228-sourceand1-224-sourcetags are source containers, which provide the necessary sources and license terms to rebuild and distribute the images.
Use the
skopeo inspectcommand to compare therhel9/mysql-80container image release version and SHA IDs that are associated with the identified tags.Note
To improve readability, the instructions truncate the SHA-256 strings.
On your system, the commands return the full SHA-256 strings.
[student@workstation ~]$
skopeo inspect \ docker://registry.ocp4.example.com:8443/rhel9/mysql-80:latest...output omitted... "Name": "registry.ocp4.example.com:8443/rhel9/mysql-80", "Digest":"sha256:d282...f38f",...output omitted... "Labels": ...output omitted... "name": "rhel9/mysql-80", "release":"237",...output omitted...You can also format the output of the
skopeo inspectcommand with a Go template. Append the template objects with\nto add new lines between the results.[student@workstation ~]$
skopeo inspect --format \ "Name: {{.Name}}\n Digest: {{.Digest}}\n Release: {{.Labels.release}}" \ docker://registry.ocp4.example.com:8443/rhel9/mysql-80:latestName: registry.ocp4.example.com:8443/rhel9/mysql-80 Digest:sha256:d282...f38fRelease:237[student@workstation ~]$
skopeo inspect --format \ "Name: {{.Name}}\n Digest: {{.Digest}}\n Release: {{.Labels.release}}" \ docker://registry.ocp4.example.com:8443/rhel9/mysql-80:1Name: registry.ocp4.example.com:8443/rhel9/mysql-80 Digest:sha256:d282...f38fRelease:237[student@workstation ~]$
skopeo inspect --format \ "Name: {{.Name}}\n Digest: {{.Digest}}\n Release: {{.Labels.release}}" \ docker://registry.ocp4.example.com:8443/rhel9/mysql-80:1-237Name: registry.ocp4.example.com:8443/rhel9/mysql-80 Digest:sha256:d282...f38fRelease:237The
latest,1, and1-237tags resolve to the same release versions and SHA IDs. Thelatestand1tags are floating tags for the1-237fixed tag.
The classroom registry hosts a copy and certain tags of the
registry.redhat.io/rhel9/mysql-80container repository. Use theoc runcommand to create arhel9-mysqlpod from theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-237container image. Verify the status of the pod and then inspect the container logs for any errors.Create a
rhel9-mysqlpod with theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-237container image.[student@workstation ~]$
oc run rhel9-mysql \ --image registry.ocp4.example.com:8443/rhel9/mysql-80:1-237pod/rhel9-mysql createdAfter a few moments, retrieve the pod's status with the
oc getcommand.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE bitnami-mysql 1/1 Running 0 5m16s rhel9-mysql 0/1 CrashLoopBackoff 2 (29s ago) 49sThe pod failed to start.
Retrieve the logs for the
rhel9-mysqlpod to determine why the pod failed.[student@workstation ~]$
oc logs rhel9-mysql=> sourcing 20-validate-variables.sh ...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. Optional Settings: MYSQL_LOWER_CASE_TABLE_NAMES (default: 0) ...output omitted...The pod failed because the required environment variables were not set for the container.
Delete the
rhel9-mysqlpod. Create anotherrhel9-mysqlpod and specify the necessary environment variables. Retrieve the status of the pod and inspect the container logs to confirm that the new pod is working.Delete the
rhel9-mysqlpod with theoc deletecommand. Wait for the pod to delete before continuing to the next step.[student@workstation ~]$
oc delete pod rhel9-mysqlpod "rhel9-mysql" deletedCreate another
rhel9-mysqlpod from theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-237container image. Use theoc runcommand with the--envoption to specify the following environment variables and their values:Variable Value MYSQL_USERredhatMYSQL_PASSWORDredhat123MYSQL_DATABASEworldx[student@workstation ~]$
oc run rhel9-mysql \ --image registry.ocp4.example.com:8443/rhel9/mysql-80:1-237 \ --env MYSQL_USER=redhat \ --env MYSQL_PASSWORD=redhat123 \ --env MYSQL_DATABASE=worldxpod/rhel9-mysql createdAfter a few moments, retrieve the status of the
rhel9-mysqlpod with theoc getcommand. View the container logs to confirm that the database on therhel9-mysqlpod is ready to accept connections.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE bitnami-mysql 1/1 Running 0 10m rhel9-mysql 1/1 Running 0 20s[student@workstation ~]$
oc logs rhel9-mysql...output omitted... 2022-11-02T20:14:14.333599Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/lib/mysql/mysqlx.sock 2022-11-02T20:14:14.333641Z 0 [System] [MY-010931] [Server] /usr/libexec/mysqld:ready for connections.Version: '8.0.30' socket: '/var/lib/mysql/mysql.sock' port: 3306 Source distribution.The
rhel9-mysqlpod is ready to accept connections.
Determine the location of the MySQL database files for the
rhel9-mysqlpod. Confirm that the directory contains theworldxdatabase.Use the
oc imagecommand to inspect therhel9/mysql-80:1-237image in theregistry.ocp4.example.com:8443classroom registry.[student@workstation ~]$
oc image info \ registry.ocp4.example.com:8443/rhel9/mysql-80:1-237Name: registry.ocp4.example.com:8443/rhel9/mysql-80:1-237 ...output omitted... Command: run-mysqld Working Dir: /opt/app-root/src User: 27 Exposes Ports: 3306/tcp Environment: container=oci STI_SCRIPTS_URL=image:///usr/libexec/s2i STI_SCRIPTS_PATH=/usr/libexec/s2i APP_ROOT=/opt/app-root PATH=/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PLATFORM=el9 MYSQL_VERSION=8.0 APP_DATA=/opt/app-root/src HOME=/var/lib/mysqlThe container manifest sets the
HOMEenvironment variable for the container user to the/var/lib/mysqldirectory.Use the
oc execcommand to list the contents of the/var/lib/mysqldirectory.[student@workstation ~]$
oc exec -it rhel9-mysql -- ls -la /var/lib/mysqltotal 12 drwxrwxr-x. 1 mysql root 102 Nov 2 20:41 . drwxr-xr-x. 1 root root 19 Oct 24 18:47 .. drwxrwxr-x. 1 mysql root 4096 Nov 2 20:54 data srwxrwxrwx. 1 mysql 1000820000 0 Nov 2 20:41 mysql.sock -rw-------. 1 mysql 1000820000 2 Nov 2 20:41 mysql.sock.lock srwxrwxrwx. 1 mysql 1000820000 0 Nov 2 20:41 mysqlx.sock -rw-------. 1 mysql 1000820000 2 Nov 2 20:41 mysqlx.sock.lockA
datadirectory exists in the/var/lib/mysqldirectory.Use the
oc execcommand again to list the contents of the/var/lib/mysql/datadirectory.[student@workstation ~]$
oc exec -it rhel9-mysql \ -- ls -la /var/lib/mysql/data | grep worldxdrwxr-x---. 2 1000820000 root 6 Nov 2 20:41worldxThe
/var/lib/mysql/datadirectory contains theworldxdatabase with theworldxdirectory.
Determine the IP address of the
rhel9-mysqlpod. Next, create another MySQL pod, namedmysqlclient, to access therhel9-mysqlpod. Confirm that themysqlclientpod can view the available databases on therhel9-mysqlpod with themysqlshowcommand.Identify the IP address of the
rhel9-mysqlpod.[student@workstation ~]$
oc get pods rhel9-mysql -o json | jq .status.podIP"10.8.0.109"Note the IP address. Your IP address might differ from the previous output.
Use the
oc runcommand to create a pod namedmysqlclientthat uses theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-237container image. Set the value of theMYSQL_ROOT_PASSWORDenvironment variable toredhat123, and then confirm that the pod is running.[student@workstation ~]$
oc run mysqlclient \ --image registry.ocp4.example.com:8443/rhel9/mysql-80:1-237 \ --env MYSQL_ROOT_PASSWORD=redhat123pod/mysqlclient created[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE bitnami-mysql 1/1 Running 0 15m mysqlclient 1/1 Running 0 19s rhel9-mysql 1/1 Running 0 5mUse the
oc execcommand with the-itoptions to execute themysqlshowcommand on themysqlclientpod. Connect as theredhatuser and specify the host as the IP address of therhel9-mysqlpod. When prompted, enterredhat123for the password.[student@workstation ~]$
oc exec -it mysqlclient \ -- mysqlshow -u redhat -p -hEnter password:10.8.0.109redhat123+--------------------+ | Databases | +--------------------+ | information_schema | | performance_schema | | worldx | +--------------------+The
worldxdatabase on therhel9-mysqlpod is accessible to themysql-clientpod.