Docker issue with Spring boot 3.2.2 and PostgreSQL on AWS

I have been running into issues figuring out how to dockerize my image and upload it to aws. Here is my dockerfile:
# Stage 1: Build the application
FROM maven:3.8.4-openjdk-17 AS build

WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean install

# Stage 2: Run the application
FROM openjdk:21
WORKDIR /app
COPY --from=build /app/target/HarmonyTracker-BE-0.0.1-SNAPSHOT.jar ./demo-aws.jar
COPY src/main/resources/application.properties /app/application.properties
COPY src/main/resources/keys/AuthKey_Z3XCBR246A.p8 /app/keys/AuthKey_Z3XCBR246A.p8
EXPOSE 8080
CMD ["java", "-jar", "demo-aws.jar"]
# Stage 1: Build the application
FROM maven:3.8.4-openjdk-17 AS build

WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean install

# Stage 2: Run the application
FROM openjdk:21
WORKDIR /app
COPY --from=build /app/target/HarmonyTracker-BE-0.0.1-SNAPSHOT.jar ./demo-aws.jar
COPY src/main/resources/application.properties /app/application.properties
COPY src/main/resources/keys/AuthKey_Z3XCBR246A.p8 /app/keys/AuthKey_Z3XCBR246A.p8
EXPOSE 8080
CMD ["java", "-jar", "demo-aws.jar"]
This is my docker-compose:
version: '3.8'
services:
db:
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"

app:
build: .
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 123456
depends_on:
- db
version: '3.8'
services:
db:
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"

app:
build: .
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 123456
depends_on:
- db
when composing or building i get errors in postgres connection attempt failed.
in pom.xml:
in pom.xml:
<properties> <java.version>17</java.version> </properties>
and:
and:
xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> ``` I ran to so much issues because i was on java 21 and mvn had errors, but at this point i cannot figure it out at all Any help would be truly appreciated. Errors i get when running the container are usually postgres connection issue and tomcat server not starting issues, i am sure i am making a mistake in connecting the app to postgres but idk what i am missing
5 Replies
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @Ed! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Ed
EdOP11mo ago
in my application.properties i have the following:
#DATABASE SETUP
spring.sql.init.platform=postgres
spring.datasource.url=jdbc:postgresql://db:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.h2.console.enabled=true

#JWT Setup
application.security.jwt.secret-key=///
application.security.jwt.expiration=///
application.security.jwt.refresh-token.expiration=///

#Google OAuth
Google-Client-Id=///

#Facebook OAuth
fb-app-id=///
fb-client-secret=///
fb-auth-url=///
fb-user-details=///

#Apple OAuth
apple-iss=///
apple-client-id=///

#LOGGR:
logging.level.org.springframework.security=TRACE
#DATABASE SETUP
spring.sql.init.platform=postgres
spring.datasource.url=jdbc:postgresql://db:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.h2.console.enabled=true

#JWT Setup
application.security.jwt.secret-key=///
application.security.jwt.expiration=///
application.security.jwt.refresh-token.expiration=///

#Google OAuth
Google-Client-Id=///

#Facebook OAuth
fb-app-id=///
fb-client-secret=///
fb-auth-url=///
fb-user-details=///

#Apple OAuth
apple-iss=///
apple-client-id=///

#LOGGR:
logging.level.org.springframework.security=TRACE
Tomasm21
Tomasm2111mo ago
in my docker-compose at the very end I have:
/....
volumes:
dbdata:
/....
volumes:
dbdata:
maybe you should have:
volumes:
postgres_data:
volumes:
postgres_data:
Ed
EdOP11mo ago
@Tomasm21 I figured something out but i still not capable of building on aws for some reason. This is my compose:
version: '3.8'
services:
db:
image: postgres:13
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s

app:
image: .
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 123456
depends_on:
- db

volumes:
postgres_data:
version: '3.8'
services:
db:
image: postgres:13
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s

app:
image: .
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/postgres
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 123456
depends_on:
- db

volumes:
postgres_data:
I am building as follows
docker buildx build --platform linux/amd64 -t harmonytracker/demo-aws:6.0 .
docker buildx build --platform linux/amd64 -t harmonytracker/demo-aws:6.0 .
and pushing:
docker push harmonytracker/demo-aws:6.0
docker push harmonytracker/demo-aws:6.0
in aws i do :
sudo docker run -p 80:8080 harmonytracker/demo-aws:6.0
sudo docker run -p 80:8080 harmonytracker/demo-aws:6.0
but it still fails, however locally on intellije i can compose and everything works correctly
JavaBot
JavaBot11mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?