Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
chore: set env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
knjk04 committed Mar 13, 2022
1 parent d5baf45 commit 30f1c6e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These settings are for local development

DB_PORT=5433
DB_NAME=db
DB_PORT=5432
DB_NAME=book_project_db
DB_USERNAME=dbuser
DB_PASSWORD=dbpassword
17 changes: 16 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@ RUN --mount=type=cache,target=/root/.m2 mvn -f /app/pom.xml $mvn_arg

# Run jar
FROM openjdk:11-jre-slim

ARG JAR_FILE=/app/book-app/target/book-app-*.jar
ARG PROFILE="local"
ENV profile_env ${PROFILE}

ARG DB_HOSTNAME
ENV db_hostname_env ${DB_HOSTNAME}
ARG DB_PORT
ENV db_port_env ${DB_PORT}
ARG DB_NAME
ENV db_name_env ${DB_NAME}
ARG DB_USERNAME
ENV db_username_env ${DB_USERNAME}
ARG DB_PASSWORD
ENV db_password_env ${DB_PASSWORD}

RUN echo ${DB_HOSTNAME}
RUN echo ${db_hostname_env}

COPY --from=builder $JAR_FILE /opt/bookapp/app.jar
WORKDIR /opt/bookapp/

RUN useradd -m myuser
USER myuser
EXPOSE 8080
CMD ["java", "-jar", "-Dspring.profiles.active=${profile_env}", "app.jar"]
CMD ["java", "-jar", "-Dspring.profiles.active=${profile_env}", "-Dspring.datasource.url=jdbc:postgresql://${db_hostname_env}:5432/${db_name_env}", "-Dspring.datasource.username=${db_username_env}", "-Dspring.datasource.password=${db_password_env}", "app.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ spring.banner.image.location=banner.txt
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

# Uncomment the line below if not running the backend in Docker:
#spring.datasource.url = jdbc:postgresql://localhost:5433/book_project_db
# comment out the line below if not running the backend in Docker:
spring.datasource.url = jdbc:postgresql://book_project_db:5432/book_project_db
spring.datasource.username = dbuser
spring.datasource.password = dbpassword
# spring.datasource.url = jdbc:postgresql://${DB_HOSTNAME}:${DB_PORT}/${DB_NAME}

spring.datasource.driver-class-name=org.postgresql.Driver

spring.flyway.enabled = true
Expand Down
11 changes: 8 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
container_name: book_project_db
restart: always
environment:
- POSTGRES_DB=book_project_db
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
Expand All @@ -29,15 +29,20 @@ services:
dockerfile: ./Dockerfile
args:
mvn_arg: clean package -DskipTests
DB_HOSTNAME: book_project_db
DB_PORT: ${DB_PORT}
DB_NAME: ${DB_NAME}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
restart: "no"
ports:
- "8080:8080"
environment:
- POSTGRES_DATABASE=book_project_db
- POSTGRES_DATABASE=${DB_NAME}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_HOSTNAME=db
- POSTGRES_PORT=${DB_PORT}
- POSTGRES_PORT=5433
depends_on:
- db

0 comments on commit 30f1c6e

Please sign in to comment.