I have to deploy a web app with a Jetty Server. This app need a database, running on MariaDB. Here the docker-compose
file used to deploy the app:
version: '3.0'
services:
jetty:
build:
context: .
dockerfile: docker/jetty/Dockerfile
container_name: app-jetty
ports:
- "8080:8080"
depends_on:
- mariadb
networks:
- app
links:
- "mariadb:mariadb"
mariadb:
image: mariadb:10.7
container_name: app-mariadb
restart: always
environment:
MARIADB_ROOT_PASSWORD: myPassword
MARIADB_DATABASE: APPBD
ports:
- "3307:3306"
networks:
- app
adminer:
image: adminer
container_name: app-adminer
restart: always
ports:
- "2002:8080"
depends_on:
- mariadb
networks:
- app
networks:
app:
driver: bridge
The Dockerfile
used by the Jetty container:
FROM gradle:7.2-jdk17 as grad
WORKDIR /tmp
RUN mkdir src
RUN ls -la
COPY src /tmp/src
COPY build.gradle /tmp/build.gradle
RUN ls -la
RUN gradle --warning-mode all war
RUN ls -la /tmp/build/libs/
FROM jetty:latest
COPY --from=grad /tmp/build/libs/tmp.war /var/lib/jetty/webapps/ROOT.war
EXPOSE 8080
The app is build with Gradle before the initialisation of Jetty.
The problem is the database: it takes several minutes to initialize the MariaDB database. So, when I want to connect to the DB from Adminer, I have to wait until the DB is ready before I can log in. But for my app, I got this: Could not connect to address=(host=mariadb)(port=3306)(type=master) : Socket fail to connect to host:mariadb, port:3306. Connection refused
, even if the database is ready. I have to stop the app-jetty
container and restart it to use the database. I thought that depends_on
will ran the app-jetty
when the database was ready, but it ran the container when the app-mariadb
was ran.
I use JDBC to establish connection to the DB : jdbc:mariadb://mariadb:3306/APPBD?user=root&password=myPassword
. I successfully establish connection on local execution with gradle appRun
and use localhost:3306
instead of mariadb:3306
, so I think I don't have any mistakes on my code.
How can I indicate to the app-jetty
container to start only when MariaDB is ready?
CodePudding user response:
Compose (2.1) supports healthchecks:
Under the mariadb service:
healthcheck:
test: [ "CMD", "mariadb-admin", "--protocol", "tcp" ,"ping" ]
timeout: 3m
interval: 10s
retries: 10
ref: tip.