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
Related
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!!
I am trying to connect spring boot app to the postgres on docker but I got org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user"
even if I use correct password(I belive so)
docker-compose.yml in main project dir:
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
application.yml in springboot app dir:
server:
port: 8080
spring:
application:
name: customer
datasource:
password: password
url: jdbc:postgresql://localhost:5432/customer
username: user
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
pg_hba.conf in docker:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
Steps I've taken to create database:
Docker compose up -d
starting containers
go to http://localhost:5050/browser/# and enter master password
creating server
creating database
and when I try to run spring boot app I got
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user"
I tried to restart images and delete them. Please help to find the bug in my configuration.
If you add the following to you docker-compose.yml, it will probably work.
POSTGRES_DB: customer
You did no create the "schema", and that's why you are getting the error.
The postgres image configuration with the fix:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
POSTGRES_DB: customer
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
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 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