Spring-boot application gets stuck on startup randomly - java

Spring-boot application gets stuck on startup randomly at:
The postgres gets started then it gets stuck.
Only one log from the Java application is:
Listening for transport dt_socket at address: 8000
It's a brand new application. I am using spring-boot 2.6.6, Spring 5.3.18, JDK 17, and Hibernate 5.6.7.Final with docker-compose.
Here is my docker-compose.yml
version: '3.7'
services:
postgres:
image: postgis:11-alpine
command: postgres -c stats_temp_directory=/tmp
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=password!
- POSTGRES_USER=xxx
- POSTGRES_DB=xxx
volumes:
- ./postgres-data:/var/lib/postgresql/data
networks:
xxx-network:
fargate-xxx:
depends_on:
- postgres
links:
- postgres:postgres
image: fargate-xxx-local-build
build:
context: ""
dockerfile: Dockerfile
ports:
- 8080:8080
- 8000:8000
networks:
xxx-network:
dns:
- 8.8.8.8
- 8.8.4.4
environment:
ENVIRONMENT: ${ENVIRONMENT}
JAVA_OPTS: -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
volumes:
- ./dynamicconfig.properties:/tmp/dynamicconfig.properties
volumes: {}
networks:
xxx-network:
driver: bridge
Dockerfile
FROM amazoncorretto:17-alpine
EXPOSE 8000
COPY app.jar app.jar
ENTRYPOINT exec java ${JAVA_OPTS} -jar app.jar
BTW, I'm on M1.

Related

IntelliJ and Docker: Create a remote debug configuration: custom command

