Open shift OC use S2I and use my self 's docker build steps - unix1998/technical_notes GitHub Wiki

When you use the oc new-app command with a source code URL, OpenShift will automatically detect the need for Source-to-Image (S2I) if appropriate and will handle the creation of the build, deployment, and service. After this step, you only need to expose the service to get a route.

Here's a summary of the steps:

  1. Create the application using oc new-app:

    oc new-app <source_code_url>
    

    This command will:

    • Create a new build configuration using S2I.
    • Start a build to create the image.
    • Create a deployment configuration.
    • Create a service to expose the deployment internally within the cluster.
  2. Expose the service to create a route:

    oc expose svc/<service_name>
    

    This command will create a route, making your application accessible externally.

Here’s an example using a hypothetical source code URL:

oc new-app https://github.com/example/repo.git
oc expose svc/repo

In this example:

  • OpenShift will create a build configuration from the source code at https://github.com/example/repo.git.
  • It will then build an image using S2I.
  • A deployment and a service named repo will be created.
  • Finally, exposing the service with oc expose svc/repo will create a route, making the application accessible from outside the cluster.

This simplifies the process of deploying applications from source code on OpenShift, automating most of the setup and configuration steps.

################################################################################################################# if we do not want to use the Source-to-Image (S2I) strategy, you need to specify a different build strategy when creating your application. In OpenShift, you can use a Docker build strategy if you have a Dockerfile in your repository.

Here's how you can do it:

  1. Create the application with the Docker build strategy:

    oc new-app https://github.com/example/repo.git --strategy=docker
    

    This command tells OpenShift to use the Docker build strategy instead of the default S2I strategy. It will create a build configuration that uses the Dockerfile present in your repository.

  2. Expose the service to create a route:

    oc expose svc/<service_name>
    

Here’s a complete example:

  1. Create the application:

    oc new-app https://github.com/example/repo.git --strategy=docker
    
  2. Expose the service to create a route:

    oc expose svc/repo
    

This setup will instruct OpenShift to use the Docker build strategy, leveraging your Dockerfile to build the image. OpenShift will then create a deployment and service based on this image. Finally, exposing the service will create a route for external access.

If you need to tag and push the image, you can do this after the image is built. OpenShift's integrated registry can be used for this purpose, or you can push the image to an external registry. For example, you can tag and push the image manually using oc tag and oc import-image commands if needed.

However, typically the process described above (using oc new-app with --strategy=docker) should handle most common use cases for building and deploying applications with Docker build strategy in OpenShift.

####################################################################################### If we prefer to use our own Dockerfile and manage the build, tag, and push process manually, we can use native Docker commands. After building and pushing image to a registry, we can then create the OpenShift application using the pre-built image.

Here's how you can do this:

Step 1: Build, Tag, and Push the Docker Image

  1. Build the Docker image:

    docker build -t myimage:latest .
    
  2. Tag the Docker image:

    docker tag myimage:latest <your-registry>/myimage:latest
    
  3. Push the Docker image to your registry:

    docker push <your-registry>/myimage:latest
    

Step 2: Create the OpenShift Application Using the Pre-Built Image

  1. Create the application using the pre-built image:

    oc new-app <your-registry>/myimage:latest
    
  2. Expose the service to create a route:

    oc expose svc/myimage
    

Detailed Example

Suppose you have a Dockerfile in your local directory, and you want to build an image, push it to Docker Hub (or any other container registry), and then deploy it on OpenShift.

Build and Push the Image

  1. Build the image:

    docker build -t myimage:latest .
    
  2. Tag the image for your registry:

    docker tag myimage:latest docker.io/yourusername/myimage:latest
    
  3. Push the image to Docker Hub:

    docker push docker.io/yourusername/myimage:latest
    

Create and Expose the Application in OpenShift

  1. Create the OpenShift application:

    oc new-app docker.io/yourusername/myimage:latest
    
  2. Expose the service to create a route:

    oc expose svc/myimage
    

Summary

By manually building, tagging, and pushing the Docker image using native Docker commands, you maintain full control over the build process. You then leverage OpenShift to handle the deployment, service creation, and routing.

This method gives you the flexibility to use any custom build process or Dockerfile without relying on OpenShift's build strategies.