docker compose: spring boot connection to mysql database refused - mysql

I'm pretty new to Docker and I need to startup my Angular/SpringBoot/MySQL project with docker-compose on the docker toolbox. I copied a docker yml file into my project which used the same technologies and changed the paths inside of it to match my project. However when I try docker-compose, it throws the following error:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
[...] 84 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
the docker-compose.yml looks like:
version: '3'
services:
database:
image: mysql
container_name: database
environment:
MYSQL_ROOT_PASSWORD: ****
MYSQL_DATABASE: db_example
MYSQL_USER: springuser
MYSQL_PASSWORD: ****
ports:
- 3306:3306
volumes:
- db_exampleData:/var/lib/mysql
springapi:
image: openjdk:10-jre-slim
container_name: springapi
ports:
- 8443:8443
depends_on:
- database
volumes:
- ./backend/target/backend-0.1.0.jar:/application.jar
command: ["java", "-jar", "application.jar"]
angular:
image: nginx:alpine
container_name: angular
ports:
- 4200:80
depends_on:
- springapi
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./frontend/my-app/dist/my-app:/usr/share/nginx/html
volumes:
db_exampleData:
the application.properties:
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=****
server.port=8443
any suggestion would be helpful!

you need to change your connecion like this:
jdbc:mysql://database:3306/db_example
and add this to your docker-compose under springapi service:
links:
- database
on the other hand you may use wait-for-it.sh to check if DB is up by add a command section under springapi service:
command: ["path/to/wait-for-it.sh", "database:3306", "-t", "6000", "--", "YOUR ACTUAL COMMAND"]

Related

Can't connect to mysql docker container from springboot docker container

I want to connect from the sprinboot container to the mysql container but i get this error:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
I am using docker-compose.
This is my docker-compose file:
version: '3'
services:
trips-db:
image: "mysql"
container_name: "trips-db"
volumes:
- newDatabase:/home/sharedData
ports:
- 49152:3306
environment:
- MYSQL_USER=adham
- MYSQL_PASSWORD=123
- MYSQL_ROOT_PASSWORD=admin
trips-app:
build: ./Wasalny_BE
container_name: trips-app
ports:
- 8080:8080
links:
- trips-db
volumes:
newDatabase:
This is my spring boot docker file:
FROM openjdk:8-jdk-alpine
COPY target/wasalny-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
This is my application.properties file:
spring.datasource.url= jdbc:mysql://trips-db:49152/wslny?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username= root
spring.datasource.password= admin
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update
I run using the following command form the terminal:
docker-compose up -d
--force-recreate
Please create a docker network and attach it into the compose file.
Create a docker Network first like below.
docker network create ext-network
Add network settings into your yaml file as below and give a try
version: '3'
services:
trips-db:
image: "mysql"
container_name: "trips-db"
volumes:
- newDatabase:/home/sharedData
ports:
- 49152:3306
networks:
- ext-network
environment:
- MYSQL_USER=adham
- MYSQL_PASSWORD=123
- MYSQL_ROOT_PASSWORD=admin
trips-app:
build: ./Wasalny_BE
container_name: trips-app
ports:
- 8080:8080
networks:
- ext-network
links:
- trips-db
networks:
ext-network:
external: true
volumes:
newDatabase:

Communication link failure. Docker-compose, Spring boot, MySQL

I'm having a problem in connection between my application to DB (in-spite of similarity)
Dockerfile:
FROM openjdk:8
ADD target/DockerMyBlog.jar DockerMyBlog.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "DockerMyBlog.jar"]
docker-compose.yml
version: "3"
services:
app-db:
image: mysql:latest
container_name: "my_db"
environment:
- MYSQL_ROOT_PASSWORD=hey
- MYSQL_DATABASE=javastudy
- MYSQL_USER=roman
- MYSQL_PASSWORD=hey
ports:
- 3306:3306
app:
build: .
container_name: "my_app"
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://app-db:3306/javastudy?autoReconnect=true&useSSL=false
depends_on:
- app-db
Instead of "build: ." tried "image: dockermyblog:latest". No result.
application.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/javastudy
jdbc.username=roman
jdbc.password=hey
my_db is working. After "exec" I can create required tables in it. But can't launch my_app because of "Communication link failure".
What's wrong in my yml??
My guess is that 'localhost' is not a valid enpoint for a Docker container talking to another container running on the host in your application.properties. You can try and put it on IP address of the machine like so:
jdbc.url=jdbc:mysql://192.168.1.100:3306/javastudy
or
jdbc.url=jdbc:mysql://host.docker.internal:3306/javastudy

Run Mysql in a docker container with Spring Boot

I am trying to change my embedded in-memory H2 database to a MySQL instance using docker.
The application runs fine in-memory using docker, but I can't manage to change to MySQL.
I did some research, and managed to write this docker-compose.yml:
version: '3.3'
services:
#service 1: definition of mysql database
db:
image: mysql:latest
container_name: mysql-db2
environment:
- MYSQL_ROOT_PASSWORD=sa
- MYSQL_USER=password
ports:
- "3306:3306"
#service 2: definition of phpMyAdmin
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: my-php-myadmin
ports:
- "8082:80"
restart: always
depends_on:
- db
environment:
SPRING_DATASOURCE_USERNAME: sa
SPRING_DATASOURCE_PASSWORD: password
#service 3: definition of the spring-boot app
customerservice:
image: mysqldockerapp
container_name: mysql-docker-app
build:
context: .
dockerfile: Dockerfile
ports:
- "8084:8084"
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db2:3306/customer?createDatabaseIfNotExist=true
SPRING_DATASOURCE_USERNAME: sa
SPRING_DATASOURCE_PASSWORD: password
And these are my application.properties
server.port=8084
spring.application.name=customer-service
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://mysql-db2:3306/customer?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=never
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.SQL=DEBUG
And my dockerfile
FROM openjdk:16-alpine3.13
MAINTAINER krzysztof.com
EXPOSE 8084
COPY target/myapp.jar myapp.jar
ENTRYPOINT ["java","-jar","/myapp.jar"]
The containers build correctly, but the app exits and I get:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
But the MySQL container is running, so I would appreciate any tips where should I look for possible reasons.

Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose

Trying to connect Springboot app dicom_viewer: Imagename: sample with Mysql: Imagename: pb_mysql running in docker container. Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
Application.properties file:
#MySQL database connection strings
spring.datasource.url=jdbc:mysql://pb_mysql:3306/test?autoReconnect=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=pb
spring.datasource.password=pb#123
#JPA property settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
Docker-compose file:
version: '2'
services:
pb_mysql:
container_name: pb_mysql
restart: always
image: mysql/mysql-server:latest
environment:
MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
volumes:
- sql-datavolume:/var/lib/mysql
patient_browser:
container_name: patient_browser
restart: always
image: patient_browser
ports:
- 8080:8080
volumes:
- ./dicom_images:/usr/src/app/dicom_images
springboot-image:
container_name: dicom_viewer
restart: always
image: sample
ports:
- 8081:8080
volumes:
sql-datavolume:
networks:
f4:
driver: bridge
Dockerfile:
FROM openjdk:8-jdk-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./target/springboot-image-0.0.1-SNAPSHOT.jar /usr/src/app
COPY . /usr/src/app
EXPOSE 8080
ENTRYPOINT ["java","-jar","springboot-image-0.0.1-SNAPSHOT.jar"]
Database: test, table: pm_of_india
Docker images, docker ps -a
docker-compose up
Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
There are two things to notice,
depends_on
You kept pb_mysql ahead of others in the yaml configuration file. This does not ensure the mysql-server to be up and running ahead of others.
You want to use depends_on, so the depended pb_mysql will be initiated first.
ports
You are using bridged network, so you need to map 3306 to 3306 for mysql-server.
version: '2'
services:
patient_browser:
container_name: patient_browser
restart: always
image: patient_browser
ports:
- 8080:8080
volumes:
- ./dicom_images:/usr/src/app/dicom_images
- 8081:8080
depends_on:
- pb_mysql
springboot-image:
container_name: dicom_viewer
restart: always
image: sample
ports:
- 8081:8080
depends_on: # Let mysql-server start first
- pb_mysql
pb_mysql:
container_name: pb_mysql
ports:
- "3306:3306" # Port mapping.
restart: always
image: mysql/mysql-server:latest
environment:
MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
volumes:
- sql-datavolume:/var/lib/mysql
volumes:
sql-datavolume:
networks:
f4:
driver: bridge
I would avoid using restart: always unless I absolutely need it.
The rest looks good to me. Let me know if it is resolved.
You are not referencing your network.
Try adding it to your services i.e.:
services:
pb_mysql:
networks:
- f4
...

JDBC Communications link failure with Docker Compose

When my Spring Boot app tries to connect to the MySQL-database, it throws a CommunicationsException:
spring-service_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
spring-service_1 |
spring-service_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
As far as I can tell, my docker-compose.yml is correct, as well as my Dockerfile which simply builds and moves the jar. I've tried changing around some application properties, but it always ends up either in this Connection Exception, or the database refusing access after building my jar and using docker-composer up or docker-composer up --build.
The docker-compose.yml:
version: "3"
services:
mysql-service:
image: mysql:5.7
networks:
- spring-boot-mysql-network
volumes:
- C:/Docker/mysql-conf:/etc/mysql/conf.d
- C:/Docker/mysql-data:/var/lib/mysql
restart: always
environment:
- MYSQL_USER=springuser
- MYSQL_PASSWORD=password123
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_DATABASE=springprototype
- MYSQL_ROOT_HOST='%'
spring-service:
build:
context: ./
dockerfile: Dockerfile
ports:
- "4000:4000"
networks:
- spring-boot-mysql-network
depends_on:
- mysql-service
networks:
spring-boot-mysql-network:
driver: bridge
The Dockerfile:
FROM adoptopenjdk:11-jdk-hotspot
ADD target/*.jar app.jar
EXPOSE 4000
ENTRYPOINT ["java", "-jar", "app.jar"]
My Spring Boot app's application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://mysql-service:3306/springprototype
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
server.port=4000
What could be wrong here? Maybe I'm overlooking or forgetting something.
You can simplify the network by removing all the extra definitions and using directly the default network because you only have two services.
version: "3"
services:
mysql-service:
image: mysql:5.7
volumes:
- C:/Docker/mysql-conf:/etc/mysql/conf.d
- C:/Docker/mysql-data:/var/lib/mysql
restart: always
environment:
- MYSQL_USER=springuser
- MYSQL_PASSWORD=password123
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_DATABASE=springprototype
- MYSQL_ROOT_HOST='%'
spring-service:
build:
context: ./
dockerfile: Dockerfile
ports:
- "4000:4000"
depends_on:
- mysql-service