JDBC connection error only while starting docker-compose - java

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

Related

Spring Boot container cannot connect to My SQL container

I have read all questions related the issue and controlled all points. It seems everything is ok with my codes but it doesn't connect anyway.
I got CONNECTION REFUSED error when I try to connect from container. (BTW. Everything is fine when I change URL and try to connect from localhost)
My java project
spring:
datasource:
url: jdbc:mysql://mysqldb:3306/bootdb
username: root
password: root
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL5Dialect
generate-ddl: true
My docker-compose file
version: "3"
services:
mysqldb:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: bootdb
networks:
- testnetwork
employee-jdbc:
image: bago1/student:latest
restart: always
build: .
ports:
- 8080:8080
networks:
- testnetwork
depends_on:
- mysqldb
links:
- mysqldb
networks:
testnetwork:
It successfully connects from my local host machine when I edit URL as
url: jdbc:mysql://mysqldb:3306/bootdb
DB works fine
They are on the same network
syntax is fine
Add the hostname field in your docker compose like this:
version: "3"
services:
mysqldb:
image: mysql
hostname: mysqldb
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: bootdb
employee-jdbc:
image: bago1/student:latest
restart: always
build: .
ports:
- 8080:8080
depends_on:
- mysqldb
links:
- mysqldb
(Ps: don't need the add the networks field, the use of docker-compose creates automatically a network for your services)
(PS2: it's a good practice to use tag when using images (instead of using mysql:latest) )
Remove ports configuration of MySQL container and insert:
expose:
- 3306

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!!

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.

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.

grails/mysql with docker compose

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

Categories

Resources