I am trying to put an application on docker spring-boot + mysql. The problem is that I am working on a MacBook M1 and all my attempts fail.
I get the database image from dockerhub, the application image also builds for me without any hindrance but the application won't start because there is a database connection error.
My Dockerfile
FROM arm64v8/openjdk:11.0.15-jdk-bullseye
ADD target/patient-registration-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD java -jar patient-registration-0.0.1-SNAPSHOT.jar
My docker-compose.yml
version: '3.8'
services:
database:
container_name: patient_db
image: 'mysql:8.0.22'
platform: linux/x86_64
environment:
- MYSQL_ROOT_PASSWORD=mysql
- MYSQL_USER=mysql
- MYSQL=patient_db
- MYSQL_PORT=3306
restart: always
patient_reservation:
image: 'patient_reservation'
build: .
ports:
- "8282:8282"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://database:3306/patient_db?useUnicode=true&characterEncoding=utf8&useSSLdo=false&useLegacyDatetimeCode=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
- SPRING_DATASOURCE_USERNAME=mysql
- SPRING_DATASOURCE_PASSWORD=mysql
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
restart: always
depends_on:
- database
I would greatly appreciate it!
Thank you in advance !
Related
I have a Java application consuming binlogs from Mysql database and interacting with another service which I have hosted on my localhost. How do I create a Docker image with all these services so that I can run them all at once?
From what I've heard, I need to use docker-compose. But I don't really know how to?
Do I create separate Docker containers and link their ports?
Do I create one container?
I have created one Dockerfile for the Java application:
FROM maslick/minimalka:jdk11
WORKDIR /app
EXPOSE 8080
COPY target/foster-maxwell-uber-jar.jar foster-maxwell.jar
COPY config.properties config.properties
CMD java -jar foster-maxwell.jar --config_type MAXWELL_UNSIGNED_KAAS --kaas_config config.yml --zk_timeout 1000
How do I go about it? Sorry for the noob question.
You should read docker-compose documentation: https://docs.docker.com/compose/compose-file/
For example, try with a docker-compose.yml file like this
version: '3.8'
services:
your-java-app1:
build:
context: . <-- (dir where you have your Dockerfile)
dockerfile: Dockerfile
image: yourjavaapp1image:1.0
container_name: your-java-app1
depends_on:
- db
your-java-app2:
build:
context: . <-- (dir where you have your Dockerfile_for_app2)
dockerfile: Dockerfile_for_app2
image: yourjavaapp2image:1.0
container_name: your-java-app2
depends_on:
- db
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: [put here your root pass]
MYSQL_DATABASE: [put here your db name]
MYSQL_USER: [put here your user]
MYSQL_PASSWORD: [put here your pass]
Once you have this defined your Dockerfiles, first, build images with your apps:
docker-compose build
Then, run images (container creations)
docker-compose up -d
My web app can't connect to the MongoDB container
here are my application.yml
spring:
data:
mongodb:
uri: mongodb://mongo:27017
host: mongo
port: 27017
database: my-db-name
and this is my Docker-Compose
version: "3"
services:
java:
build:
context: ./
ports:
- "8080:8080"
links:
- mongo
depends_on:
- mongo
networks:
- shared-net
mongo:
image: 'mongo'
ports:
- 27017:27017
hostname: mongo
volumes:
- ./data/db:/data/db
networks:
- shared-net
networks:
shared-net:
driver: bridge
and this is the Dockerfile wrote for running java
FROM openjdk:11
COPY ./code/lemon-backend/target/lemon-0.0.1-SNAPSHOT.jar /usr/src/
WORKDIR /usr/src/
EXPOSE 8080
CMD ["java", "-jar", "lemon-0.0.1-SNAPSHOT.jar"]
I can't even build the application using these options
I get this exception:
org.mongodb.driver.cluster: Exception in monitor thread while connecting to server mongo:27017
if possible try giving solutions with docker-compose, thanks
OLD VERSION ANSWER
IMPORTANT NOTE:
older versions of MongoDB ignore this configuration in application.properties, proceed ahead & use the new solutions I added
This workaround is used for old versions of spring and mongo that ignore the normal configuration (other than uri)
. I had a warning that this property cant be resolved
but hopefully, it worked :)
dockerspring.data.mongodb.uri= mongodb://<your_mongodb_container_name>:27017/<name_of_your_db>
the mongodb part is not changeable but mongo before the port number is actually the name of the container witch you have specified in your docker-compose
SPRING BOOT SOLUTION
spring:
data:
mongodb:
host: <mongo-db-container-name>
port: <mongo-db-port>
database: <database-name>
DOCKER SOLUTION
In Your Dockerfile Add This Option For Executing Java
ENTRYPOINT [“java”,”-Dspring.data.mongodb.uri=mongodb://mongo:27017/name_of_your_db”, “-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/<name_of_your_app>.jar”]
Linking Java And Mongo Containers + Giving Them Names
here this is my final docker-compose.yml,
I hope that it helps you
version: "3"
services:
java:
build:
context: ./
ports:
- "8080:8080"
container_name: java
links:
- mongo
depends_on:
- mongo
networks:
- shared-net
mongo:
image: 'mongo'
ports:
- 27017:27017
container_name: mongo
volumes:
- /home/sinoed/data/db:/data/db
networks:
- shared-net
networks:
shared-net:
driver: bridge
Compare this version and the one specified in the question carefully
I'm trying to run 3 containers through docker-compose, with postgres, cassandra and my webapp, which has a embedded tomcat server with some dependencies as ARP/Native. This libraries are located in a folder called "lib" at jar's same level.
I'm running a PoC on Windows 10 (using Linux containers) before moving it to a CentOS server, if it works on the PoC. I searched over the net and seems like is not an isolated problem but or I have no find the solution, or the solution showed didn't work for me. Here is my docker-compose.yml, with all the related files/folders stored at same level:
version: '3.1'
services:
fulmar-webapp:
container_name: "my-webapp"
image: openjdk:11-jre-slim
hostname: mywebapp
volumes:
- ./lib:/home/lib
- ./fulmar-1.0.1-SNAPSHOT-exec.jar:/home/mywebapp-1.0.1-SNAPSHOT-exec.jar
entrypoint:
- java
- -jar
- /home/mywebapp-1.0.1-SNAPSHOT-exec.jar
environment:
- LD_LIBRARY_PATH:/home/lib
- spring.datasource.url=jdbc:postgresql://postgresql:5432/mydb
- spring.datasource.username=postgres
- spring.datasource.password=postgres
- spring.jpa.hibernate.ddlAuto=update
network_mode: bridge
ports:
- 8443:8443
- 8080:8080
links:
- postgresql
- cassandra
postgresql:
container_name: "mydb"
image: postgres:11.1-alpine
restart: always
environment:
POSTGRES_DB: mydb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql
- postgresdata:/var/lib/postgresql/data
ports:
- 5432:5432
network_mode: bridge
cassandra:
container_name: "cassandra"
image: cassandra
ports:
- 9042:9042
network_mode: bridge
volumes:
postgresdata:
Not sure if is not properly mapping the folder with the libraries, or is not actually mounting the volume. This is exactly the Environment var I need to put in there:
Environment=LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/libraries/lib
Two results I have encountered:
1-Tomcat exception cause it can not find the libraries:
[ERROR][org.apache.catalina.util.LifecycleBase#log(175)] Failed to
initialize component [Connector[org.apache.coyote.http11.Http11AprProtocol-
8080]] | org.apache.catalina.LifecycleException: The configured protocol
[org.apache.coyote.http11.Http11AprProtocol] requires the APR/native library
which is not available
2-WARNING: The LD_LIBRARY_PATH variable is not set. Defaulting to a blank string.
Thanks you all in advance
EDIT: just let you know the once I run docker-compose up, and my app throws this exception, the container is no longer available so I'm unable to run any commands in it
You have a wrong syntax, it should be like this:
environment:
- LD_LIBRARY_PATH=/home/lib
I'm using docker-compose to run 3 containers:
My webapplication
Postgres
Cassandra
Once I use: docker-compose up
My webapp launches this exception:
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s)
tried for query failed (tried: cassandra/172.17.0.3:9042
Once all containers are running, I'm able to enter into my webapps and try to ping cassandras container before it dies (webapp container), and all packets are successfully returned so I guess there actually IS connectivity between them.
The weirdest thing is that once I got this exception:
.InvalidQueryException: Keyspace 'myKeyspace' does not exist
Which means connection has been stablished, but was before I add persistence and created the mentioned schema, but I did change nothing on my compose.yml to get this new result
Here is my docker-compose.yml:
version: '3.1'
services:
cassandra:
container_name: "cassandra"
image: cassandra
ports:
- 9042:9042
volumes:
- /home/cassandra:/var/lib/cassandra
postgresql:
container_name: "postgresql"
image: postgres:11.1-alpine
restart: always
environment:
POSTGRES_DB: mywebapp
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
#- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql
- postgresdata:/var/lib/postgresql/data
ports:
- 5432:5432
mywebapp:
container_name: "mywebapp"
image: openjdk:10-jre-slim
hostname: mywebapp
volumes:
- ./lib:/home/lib
- ./mywebapp-1.0.1-SNAPSHOT-exec.jar:/home/mywebapp-1.0.1-SNAPSHOT-exec.jar
entrypoint:
- java
- -jar
- -Djava.library.path=/home/lib
- /home/mywebapp-1.0.1-SNAPSHOT-exec.jar
environment:
- LD_LIBRARY_PATH=/home/lib
- spring.datasource.url=jdbc:postgresql://postgresql:5432/mywebapp
- spring.cassandra.contactpoints=cassandra
- spring.cassandra.port=9042
- spring.cassandra.keyspace=mywebapp
#- spring.datasource.username=postgres
#- spring.datasource.password=postgres
#- spring.jpa.hibernate.ddlAuto=update+
ports:
- 8443:8443
- 8080:8080
depends_on:
- cassandra
volumes:
postgresdata:
Thank you all in advance
I am assuming your web app requires for the cassandra service to be running when it starts. You should add depends_on entry to your web app service so docker starts it only when cassandra is started
And the links entry is not necessary as docker automatically will use the service names as hostnames in the network created for this docker-compose project. Same goes for the network_type: bridge - that is the default network type, so you can omit that in your case.
Why when I am running docker-compose with the file below, and I am trying to run localhost:8080/todo-application-1.0/ I am getting internal error within is mentioned that jdbc cannot connect. But when I am running the same docker-compose file without web service I am able to connect to the database, can anyone explain to me what is wrong? How to successfully create a docker-compose or dockerfile that will copy the .war with the application and run it without any issues with the connection.
version: '3.3'
services:
database:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: 'todo-app'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'admin'
MYSQL_ROOT_PASSWORD: 'admin'
ports:
- '3306:3306'
expose:
- '3306'
web:
image: tomcat
ports:
- "8080:8080"
volumes:
- ./build/libs/todo-application-1.0.war:/usr/local/tomcat/webapps/todo-application-1.0.war