Introduction to OpenShift: Part 3

Ramazan Akkoyun
7 min readSep 24, 2022

--

Finding an Image on Docker Hub

Many container images are available for download from the Docker community website. It is a large repository where developers and administrators can get a number of container images developed by the community and some companies. Anyone can publish images to Docker Hub after they register. For larger projects (with multiple images), a paid subscription is needed. By default, Docker downloads image layers from the Docker Hub image registry. However, images do not provide textual information about themselves, and a search engine tool called Docker Hub was created to look for information about each image and its functionality. Red Hat also provides a private registry with tested and certified container images. By default, RHEL 7 is configured to look for the Red Hat registry in addition to Docker Hub.

Searching From the Docker Client

The docker command can also be used to search for container

  • docker search mysql

The search uses the Docker Hub registry and also any other version compatible registries configured in the local Docker daemon.

Running the docker command requires special privileges. A development environment usually manages that requirement by assigning the developer to the docker group. Without the correct privileges to run the docker command, an error message appears.

Fetching an Image

To download an image from repository

  • docker pull mysql
  • docker pull mysql:5.5

Many version of the same image cana be provided. Each one receiver a different tag name. If no tag name is provided, then docker pull assumes the tag called latest by default. To download specific tag, append the tag name to the image name separated by a colon in the docker pull command.

Listing the Images Available in the Local Cache

To list all images that were already downloaded by the local Docker daemon, we can use docker images command

  • docker images

Creating a Container

To create and start a process within a new container, we can use docker run command. The container is created from the container image name passed as argument.

  • docker run mysql
  • docker run — name mysql-container -it mysql:5.5 /bin/bash

If the image is not available on the local Docker daemon cache, the docker run command tries to pull the image as if a docker pull command had been used.

The -i and -t options are usually needed for interactive text-based programs, so the get allocated a pseudo-terminal, but not for background daemons. The program must exist inside the container image.

The management docker commands require and ID or a name. The docker run command generates a random ID and a random name that are unique. The docker ps command is responsible for displaying these attributes.

Docker Client Verbs

The Docker client, implemented by the docker command, provides a set of verbs to create and manage containers. The following figure shows a summary of the most commonly used verbs that change container state.

Docker client action verbs
Docker client query verbs

Some of the other docker commands:

  • docker exec: The docker exec command starts an additional process inside a running container.
  • docker ps: This command is responsible for listing running containers. docker ps -a used for list all containers.
  • docker inspect: This command is responsible for listing metadata about a running or stopped container.
  • docker stop: This command is responsible for stopping a running container gracefully.
  • docker kill: This command is responsible for stopping a running container forcefully.
  • docker restart: This command is responsible for restarting a stopped container.
  • docker rm: This command is responsible for deleting a container, discarding its state and filesystems.
  • docker save: An existing image from Docker cache can be saved to a tar file using this command.
  • docker load: To restore the container image from backup, we can use this command.
  • docker tag: To push an image to a registry, it must be stored in the docker’s cache, and it should be tagged for identification purposes. To tag an image, we can use this command.
  • docker push: To push the image to the registry, we can use this command.
  • docker rmi: To delete an image from the cache, we can use this command. To delete all images that are not used by any container, we can use docker rmi $(docker images -q) .
  • docker diff: To identify which files were changed, created or deleted since the container was started, docker client has a verb called diff to identify the changes made to a container.

Transferring a Container Image

Let’s assume one developer finished testing a custom container in his/her machine and needs to transfer this container image to another host, for another developer to use it, or to a production server. There are two ways to achieve that:

  1. Save the container image to a tar file
  2. Publish or push the container image to an image registry.

Building Custom Container Images with Dockerfile

A Dockerfile is a mechanism that the Docker packaging model provides to automate the building of container images. Building an image from a Dockerfile is a three-step process:

  1. Create a working directory: The docker command can use the files in a working directory to build an image. An empty working directory should be created to keep from incorporating unnecessary files into the image. For security reasons, the root directory, /, should never be used as a working directory for image builds.
  2. Write the Dockerfile specification
  3. Build the image with the docker command: The docker build command processes and builds a new image based on the instructions it contains.
Example of Dockerfile specification

The oc Command-Line Tool

OpenShift Container Platform (OCP) ships with a command-line tool that enables system administrators and developers to work with an OCP cluster. The oc command-line tool provides the ability to modify and manage resources throughout the delivery life cycle of a software development project. Common operations with this tool include deploying applications, scaling applications, checking the status of projects, and similar tasks.

Once the oc CLI tool is installed, the oc help command displays help information. There are oc subcommands for tasks such as:

  • Logging in and out of an OCP cluster.
  • Creating, changing, and deleting projects.
  • Creating applications inside a project. For example, creating a deployment configuration from a container image, or a build configuration from application source, and all associated resources.
  • Creating, deleting inspecting, editing, and exporting individual resources such as pods, services, and routes inside a project.
  • Scaling applications.
  • Starting new deployments and builds.
  • Checking logs from application pods, deployments, and build operations.

Core CLI Commands

  • oc login: We can use this command to log in interactively.
  • oc whoami: To check our current credentials, we can use this command.
  • oc new-project: To create a new project, we can use this command.
  • oc status: We can use this command to verify the status of the project.
  • oc delete project: To delete a project, we can use this command.
  • oc logout: To log out of the OpenShift cluster, we can use this command.
  • oc cluster up: If running the all-in one OCP cluster, the user who started the cluster can also log in as the system:admin user without a password, because the oc cluster up command saves an cluster administrator authentication token in the user home folder.
  • oc new-app: This command can create application pods to run on OpenShift in many different ways. It can create pods from existing docker images, from Dockerfiles, and from raw source code using the Source-to-Image (S2I) process.
Deployment configuration and dependent resources

Useful Commands to Manage OpenShift Resources

  • oc get: This command allows a user to get information about resources in the cluster. If the resource name parameter is omitted, then all resources of the specified resource type are summarized. For example; oc get pods .
  • oc get all: If we want a summary of all the most important components of a cluster, we can use this command.
  • oc describe resource resource_name: If the summaries provided by oc get are insufficient, additional information about the resources can be retrieved by using this command.
  • oc export: This command can be used to export a definition of a resource.
  • oc create: This command allows the user to create resources from a resource definition.
  • oc edit: This command allows the user to edit resources of a resource definition.
  • oc delete resource_type name: This command allows the user to remove a resource from an OpenShift cluster.
  • oc exec pod_name <options> <command>: This command allows the user to execute commands inside a container.
  • oc rsh pod_name <options>: This command opens an interactive shell session to execute commands inside a container. It is a shorthand for oc exec -it pod_name bash .

Thank you for reading…

Source & References:

  • Docker official website
  • Kubernetes official website
  • RedHat OpenShift website
  • edX Learning Platform

--

--

Ramazan Akkoyun
Ramazan Akkoyun

Written by Ramazan Akkoyun

Solution Architect 💻 lives in Amsterdam 🇱🇺

No responses yet