Containerize the Application#
Running workloads in Kubernetes/OpenShift requires the application to be containerized. Typically, this includes taking a relevant base image, installing application dependencies if not available, copying & building the code and command to run your application executed at runtime.
Objectives#
- Containerize your application for deployment on Stakater App Agility Platform (SAAP).
Key Results#
- Dockerfile created.
- Image built and pushed to image repository
Tutorials#
Consider the stakater-nordmart-review-api
application.
git clone https://github.com/stakater-lab/stakater-nordmart-review-api
cd stakter-nordmart-review-api
Lets create a Dockerfile inside the repository folder and delete any existing file.
-
Decide a base image for your application. Navigate to RedHat Container Registry and Find a suitable image for your application. Since this application is java application. We use
maven
base image. -
Use COPY and RUN commands to copy required content to containers filesystem and run commands in containers context.
-
We will use another FROM statement to create a multi-stage build for reducing the overall image size. More info here. With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.
-
Add labels to your image, if any.
-
Set an environment variable with
ENV
command and set it as working directory. -
Use EXPOSE command to expose a container port, typically this corresponds to port on which application runs.
-
JAR files were generated as a result of
mvn package
. Copy the artifact generated from build stage. -
Define user.
-
Set the Entrypoint
-
Finally, specify the command to be executed when container is created with this image, typically the command to run the application.
-
Run the following command to build the image.
-
Execute the following command to run the image.
Note
To run the application container you need Mongo_DB server running.
# -p flag exposes container port 8080 on your local port 8080 # --env flag allows Mongo_DB necessary environment variables; e.g. MONGO_HOST, MONGO_DB_PASS docker run -dt -p [<localhost-port>:<container-port>] --env <variable1>=<value> --env <variable2>=<value> <image-name>:1.0.0
Note
If Mongo_DB server is running on your local machine, replace -p flag and it's values with --network="host".
-
Run a curl command to verify that image is running.
Read the following articles for more information: