In my production environment with this command I run my docker-compose:
docker-compose -f docker-compose.yml up -d
DOCKER-COMPOSE
version: "3.3"
services:
db:
image: mysql
platform: linux/amd64
restart: always
container_name: mysql-db-users
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: 'userDb'
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin
restart: always
container_name: php-my-admin-users
ports:
- "8081:80"
Now by connecting to 172.22.14.43:8081 I can login to phpmyadmin when I can find that userDb has been created.
The next step is to connect a dockerized Springboot app to this userDb, but I continue to obtain:
com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
This is the dockerfile of my SprigBoot App:
DOCKERFILE
FROM openjdk:17-jdk
ARG JAR_FILE=target/*.jar
COPY target/ldap-conn-*.jar /ldap-conn.jar
ENTRYPOINT ["java", "-jar", "/ldap-conn.jar" ]
EXPOSE 8090
APPLICATION.YML
server:
port: '8090'
spring:
application:
name: ldap-connection
datasource:
username: 'root'
password: 'password'
platform: 'mysql'
url: jdbc:mysql://mysql-db-users:3006/userDb?useSSL=false&allowPublicKeyRetrieval=true
sql:
init:
mode: always
jpa:
defer-datasource-initialization: 'true'
hibernate:
ddl-auto: update
properties:
hibernate:
data: classpath:Data.sql
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
eureka:
client:
service-url:
defaultZone: http://172.17.0.2:8761/eureka
fetch-registry: true
register-with-eureka: true
And in my POM.XML I have both these dependencies:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
I run my image in this way:
docker run -d -p 8090:8090 --name ldap myRepo/ldap1.0:ldap
I think the main issue is this line:
url: jdbc:mysql://mysql-db-users:3006/userDb?useSSL=false&allowPublicKeyRetrieval=true
As far as I understand using the container name is the way to go, but it doesn't seem to work.
I've tried by using the ip address of the docker container (with Eureka server is working)
jdbc:mysql://172.17.0.2:3006/userDb
the server ip
jdbc:mysql://serverIp:3006/userDb
but this is not working.
Related
I have created a simple spring boot app to communicate with MYSQL via Docker using the same network. Once I run docker-compose up command then following errors have occurred.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up
(https://i.stack.imgur.com/gwUuG.png)
version: "3.8"
services:
project:
build: .
ports:
- "8080:8080"
environment:
-SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/spring?autoReconnect=true&useSSL=false
depends_on:
- mysqldb
mysqldb:
image: mysql:8
ports:
- 33066:3306
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_DATABASE=spring
volumes:
- spring:/data/db
volumes:
spring:
here is my yaml file
spring.datasource.url=jdbc:mysql://localhost:3306/spring?
serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.database=mysql
server.error.include-message=always
server.error.include-binding-errors=always
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = create
here is my app properties file
I tried recreating over and over again, prune all the files(network image container) in Docker. Tried creating network via Terminal.
it would be more helpful if you add your pom.xml or gradle configs and your application.yml. However, make sure to have the following:
The correct dependency with a compatible version:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
In your application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://<host>:3306/<dbname>
spring.datasource.username=<username>
spring.datasource.password=<password>
host will be localhost if you are running your application locally, if your spring boot is in a container in your case it should be as you mentioned mysqldb. You also need to make sure that your database schema is already created
Your docker-compose.yml
version: '3' # specify a version
services:
mysqldb: # container-name
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=<password>
- MYSQL_DATABASE=<dbname>
- MYSQL_USER=<user-name>
- MYSQL_PASSWORD=<password>
volumes:
- app-data:/var/lib/mysql
spring-boot:
ports:
[8080:8080]
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysqldb
volumes:
app-data:
I have dockerized MySQL which runs on port 3307, there is all configuration on docker-compose file:
version: '3.9'
services:
db:
container_name: mysql
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=my_user
- MYSQL_PASSWORD=user_pass
- MYSQL_DATABASE=my_db
ports:
- "127.0.0.1:3307:3306"
volumes:
- mysql:/var/lib/mysql
But problem is that the Spring app is running outside of the Docker and when I run my application I've got error:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
And there is also configuration of connection with db on application.yml file:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3307/my_db?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: none
database-platform: org.hibernate.dialect.MySQL5Dialect
I also tried expose ports without 127.0.0.1 and connect by localhost but this doesn't work.
I'm having trouble connecting my spring application with the MySQL database.
In particular, I report the code of the docker compose, the docker file, application.yml and the error log.
docker-compose:
version: '3.3'
services:
my-sql-db:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=user
- MYSQL_USER=user
- MYSQL_DATABASE=spring-app
ports:
- "3306:3306"
customerservice:
image: customer-service
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
- my-sql-db
environment:
SPRING_PROFILES_ACTIVE: MySQLdocker
Dockerfile:
FROM maven:3.6.3-jdk-8
COPY ./ ./
RUN mvn clean package
EXPOSE 8080
CMD ["java", "-jar", "target/my-spring-project-0.0.1-SNAPSHOT.jar"]
Application.yml:
spring:
jpa:
open-in-view: false #per levare warnig OpenSessionInView
properties:
hibernate:
jdbc:
time_zone: CET
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://my-sql-db:3306/spring-app?createDatabaseIfNotExist=true
username: user
password: user
config:
activate:
on-profile: MySQLdocker
Executing the command "docker-compose up -d" the output error is:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
I'm not seeing MYSQL_PASSWORD defined in your docker-compose file.
Generally, the error suggests the app cannot connect to MySQL at all. When you have such a problem that could be caused by many things, you should try and narrow it down. Check if the DB is up and running. If yes, then try to connect to it from some MySQL client. And so on, until you figure out what exactly went wrong or at least come up with a more specific question.
I am about to finish my spring boot project.
However, the deployment with docker-compose is just painfully not working
Below is my codes
docker-compose.yml
version: '3'
services:
ibin-db:
container_name: ibin-db
image: mysql:5.7
environment:
MYSQL_DATABASE: iBinApiDB
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: '%'
ports:
- 3306:3306
ibin-inplay:
image: openjdk:14
volumes:
- ./run.sh:/home/ibin/run.sh
- ./ibin-inplay/build/libs:/home/ibin/libs
ports:
- 8001:8080
- 16100:16100
networks:
vpcbr:
ipv4_address: 192.168.219.69
depends_on:
- ibin-db
command: bash -c "cd /home/ibin && sh run.sh"
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 192.168.219.0/24
application.yml
spring:
datasource:
url: jdbc:mysql://ibin-db:3306/iBinApiDB?useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL5Dialect
generate-ddl: true
didn't miss to run ./gradlew bootJar
However, error pops up no matter what.
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
please help me figure this out.
The two containers aren't on the same Docker-internal network, so they can't see each other. The ibin-inplay container is explicitly assigned to the vpcbr network; the ibin-db container doesn't have any manual networks: configuration, so Compose assigns it to a default network. Networking in Compose describes what Compose will automatically create for you.
You shouldn't need to manually assign IP address to containers or otherwise do manual network configuration in Docker. I'd recommend deleting all of the networks: blocks in the whole file (both the top-level block and the one for the ibin-inplay container) and using the default network everywhere.
Any help will be appreciated.
I am trying to run mysql and spring-boot app in two container but, in same netowrk.
Using following command two container communicate each other.
mysql command
docker run --name mysql-local --network=spring-boot-network -p 4000:3306 -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=root-user -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=testdb mysql
application command
docker run --network=spring-boot-network -p 8080:8080 --name test-app -e RDH_HOSTNAME:mysql-local -e RDS_PORT:4000 -e RDS_USER:root-user -e -e RDS_PASSWORD:password 171ce195f461
using above tow docker command two containers are created in spring-boot-network and they can communicate each other.
problem arise when I tried to use docker-compose.
I passed same environments as command line but, my app refuse to connect with mysql.
I am using spotify plugin to create an image.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.3</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>rajesh132/test-docker</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
when I startup container mysql container is created and started. I can connect to it using mysqlsh client and mysql workbench as well. However, I cannot connect to my application. Following are properties and docker files that I used in my project.
Docker file
ADD target/docker-test.jar docker-test.jar
ENTRYPOINT ["java", "-jar", "docker-test.jar"]
Docker Compose file
version: '3.7'
services:
spirng-boot-test-application:
image: rajesh132/test-docker:0.0.1-SNAPSHOT
#build:
#context: .
#dockerfile: Dockerfile
ports:
- "8080:8080"
restart: always
depends_on: # Start the depends_on first
- mysql-app
environment:
RDS_HOSTNAME: mysql-app
RDS_PORT: 4000
RDS_DB_NAME: testdb
RDS_USERNAME: test-user
RDS_PASSWORD: password
networks:
- web-application-network
mysql-app:
image: mysql
ports:
- "4000:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: test-user
MYSQL_PASSWORD: password
MYSQL_DATABASE: testdb
volumes:
- mysql-database-data-volume:/var/lib/mysql
networks:
- web-application-network
# Volumes
volumes:
mysql-database-data-volume:
networks:
web-application-network:
application.yml
datasource:
# use this url for localhost
#use docker container name instead 'localhost'
# url: jdbc:mysql://localhost:3306/authorizationDb?createDatabaseIfNotExist=true
url: jdbc:mysql://${RDS_HOSTNAME:localhost}:${RDS_PORT:3306}/${RDS_DB_NAME:test}?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: ${RDS_USER:root}
password: ${RDS_PASSWORD:password}
driver-class-name: com.mysql.cj.jdbc.Driver
initialization-mode: always
tomcat:
test-on-borrow: true
validation-query: SELECT 1
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: update
properties:
hibernate:
show_sql: true
format_sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
logging:
level:
org:
hibernate:
type: trace
I followed so many blogs, documentation, however coundnt succeed to build. Any help much appreciate.
Your RDS_PORT: 4000 must be RDS_PORT: 3306 instead.
When you expose a container port as "4000:3306", the port 4000 can only be used from the host machine. Within the container network, you need to use 3306.