Bookmark this page

Lab: Building and Publishing Container Images

Deploy container images from private registries by using Image Streams.

Outcomes

  • Build a container image locally.

  • Publish a container image to a private image registry.

  • Create an image stream from a container image in a private registry.

  • Create an application using a new image stream.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command ensures that the required files are in the lab ~/DO288/labs/images-review directory and that the images-review project exists in the Red Hat OpenShift cluster.

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

Instructions

Use the following as the input data for the required actions:

Application name custom-server
Image name custom-server:1.0.0
Image registry registry.ocp4.example.com:8443
Registry username developer
Registry password developer
Registry email developer@example.org
Registry secret name registry-credentials
  1. Build the container image in the ~/DO288/labs/images-review directory.

    1. Go to the location of the container that the exercise provides.

      [student@workstation ~]$ cd ~/DO288/labs/images-review
    2. Review the Containerfile contents.

      [student@workstation images-review]$ cat Containerfile
      FROM registry.ocp4.example.com:8443/redhattraining/hello-world-nginx:latest
      
      USER root
      
      RUN sed -i "s/nginx/OpenShift/g" /usr/share/nginx/html/index.html
      
      USER 1001
    3. Log in to the classroom container registry.

      [student@workstation images-review]$ podman login -u developer -p developer \
        registry.ocp4.example.com:8443
      Login Succeeded!
    4. Build the container image.

      [student@workstation images-review]$ podman build . \
        -t registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      STEP 1/4: FROM registry.ocp4.example.com:8443/redhattraining/hello-world-nginx:latest
      STEP 2/4: USER root
      --> c8bd9df97f6
      STEP 3/4: RUN sed -i "s/nginx/OpenShift/g" /usr/share/nginx/html/index.html
      --> a09383c6d5e
      STEP 4/4: USER 1001
      COMMIT registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      --> afef715ff1d
      Successfully tagged registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      afef715ff1daffe01e1f0a225fbaa089ee28617d23e9ffc4e91f4f6f75c32faa
  2. Push the custom-server image to the classroom private registry.

    1. Push the container image to the internal classroom registry.

      [student@workstation images-review]$ podman push \
        registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      ...output omitted...
      Writing manifest to image destination
      Storing signatures
  3. Create a secret called registry-credentials to access the private registry by using the data that the exercise provides.

    1. Log in to Red Hat OpenShift.

      [student@workstation images-review]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    2. Ensure that you are in the images-review project.

      [student@workstation images-review]$ oc project images-review
      Already on project"images-review" on server "https://api.ocp4.example.com:6443".
    3. Create the registry-credentials secret of the docker-registry type that contains the credentials.

      [student@workstation images-review]$ oc create secret docker-registry \
        registry-credentials \
        --docker-server=registry.ocp4.example.com:8443 \
        --docker-username=developer \
        --docker-password=developer \
        --docker-email=developer@example.org
      secret/registry-credentials created
    4. Link the secret to the default service account.

      [student@workstation images-review]$ oc secrets link default \
        registry-credentials --for=pull
      no output expected
  4. Create an image stream for the custom-server:1.0.0 image that you pushed to the classroom registry.

    1. Create the custom-server image stream that points to the container image that you pushed to the registry.

      [student@workstation images-review]$ oc import-image custom-server --confirm \
      --from registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      imagestream.image.openshift.io/custom-server imported
      
      Name:           custom-server
      Namespace:      images-review
      ...output omitted...
      Unique Images:  1
      Tags:           1
      
      latest
        tagged from registry.ocp4.example.com:8443/developer/custom-server:1.0.0
      ...output omitted...
  5. Create an OpenShift application by using the custom-server image stream.

    1. Deploy an application by using the image stream.

      [student@workstation images-review]$ oc new-app --name custom-server \
      -i images-review/custom-server
      --> Found image ...output omitted...
      ...output omitted...
      --> Creating resources ...
          deployment.apps "custom-server" created
          service "custom-server" created
      --> Success
      ...output omitted...
    2. Wait until the application pod is ready and running.

      [student@workstation images-review]$ oc get pod
      NAME                     READY   STATUS    RESTARTS   AGE
      custom-server-7d597c89c4-lng8p   1/1     Running   0          30s
  6. Validate that the application works by using the route URL and by verifying that the Hello, world from OpenShift! string shows in the response.

    1. Create a route to expose the application.

      [student@workstation images-review]$ oc expose svc custom-server
      route.route.openshift.io/custom-server exposed
    2. Get the hostname of the route.

      [student@workstation images-review]$ oc get route
      NAME    HOST/PORT ...output omitted...
      custom-server   custom-server-images-review.apps.ocp4.example.com ...output omitted...
    3. Test the application by using the curl command and the hostname from the previous step.

      [student@workstation images-review]$ curl \
        http://custom-server-images-review.apps.ocp4.example.com
      <html>
        <body>
          <h1>Hello, world from OpenShift!</h1>
        </body>
      </html>

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 images-review

Finish

As the student user on the workstation machine, 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 images-review

Revision: do288-4.12-0d49506