grails/mysql with docker compose - java

I am trying to run a grails app in docker and keep running in some mysql connection problems. I can't figure out where the problem is.
This is my docker-compose file
version: "2"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root2017
MYSQL_DATABASE: dbname
MYSQL_USER: Dbuser
MYSQL_PASSWORD: passw
grails:
depends_on:
- db
ports:
- "9001:9001"
restart: always
environment:
DB_HOST: db:3306
DB_PASSWORD: passw
volumes:
db_data:
The grails app does not start with the following error:
ERROR 18:08:05 org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
grails_1 | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

You will need to configure your datasource to point to "jdbc:mysql://db:3306/dbname"
You can do this with external config file for production env.
Or you can read the values for host and db name from system environments.
See Deploying grails application war to tomcat with docker and docker compose

Related

Error creating a docker-compose connecting a java and a mysql containers

I am trying to connect the container of my springboot application with the container of a mysql image using docker-compose, however when I run docker-compose up my terminal starts a loop where it starts the spring application, try to connect with the MySQL container, fails and keep trying. The error that I get is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failures
docker-compose file:
version: '3.8'
services:
mysqldb:
image: mysql
platform: linux/x86_64
env_file: ./.env
restart: always
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: .
restart: always
env_file: ./.env
ports:
- $APP_LOCAL_PORT:$APP_DOCKER_PORT
environment:
- DB_HOST=mysqldb
- DB_USER=$MYSQLDB_USER
- DB_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- DB_NAME=$MYSQLDB_DATABASE
- DB_PORT=$MYSQLDB_DOCKER_PORT
stdin_open: true
tty: true
volumes:
db:
.env:
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=12345678
MYSQLDB_DATABASE=dronefeederdb
MYSQLDB_LOCAL_PORT=3306
MYSQLDB_DOCKER_PORT=3306
APP_LOCAL_PORT=8080
APP_DOCKER_PORT=8080
Application.yaml:
server:
port: 8080
spring:
datasource:
username: ${DB_USER}
password: ${DB_PASSWORD}
url: jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}
jpa:
hibernate:
ddl-auto: update
show-sql: true
open-in-view: false
#https://ia-tec-development.medium.com/lombok-e-spring-data-jpa-142398897733
security.user:
name: dronefeeder
password: dronefeeder
#https://www.baeldung.com/spring-boot-security-autoconfiguration
resilience4j.circuitbreaker:
configs:
default:
waitDurationInOpenState: 10s
failureRateThreshold: 10
#instances:
#estudantes:
#baseConfig: default
Dockerfile:
FROM openjdk:11.0-jdk as build-image
WORKDIR /app
COPY . .
RUN ./mvnw clean package -DskipTests
FROM openjdk:11.0-jre
COPY --from=build-image /app/target/*.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"]
Repository link:
https://github.com/julia-baptista/dronefeeder/tree/docker-configuration
I believe the issue is your application's use of localhost for the SQL URL in the Application.yaml property file. Since your app runs on a container by itself it tries to look at localhost of the container, while your SQL server is in another container, with its own localhost. Localhost in docker container do not refer to the host, they refer to the localhost within the container itself. If you want to access the host machine, this is an excellent answer From inside of a Docker container, how do I connect to the localhost of the machine?
url: jdbc:mysql://localhost:3306/dronefeederdb
localhost should not be used, you need to use the sql continainer url.
The fastest option is to use host.docker.internal instead of localhost. But it's not the best.
Another quick option is to run the two containers on the same docker network. Define that in your compose file the same way as the volumes. Then set each container to that network. See Networking in Compose. Then you can set your SQL url to use the SQL container name instead of localhost. So this..
url: jdbc:mysql://localhost:3306/dronefeederdb becomes url: jdbc:mysql://mysql/dronefeederdb
Neither option is robust, since you're hardcoding the container name in the application property file. A better solution is to have an environment variable in your webApp image that can accept the URL to the SQL server. Then you can provide the SQL location when running the container, or in your compose file (Environment variables in Compose). This way the SQL server can be anywhere.
Update:
There were a couple of issues in the compose and env files that caused mySQL container to fail startup. Thus the webApp was not able to connect.
Credentials
MYSQL_USER was set to root. mySql already creates the user root. You cannot create it again. I changed that to foo. See the Environment Variables section in the official docker image readme for more.
MYSQL_PASSWORD was not set. This is the password for the user your app will use. I set this to pass!123
The apps DB_PASSWORD was set to user root. That would have been ok if sql had started and it was using the root user I guess. But I changed that to the non-root user since were setting DB_USER=foo
Network was not defined
The two containers need to be on the same "docker network" if they are to run together in docker in the same machine. There's more to this which is beyond my experience. But in this case it needs to be on the same network for app to access mysqldb by its container name. I created dronefeederNet and added each container to it.
Files:
.env
MYSQLDB_USER=foo
MYSQLDB_PASSWORD=pass!123
MYSQLDB_ROOT_PASSWORD=12345678
MYSQLDB_DATABASE=dronefeederdb
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
APP_LOCAL_PORT=8081
APP_DOCKER_PORT=8080
docker-compose.yml
version: '3.8'
services:
mysqldb:
image: mysql
platform: linux/x86_64
env_file: ./.env
restart: always
environment:
- MYSQL_USER=$MYSQLDB_USER
- MYSQL_PASSWORD=$MYSQLDB_PASSWORD
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
networks:
- dronefeederNet
app:
depends_on:
- mysqldb
build: .
restart: always
env_file: ./.env
ports:
- $APP_LOCAL_PORT:$APP_DOCKER_PORT
environment:
- DB_HOST=mysqldb
- DB_USER=$MYSQLDB_USER
- DB_PASSWORD=$MYSQLDB_PASSWORD
- DB_NAME=$MYSQLDB_DATABASE
- DB_PORT=$MYSQLDB_DOCKER_PORT
stdin_open: true
tty: true
networks:
- dronefeederNet
volumes:
db:
networks:
dronefeederNet:
Give this a try and I hope it runs. I was able to start it up ok.
You need to add in the app definition block a depends on: sentence, to make docker compose to not boot the application until the database is up.
Check this documentation: Docker Compose Startup Order

Docker not catching env vars

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

Connectivity issue between containers (I suppose...)

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.

JDBC connection error only while starting docker-compose

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

Java ee api gets 404 on payara production server

I'm running a java ee application on payara server with docker-compose, the application seems to work normally locally. But when deployed it will give a 404 on all "/api" requests. The jsp files seem to work fine.
#ApplicationPath("/api")
public class SimulationApplication extends Application {
}
Is there something that could cause this behaviour.
I already tried restarting the server and docker. And the server logs don't show anything special. The only exception that it throws is that it can't backup the domain.xml to domain.xml.bak. I have tried to start it without the domain.xml mapped but this won't fix the api.
Docker-compose
version: "2"
services:
java_ee:
container_name: 'java'
image: payara/server-full
ports:
- '8080:8080'
- '4848:4848'
links:
- 'db:db'
volumes:
- './payara/autodeploy:/opt/payara41/glassfish/domains/domain1/autodeploy'
- './payara/lib:/opt/payara41/glassfish/domains/domain1/lib'
environment:
JVM_OPTS: "-Xmx12g -Xms12g -XX:MaxPermSize=1024m"
angular:
container_name: 'angular'
image: nginx
ports:
- '80:80'
volumes:
- './angular:/usr/share/nginx/html'
db:
image: mysql
container_name: 'mysql'
command: mysqld --user=root --verbose
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: "Db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "pass"
MYSQL_ROOT_PASSWORD: "pass"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
I didn't know for sure if this would be better suited for serverfault, if that fits better i'll post it there.

Categories

Resources