How do I set Java JDBC Environment Variables?

I am building a simple Java 8 app using Vanilla JDBC to connect to a postgres database. In my getConnection() method within ConnectionUtil.java I save my connection credentials in the following variables:

        String url = System.getenv("DB_URL");
        String username = System.getenv("DB_USERNAME");
        String password = System.getenv("DB_PASSWORD");

My docker-compose.yml file is as follows:

version: "3.9"

services:
  jdbc:
    build: .
    ports:
    - 7000:7000
    volumes:
    - ./logs/log.txt:/logslog.txt
    environment:
    - DB_URL
    - DB_USERNAME
    - DB_PASSWORD
    container_name: jdbc

For additional context, here is my Dockerfile:

# Base image with jdk 8 and maven
FROM maven:3.6.3-openjdk-8 as builder

# Copy pom.xml and source-code
COPY pom.xml pom.xml
COPY src/ src/

# Build application
RUN mvn clean package

# As a separate stage, to save on resulting image size, discard everything from previous stages
FROM java:8 as runner
# Base image only needs JRE 8

# Expose port 7000 for our web-app
EXPOSE 7000

# Copy the jar file from previous stage
COPY --from=builder target/jdbc-demo-jar-with-dependencies.jar app.jar

# Run program
ENTRYPOINT [ "java", "-jar", "app.jar" ]

I am still learning Docker. My question is, where do I set the environment variables?

Source: Docker Questions

LEAVE A COMMENT