I am trying to run my java application (a project at my uni) in debug mode in IntelliJ with a dockerfile.
I found this tutorial:
https://www.jetbrains.com/help/idea/debug-a-java-application-using-a-dockerfile.html#create-remote-debug-config
I tried to follow each step (#create-remote-debug-configuration), but at 4:
Select the Docker configuration that runs your app (MyPassApp) and
specify the command to use when running your app in the Custom Command
field. The remote debug configuration will use this custom command
instead of the one defined in the Dockerfile. This command should
contain the -agentlib option to let the debugger attach to the
process:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 -jar JavaPassFromConsole.jar
I don't know what any of those commands mean, so I dont know what to put in the custom command section.
This is my docker-compose.yml file:
version: '3'
volumes: mysql_data: {}
networks:
back:
services:
backend:
build:
context: '.'
dockerfile: 'docker_config/backend/Dockerfile'
depends_on:
- db
links:
- db
ports:
- 8080:8080
- 9990:9990
environment:
CONTACT_USERNAME: <XXX>
CONTACT_PASSWORD: <XXX>
networks:
- back db:
container_name: db
image: mysql:5.7
restart: always
volumes:
- mysql_data:/var/lib/mysql
command: --lower_case_table_names=1
environment:
MYSQL_DATABASE: <XXX>
MYSQL_ROOT_PASSWORD: root
networks:
- back
ports:
- 3306:3306
- 5005:5005 phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 4000:80
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- back
Any help is appreciated!!

unable to prepare context: path "./mile-restoraunt-app" not found

I am trying to build a local network for two services with a docker compose file.
service mysqldb - the data base for the spring boot app.
service app - the spring boot app.
this is the error i am receiving:
these are my files
Dockerfile
FROM maven:3.8.3-jdk-8
WORKDIR /mile-restoraunt-app
COPY . .
RUN mvn clean install
CMD mvn spring-boot:run
docker-compose.yml file
version: "3.8"
services:
mysqldb:
image: mysql
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
app:
depends_on:
- mysqldb
build: ./mile-restoraunt-app
restart: on-failure
env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
.env file
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=thanatos
MYSQLDB_DATABASE=restaurant
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
SPRING_LOCAL_PORT=6868
SPRING_DOCKER_PORT=8080
And this is my directory
in docker-compose.yaml for a app you specified build: ./mile-restoraunt-app, but you run your command inside the mile-restoraunt-app folder. You need to change the build folder.

API-gateway never getting config from config server when both are run with docker compose

I have three services.
Config server
Eureka server
api-gateway
If I run them individually it's working fine. Then I am trying to introduce docker on above services. So I have prepare 3 dockerfile for each services:
VOLUME /tmp
ADD config-server/build/libs/config-server-0.0.1-SNAPSHOT.jar config-server-0.0.1-SNAPSHOT.jar
CMD ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]
VOLUME /var/lib/config-repo
EXPOSE 10270
FROM java:8
VOLUME /tmp
ADD eureka-server/build/libs/eureka-server-1.0-SNAPSHOT.jar eureka-server-1.0-SNAPSHOT.jar
CMD ["java","-jar","eureka-server-1.0-SNAPSHOT.jar"]
EXPOSE 10210
FROM java:8
VOLUME /tmp
ADD api-gateway/build/libs/api-gateway-0.0.1-SNAPSHOT.jar api-gateway-0.0.1-SNAPSHOT.jar
RUN bash -c 'touch /api-gateway-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/api-gateway-0.0.1-SNAPSHOT.jar"]
And then I have prepared my docker-compose file:
version: '3'
services:
eureka-server:
restart: always
container_name: eureka-server
build:
context: .
dockerfile: eureka-server/Dockerfile_eureka_server
expose:
- 10210
ports:
- 10210:10210
networks:
- servicenet
config-server:
restart: always
container_name: config-server
build:
context: .
dockerfile: config-server/Dockerfile_config_server
expose:
- 10270
ports:
- 10270:10270
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:10270"]
interval: 30s
timeout: 10s
retries: 5
networks:
- servicenet
api-gateway:
restart: on-failure
container_name: api-gateway
build:
context: .
dockerfile: api-gateway/Dockerfile_api_gateway
expose:
- 10200
ports:
- 10200:10200
networks:
- servicenet
links:
- config-server
- eureka-server
networks:
servicenet:
driver: bridge
But api-gateway start before config-server completely start its service. Thats why api gateway start on 8080 port and looking for eureka server at host localhost and port 8621. though its not getting this ports and hosts in docker its continuing looking for eureka server but not again fetching config from config server. Is there anything wrong with my configuration?
My application.properties file on github like this
server.port=10200
#Eureka configuration
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
lombok.equalsAndHashCode.callSuper = call
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.client.registryFetchIntervalSeconds=5
eureka.client.serviceUrl.defaultZone=http://eureka-server:10210/eureka
spring.cloud.service-registry.auto-registration.enabled=true
eureka.client.enabled=true
eureka.client.serviceUrl.registerWithEureka=true
lombok.anyConstructor.suppressConstructorProperties = true
#Zuul Configuration
# A prefix that can added to beginning of all requests.
#zuul.prefix=/api
# Disable accessing services using service name (i.e. gallery-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*
# Map paths to services
zuul.routes.gallery-service.path=/gallery/**
zuul.routes.gallery-service.service-id=gallery-manager
zuul.routes.image-service.path=/image/**
zuul.routes.image-service.service-id=image-service
zuul.routes.book-manager.path=/book-manager/**
zuul.routes.book-manager.service-id=book-manager
zuul.routes.auth-service.path=/auth/**
zuul.routes.auth-service.service-id=auth-manager
zuul.routes.remote-library.path=/remote/**
zuul.routes.remote-library.service-id=remote-library
#zuul.routes.auth-service.strip-prefix=false
# Exclude authorization from sensitive headers
zuul.routes.auth-service.sensitive-headers=Cookie,Set-Cookie
NB: If I try with other services rather than api-gateway its working fine. I am using zuul proxy for api gateway service.
A quick fix would be to add a 'depends_on' clause so that the api-service depends on the config server - that way the api-service won't start until the config server is up.
To make this workable we have to wait still other services are up. So I have written a script for waiting and rewrite docker compose like this
version: '3'
services:
eureka-server:
restart: always
container_name: eureka-server
build:
context: .
dockerfile: eureka-server/Dockerfile_eureka_server
expose:
- 10210
ports:
- 10210:10210
networks:
- servicenet
config-server:
restart: always
container_name: config-server
build:
context: .
dockerfile: config-server/Dockerfile_config_server
expose:
- 10270
ports:
- 10270:10270
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:10270"]
interval: 30s
timeout: 10s
retries: 5
networks:
- servicenet
api-gateway:
restart: always
container_name: api-gateway
build:
context: .
dockerfile: api-gateway/Dockerfile_api_gateway
expose:
- 10200
ports:
- 10200:10200
networks:
- servicenet
links:
- config-server
depends_on:
- config-server
command: ["./wait-for-it.sh", "config-server:10270","--","./wait-for-it.sh", "eureka-server:10210", "--", "java","-jar", "api-gateway-0.0.1-SNAPSHOT.jar"]
And also rewrite docker files like this(for api-gateway)
FROM java:8
VOLUME /tmp
ADD api-gateway/build/libs/api-gateway-0.0.1-SNAPSHOT.jar api-gateway-0.0.1-SNAPSHOT.jar
ADD wait-for-it.sh wait-for-it.sh
RUN chmod +x wait-for-it.sh

container discovery in docker-compose

I've the following docker-compose, how can moduleA find what IP or port the moduleB is running on so it can make a REST call to it.
question is how should I configure docker properties that moduleA get's the ip port of ModuleB
version: "3"
services:
moduleA:
image: jboss/wildfly
ports:
- 9080:8080
- 9990:9990
- 65193:65193
volumes:
- ./ModuleA/target/ModuleA.war:/opt/jboss/wildfly/standalone/deployments/ModuleA.war
environment:
- MONGO_HOST=mongo
- MONGO_PORT=27017
- MONGO_USERNAME=myuser
- MONGO_PASSWORD=mypass
- MONGO_DATABASE=mydb
- MONGO_AUTHDB=admin
command: >
bash -c "/opt/jboss/wildfly/bin/add-user.sh admin Admin#007 --silent && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"
links:
- mongo
depends_on:
- mongo
moduleb:
image: jboss/wildfly
ports:
- 9081:8080
- 9991:9990
- 65194:65193
volumes:
- ./ModuleB/target/ModuleB.war:/opt/jboss/wildfly/standalone/deployments/ModuleB.war
environment:
- MONGO_HOST=mongo
- MONGO_PORT=27017
- MONGO_USERNAME=myuser
- MONGO_PASSWORD=mypass
- MONGO_DATABASE=mydb
command: >
bash -c "/opt/jboss/wildfly/bin/add-user.sh admin Admin#007 --silent && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"
links:
- mongo
depends_on:
- mongo
mongo:
image: mongo:4.0.10
container_name: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=mongouser
- MONGO_INITDB_ROOT_PASSWORD=mongopass
ports:
- 27017:27017
- 27018:27018
- 27019:27019
I've the ports hard coded, are we supposed to inject hard coded ports or how does it work?
You can link moduleA in moduleB same way you did for mongo. And then either use the same in your environment to define MODULEA_HOST or use in your properties file.

Java service in docker container not connecting to host mysql

I have Java microservices running in docker container which is not able to connect to mysql hosted locally.
docker is running in network having ip address as 172.0...
If I execute Java service directly as java -jar, it is able to connect to mysql running in 10.0..
docker-compose file
version: '2.0'
services:
config-server:
image: test/config-server
container_name: config-server
environment:
- GIT_USERNAME=${GIT_USERNAME}
- GIT_PASSWORD=${GIT_PASSWORD}
ports:
- 8889:8889
entrypoint: ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Drun.arguments=GIT_USERNAME=${GIT_USERNAME}, GIT_PASSWORD=${GIT_PASSWORD} -Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
discovery-server:
image: test/discovery-server
container_name: discovery-server
links:
- config-server
depends_on:
- config-server
entrypoint: ["./wait-for-it.sh","config-server:8889","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8761:8761
web-authentication:
image: test/web-authentication
container_name: web-authentication
links:
- config-server
- discovery-server
depends_on:
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8444:8444
gateway:
image: test/gateway
container_name: gateway
links:
- config-server
- discovery-server
- web-authentication
depends_on:
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 81:8765
The issue was resolved after configuring networks configuration in docker-compose.yml, the issue was mysql and Docker containers were running in different subnet.

Categories

Resources