Issue
I am trying to deploy my Spring boot and react-based application on an Amazon EC2 instance. My health status has transitioned to a warning.
My logs of the aws ec2 instance are :
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.2)
2023-10-05T18:53:10.445Z INFO 1 --- [ restartedMain] c.a.fullstack.FullstackApplication : Starting FullstackApplication using Java 17.0.8.1 with PID 1 (/app/classes started by root in /)
2023-10-05T18:53:10.449Z INFO 1 --- [ restartedMain] c.a.fullstack.FullstackApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-05T18:53:10.884Z INFO 1 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-05T18:53:10.885Z INFO 1 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-05T18:53:15.413Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-10-05T18:53:15.432Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2023-10-05T18:53:15.711Z INFO 1 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.amigoes.fullstack.customer.CustomerRepository; If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2023-10-05T18:53:15.712Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 238 ms. Found 0 JDBC repository interfaces.
2023-10-05T18:53:15.795Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-10-05T18:53:15.823Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-10-05T18:53:16.087Z INFO 1 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 224 ms. Found 1 JPA repository interfaces.
2023-10-05T18:53:21.480Z INFO 1 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-10-05T18:53:21.563Z INFO 1 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-05T18:53:21.563Z INFO 1 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.11]
2023-10-05T18:53:21.849Z INFO 1 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-05T18:53:21.857Z INFO 1 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 10659 ms
2023-10-05T18:53:22.467Z INFO 1 --- [ restartedMain] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 9.16.3 by Redgate
2023-10-05T18:53:22.467Z INFO 1 --- [ restartedMain] o.f.c.internal.license.VersionPrinter : See release notes here: https://rd.gt/416ObMi
2023-10-05T18:53:22.467Z INFO 1 --- [ restartedMain] o.f.c.internal.license.VersionPrinter :
2023-10-05T18:53:22.480Z INFO 1 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
----------------------------------------
/var/log/containers/fullstack-f15b80382c26-stdouterr.log
----------------------------------------
----------------------------------------
/var/log/containers/frontend-react-3d88b92cdce9-stdouterr.log
----------------------------------------
exec /usr/local/bin/docker-entrypoint.sh: exec format error
----------------------------------------
/var/log/containers/frontend-react-64e593b5f8a9-stdouterr.log
----------------------------------------
exec /usr/local/bin/docker-entrypoint.sh: exec format error
----------------------------------------
/var/log/containers/frontend-react-bd3ac2e1dd79-stdouterr.log
----------------------------------------
exec /usr/local/bin/docker-entrypoint.sh: exec format error
----------------------------------------
/var/log/containers/fullstack-0767fd89d97b-stdouterr.log
----------------------------------------
----------------------------------------
/var/log/containers/fullstack-6396590b65cf-stdouterr.log
----------------------------------------
----------------------------------------
/var/log/containers/fullstack-8c5a0a96b6c1-stdouterr.log
----------------------------------------
My Docker file of react is:
FROM node:20-alpine
ARG api_base_url
WORKDIR /app
COPY package*.json ./
RUN npm i --silent
COPY . .
RUN echo "VITE_API_BASE_URL=${api_base_url}" > .env
EXPOSE 5173
CMD [ "npm", "run", "dev" ]
My docker compose:
services:
db:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456789
PGDATA: /data/postgres
volumes:
- db:/data/postgres
ports:
- "5433:5432"
networks:
- db
restart: unless-stopped
fullstack-api:
container_name: fullstack-api
image: ahmadmujtaba200210/fullstack
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/customer
ports:
- "8088:8080"
networks:
- db
depends_on:
- db
restart: unless-stopped
fullstack-frontend-react:
container_name: frontend-react
image: ahmadmujtaba200210/fullstack-react
build:
dockerfile: Dockerfile
context: frontend/react
args:
api_base_url: http://localhost:8088
ports:
- "3000:5173"
depends_on:
- fullstack-api
restart: unless-stopped
networks:
db:
driver: bridge
volumes:
db:
My Dockerrun.aws.json file is :
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "fullstack-react",
"image": "ahmadmujtaba200210/fullstack-react:latest",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 80,
"containerPort": 5137
}
]
},
{
"name": "fullstack",
"image": "ahmadmujtaba200210/fullstack:04.10.2023.05.02.43",
"essential": true,
"memory": 512,
"portMappings": [
{
"hostPort": 8080,
"containerPort": 8080
}
],
"environment": [
{
"name": "SPRING_DATASOURCE_URL",
"value": "jdbc:postgresql://awseb-e-ewuh4fh3ed-stack-awsebrdsdatabase-pyslfllvhlal.cw2qjcr4sn8j.us-east-2.rds.amazonaws.com:5432/customer"
}
]
}
]
}
I tried to build docker image with aws database url which was successful but my frontend application is on react can understand why it is not working, why it is not being deployed?
Solution
Let's break down the issues and solve them step by step:
Exec format error:
You have multiple log entries:
exec /usr/local/bin/docker-entrypoint.sh: exec format error
This error usually indicates that there's a problem with the entry point script (
docker-entrypoint.sh
) for your container. The most common causes for this error include:- The entry point script does not have the executable permissions.
- The entry point script has Windows-style line endings (if you developed on Windows) instead of UNIX-style line endings.
To solve this:
- Ensure the script has executable permissions:
chmod +x /path/to/your/docker-entrypoint.sh
- If you're developing on Windows, you can convert the line endings using tools like
dos2unix
or any text editor that allows you to convert line endings.
Mismatched Ports:
In your
Dockerfile
for the React application, you're exposing port5173
:EXPOSE 5173
However, in the
Dockerrun.aws.json
file, the container port you're trying to map is5137
:{ "hostPort": 80, "containerPort": 5137 }
This is a mismatch. You need to ensure that both the ports are the same, either
5173
or5137
, whichever is the correct one.Environment Variable for React Frontend:
Your React frontend likely needs to know the URL of your backend API to make calls to it. In the Docker Compose file, you're passing this information as an argument to the Docker build:
build: args: api_base_url: http://localhost:8088
This only works if you're building your Docker image every time you deploy, and that the API URL does not change. If you have a static API URL, it's better to pass it as an environment variable to the running container, similar to how you pass the
SPRING_DATASOURCE_URL
to your Spring Boot application.Networking on AWS:
Docker Compose creates a custom network for the services defined in a
docker-compose.yml
file, allowing them to communicate with each other using the service names as hostnames (e.g.,db
for the database,fullstack-api
for the API). However,Dockerrun.aws.json
doesn't create such a network by default.If your React frontend communicates with the Spring Boot backend, ensure they are on the same network, or the React frontend knows the correct URL to communicate with the backend.
DevTools in Production:
The logs indicate that Spring Boot's DevTools is active:
.e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active!
It's not recommended to have DevTools active in a production environment. Ensure that DevTools is not included in the production build of your application.
Database Connection:
Ensure that the database hosted on RDS is accessible from the EC2 instance where your application is deployed. Security groups, VPC configurations, or the RDS instance's public accessibility setting can affect this.
After addressing these issues, try redeploying your application. If you continue to encounter issues, check the logs for any new or persisting errors.
Answered By - heyValdemar Answer Checked By - Pedro (WPSolving Volunteer)