Bookmark this page

Guided Exercise: Deploy Containers

In this exercise, you use container management tools to build an image, run a container, and query the running container environment.

Outcomes

  • Configure a container image registry and create a container from an existing image.

  • Create a container by using a container file.

  • Copy a script from a host machine into containers and run the script.

  • Delete containers and images.

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

Instructions

  1. Log in to the servera machine as the student user.

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

    [student@servera ~]$ sudo dnf install container-tools
    [sudo] password for student: student
    ...output omitted...
    Is this ok [y/N]: y
    ...output omitted...
    Complete!
  3. Configure the registry.lab.example.com classroom registry in your home directory. Log in to the container registry with admin as the user and redhat321 as the password.

    1. Create the /home/student/.config/containers directory.

      [student@servera ~]$ mkdir -p /home/student/.config/containers
    2. Create the /home/student/.config/containers/registries.conf file with the following contents:

      unqualified-search-registries = ['registry.lab.example.com']
      
      [[registry]]
      location = "registry.lab.example.com"
      insecure = true
      blocked = false
    3. Verify that the classroom registry is added.

      [student@servera ~]$ podman info
      ...output omitted...
      registries:
        registry.lab.example.com:
          Blocked: false
          Insecure: true
          Location: registry.lab.example.com
          MirrorByDigestOnly: false
          Mirrors: null
          Prefix: registry.lab.example.com
        search:
        - registry.lab.example.com
      ...output omitted...
    4. Log in to the classroom registry.

      [student@servera ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
  4. Run the python38 container in detached mode from an image with the python 3.8 package and based on the ubi8 image. The image is hosted on a remote registry.

    1. Search for a python-38 container in the registry.lab.example.com registry.

      [student@servera ~]$ podman search registry.lab.example.com/
      NAME                                        DESCRIPTION
      ...output omitted...
      registry.lab.example.com/ubi8/python-38
      registry.lab.example.com/ubi8/httpd-24
      registry.lab.example.com/rhel8/php-74
    2. Inspect the image.

      [student@servera ~]$ skopeo inspect \
      docker://registry.lab.example.com/ubi8/python-38
      ...output omitted...
              "description": "Python 3.8 available as container is a base platform for building and running various Python 3.8 applications and frameworks.
      ...output omitted...
    3. Pull the python-38 container image.

      [student@servera ~]$ podman pull registry.lab.example.com/ubi8/python-38
      Trying to pull registry.lab.example.com/ubi8/python-38:latest...
      ...output omitted...
      671cc3cb42984e338733ebb5a9a68e69e267cb7f9cb802283d3bc066f6321617
    4. Verify that the container is downloaded to the local image repository.

      [student@servera ~]$ podman images
      REPOSITORY                               TAG     IMAGE ID      CREATED     SIZE
      registry.lab.example.com/ubi8/python-38  latest  671cc3cb4298  5 days ago  901 MB
    5. Start the python38 container.

      [student@servera ~]$ podman run -d --name python38 \
      registry.lab.example.com/ubi8/python-38 sleep infinity
      004756b52d3d3326545f5075594cffa858afd474b903288723a3aa299e72b1af
    6. Verify that the container was created.

      [student@servera ~]$ podman ps
      CONTAINER ID  IMAGE                                             COMMAND         CREATED             STATUS                 PORTS       NAMES
      004756b52d3d  registry.lab.example.com/ubi8/python-38:latest    sleep infinity  About a minute ago  Up About a minute ago              python38
  5. Build a container image called python39:1.0 from a container file, and use the image to create a container called python39.

    1. Examine the container file in the /home/student/python39 directory.

      [student@servera ~]$ cat /home/student/python39/Containerfile
      FROM registry.lab.example.com/ubi9-beta/ubi:latest
      RUN echo -e '[rhel-9.0-for-x86_64-baseos-rpms]\nbaseurl = http://content.example.com/rhel9.0/x86_64/dvd/BaseOS\nenabled = true\ngpgcheck = false\nname = Red Hat Enterprise Linux 9.0 BaseOS (dvd)\n[rhel-9.0-for-x86_64-appstream-rpms]\nbaseurl = http://content.example.com/rhel9.0/x86_64/dvd/AppStream\nenabled = true\ngpgcheck = false\nname = Red Hat Enterprise Linux 9.0 Appstream (dvd)'>/etc/yum.repos.d/rhel_dvd.repo
      RUN yum install --disablerepo=* --enablerepo=rhel-9.0-for-x86_64-baseos-rpms --enablerepo=rhel-9.0-for-x86_64-appstream-rpms -y python3
    2. Create the container image from the container file.

      [student@servera ~]$ podman build -t python39:1.0 /home/student/python39/.
      STEP 1/4: FROM registry.lab.example.com/ubi9-beta/ubi:latest
      ...output omitted...
      STEP 2/4: RUN echo -e '[rhel-9.0-for-x86_64-baseos-rpms] ...
      ...output omitted...
      STEP 3/4: RUN yum install --disablerepo=* --enablerepo=rhel-9.0-for-x86_64-baseos-rpms --enablerepo=rhel-9.0-for-x86_64-appstream-rpms -y python3
      ...output omitted...
      STEP 4/4: CMD ["/bin/bash", "-c", "sleep infinity"]
      ...output omitted...
      Successfully tagged localhost/python39:1.0
      80e68c195925beafe3b2ad7a54fe1e5673993db847276bc62d5f9d109e9eb499
    3. Verify that the container image exists in the local image repository.

      [student@servera ~]$ podman images
      REPOSITORY                              TAG    IMAGE ID      CREATED        SIZE
      localhost/python39                      1.0    80e68c195925  3 minutes ago  266 MB
      registry.lab.example.com/ubi8/python-38 latest 671cc3cb4298  5 days ago     901 MB
      registry.lab.example.com/ubi9-beta/ubi  latest fca12da1dc30  4 months ago   235 MB
    4. Inspect the python39 container.

      [student@servera ~]$ podman inspect localhost/python39:1.0
      ...output omitted...
                  "comment": "FROM registry.lab.example.com/ubi9-beta/ubi:latest"
      ...output omitted...
                  "created_by": "/bin/sh -c yum install --disablerepo=*
      --enablerepo=rhel-9.0-for-x86_64-baseos-rpms --enablerepo=rhel-9.0-for-x86_64-appstream-rpms -y python3"
      ...output omitted...
                  "created_by": "/bin/sh -c #(nop) CMD [\"/bin/bash\", \"-c\", \"sleep infinity\"]",
      ...output omitted...
    5. Create the python39 container.

      [student@servera ~]$ podman create --name python39 localhost/python39:1.0
      3db4eabe9043224a7bdf195ab5fd810bf95db98dc29193392cef7b94489e1aae
    6. Start the python39 container.

      [student@servera ~]$ podman start python39
      python39
    7. Verify that the container is running.

      [student@servera ~]$ podman ps
      CONTAINER ID  IMAGE                                             COMMAND               CREATED             STATUS             PORTS       NAMES
      004756b52d3d  registry.lab.example.com/ubi8/python-38:latest    sleep infinity        33 minutes ago      Up 33 minutes ago              python38
      3db4eabe9043  localhost/python39:1.0                            /bin/bash -c slee...  About a minute ago  Up 42 seconds ago              python39
  6. Copy the /home/student/script.py script into the /tmp directory of the running containers, and run the script on each container.

    1. Copy the /home/student/script.py python script into the /tmp directory in both containers.

      [student@servera ~]$ podman cp /home/student/script.py python39:/tmp/script.py
      [student@servera ~]$ podman cp /home/student/script.py python38:/tmp/script.py
    2. Run the Python script in both containers, and then run the Python script on the host.

      [student@servera ~]$ podman exec -it python39 python3 /tmp/script.py
      This script was not run on the correct version of Python
      Expected version of Python is 3.8
      Current version of python is 3.9
      [student@servera ~]$ podman exec -it python38 python3 /tmp/script.py
      This script was correctly run on Python 3.8
      [student@servera ~]$ python3 /home/student/script.py
      This script was not run on the correct version of Python
      Expected version of Python is 3.8
      Current version of python is 3.9
  7. Delete containers and images. Return to workstation.

    1. Stop both containers.

      [student@servera ~]$ podman stop python39 python38
      ...output omitted...
      python38
      python39
    2. Remove both containers.

      [student@servera ~]$ podman rm python39 python38
      3db4eabe9043224a7bdf195ab5fd810bf95db98dc29193392cef7b94489e1aae
      004756b52d3d3326545f5075594cffa858afd474b903288723a3aa299e72b1af
    3. Remove both container images.

      [student@servera ~]$ podman rmi localhost/python39:1.0 \
      registry.lab.example.com/ubi8/python-38:latest \
      registry.lab.example.com/ubi9-beta/ubi
      Untagged: localhost/python39:1.0
      Untagged: registry.lab.example.com/ubi8/python-38:latest
      Deleted: 80e68c195925beafe3b2ad7a54fe1e5673993db847276bc62d5f9d109e9eb499
      Deleted: 219e43f6ff96fd11ea64f67cd6411c354dacbc5cbe296ff1fdbf5b717f01d89a
      Deleted: 671cc3cb42984e338733ebb5a9a68e69e267cb7f9cb802283d3bc066f6321617
    4. Return to workstation.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.

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

This concludes the section.

Revision: rh199-9.0-4fecb06