Dockerfile with SPRING_PROFILES_ACTIVE

Build Docker Image with SPRING_PROFILES_ACTIVE as argument.

FROM openjdk:8-jdk-slim
ENV PORT 8080
ENV CLASSPATH /opt/lib
EXPOSE 8080

# copy pom.xml and wildcards to avoid this command failing if there's no target/lib directory
COPY pom.xml target/lib* /opt/lib/

# NOTE we assume there's only 1 jar in the target dir
# but at least this means we don't have to guess the name
# we could do with a better way to know the name - or to always create an app.jar or something
COPY ./target/*.jar /opt/app.jar
WORKDIR /opt

# Recieve the name of an argument with ENVIRONMENT
ARG ENVIRONMENT
# Check the Argument is working Delete in Prod
RUN echo ${ENVIRONMENT}

ENV SPRING_PROFILES_ACTIVE=${ENVIRONMENT}
CMD ["java", "-jar", "app.jar"]
docker build -t spring-test --build-arg ENVIRONMENT=dev .
  • Build parameter with dev
docker run spring-test

2021-03-21 11:49:11.706  INFO 1 --- [           main] c.e.board.boardapi.BoardApiApplication   : The following profiles are active: dev
  • The following profiles are active: dev

Leave a Reply

Your email address will not be published.

ANOTE.DEV