- read

How to dockerize Golang application with installed Oracle Client

Maryia Krauchanka 36

Dockerizing your Golang application with an Oracle client can simplify deployment and ensure consistent behavior across different environments. In this article, we will walk you through the Dockerfile provided and explain each step in detail.

Here is a sample of Dockerfile

Explanation of Dockerfile Steps

Let’s explain each step in the Dockerfile:

  1. FROM golang:1.13-buster AS builder: This line sets the base image to use for building the application. It's based on the Golang 1.13 image.
  2. The RUN command installs the Oracle client and its dependencies if the ORA_CLIENT_URL and ORA_SDK_URL build-time arguments are provided. It also configures symbolic links for the Oracle client libraries.
  3. COPY ./build/oracle.pc /opt/oracle/instantclient/oci8.pc: This line copies the Oracle package configuration file to the Oracle client directory.
  4. Several ARG and ENV commands set build-time arguments and environment variables for later use.
  5. WORKDIR /build: This sets the working directory for subsequent commands to /build.
  6. COPY go.* ./ and RUN go mod download: These lines copy the go.mod and go.sum files and download Go module dependencies.
  7. COPY . ./ copies the remaining application source code.
  8. RUN go build builds the Golang application.
  9. The second FROM command switches to the final base image, which is a Debian Buster Slim image.
  10. ENV LD_LIBRARY_PATH="/opt/oracle/instantclient:${LD_LIBRARY_PATH}" sets the LD_LIBRARY_PATH environment variable to include the Oracle client library.
  11. ENV TZ=utc sets the timezone to UTC.
  12. Copy Oracle Client Files and Set Environment Variables: copies Oracle client files and the built application from the builder image.
  13. Create Build Info File and Define Default Command: creates a JSON file with build-related information and sets the default command to run the application using CMD.

Wrapping Up

In this article, we’ve walked through the steps involved in Dockerizing a Golang application with an Oracle client. Dockerizing your application allows you to package it with all its dependencies, ensuring consistency and portability across different environments. Here’s a recap of the key steps covered:

  1. Setting up the base image for building your Golang application.
  2. Configuring proxy settings if necessary.
  3. Installing the Oracle client and its dependencies, along with configuring Oracle-specific settings.
  4. Managing SSL certificates for secure communication.
  5. Defining build-time arguments and environment variables for flexibility.
  6. Building the Golang application within the builder image.
  7. Switching to the final base image for the production-ready container.
  8. Setting environment variables and configurations in the final image.
  9. Copying necessary files from the builder image to the final image.
  10. Defining the command to run the application.

By following these steps, you can create a Docker image that encapsulates your Golang application and the Oracle client, making it easier to deploy and manage across various environments.

Remember that this Dockerfile is tailored to a specific use case. You may need to customize it further to suit your application’s requirements and the Oracle client version you are using.

Docker simplifies the deployment of complex applications like those relying on Oracle databases. It provides isolation, reproducibility, and scalability benefits that can streamline your development and deployment processes.

I hope this article has been informative and helpful in your journey to Dockerize your Golang application with an Oracle client. If you have any questions or need further assistance, please don’t hesitate to reach out. Happy coding!