Environment variables

Environment variables keep your app secure, flexible and organized. Let’s take a look at how to pass environment variables to containers.

1. Changing your application to use an environment variable

Edit the Java Quarkus application you created using Visual Studio Code (alternatively you can use your preferred text editor):

code .

Open the GreetingResource.java class and change it like this:

package com.redhat.developers;

import java.util.Optional;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @ConfigProperty(name = "config")
    Optional<String> config;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return config.orElse("no config");
    }
}

Notice that instead of just printing hello, it will print the content of a ConfigProperty or print no config if the config doesn’t exist.

The @ConfigProperty annotation in Quarkus (based on MicroProfile standard) is used to inject configuration values (like server ports or database credentials) from various sources (e.g., application.properties, environment variables) directly into your Java fields or method parameters, making applications easily configurable.

Now, back to terminal, package your project:

mvn package -DskipTests=true

2. Building the container image

To build an image with an environment variable, you need to add the ENV directive to the Containerfile.

Rebuild our image to get the new version of the application:

podman build -t my-image .

Remove your running container:

podman rm -f my-container

And run the new one:

podman run -d --name my-container -p 8080:8080 my-image

Now let’s call the application:

curl localhost:8080/hello

You now got this output.

no config

The no config output happens because we didn’t really create the environment variable. Let’s fix it.

Edit your Containerfile like this (or simply add the ENV config=dockerfile line):

FROM registry.access.redhat.com/ubi9/openjdk-21-runtime:1.18-4

ENV LANGUAGE='en_US:en'

COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185 target/quarkus-app/*.jar /deployments/
COPY --chown=185 target/quarkus-app/app/ /deployments/app/
COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/

EXPOSE 8080
USER 185
ENV JAVA_OPTS="-Djava.util.logging.manager=org.jboss.logmanager.LogManager"
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"

ENV config=containerfile

Let’s rebuild our image, re-create the container and call it again:

podman build -t my-image .
podman rm -f my-container
podman run -d --name my-container -p 8080:8080 my-image
curl localhost:8080/hello

Now your output is:

containerfile

Finally, let’s replace the variable’s content. First we remove the container:

podman rm -f my-container

And then we re-create it passing a new value for the environment variable:

podman run -d --name my-container -p 8080:8080 -e config=container my-image

Then we call the application again:

curl localhost:8080/hello

And the output is:

container