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

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:

Related

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

How to execute sql file in docker?

I have lamp project and try to run it in the Docker compose. The app is working but doesn't have connection with mysql. The reason: sql file does not execute and the database is not created. When I go into the container and run the sql file manualy, then everything works good.
version: '3.7'
services:
db:
build:
context: ./database
dockerfile: Dockerfile
container_name: data36
environment:
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_DATABASE=php_mysql_crud
volumes:
- ./database/script.sql:/docker-entrypoint-initdb.d/script.sql
- mysqlvol:/var/lib/mysql
restart: always
ports:
- 3306:3306
networks:
- app
php:
container_name: php8
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html/
ports:
- 8000:80
networks:
- app
depends_on:
- db
volumes:
database:
mysqlvol:
networks:
app:
driver: bridge
Dockerfile:
FROM mysql:5.7.17
ENV MYSQL_ROOT_PASSWORD password123
COPY script.sql /docker-entrypoint-initdb.d/
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/
EXPOSE 3306
CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
How can I execute script.sql in container ???

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

Spring Boot MySQL Docker Caused by: java.net.ConnectException: Connection refused (Connection refused)

As in the title:
Caused by: java.net.ConnectException: Connection refused (Connection refused)
This worked for me sometime ago but now unfortunately not.
Script that I execute contains:
mvn clean install -> docker-compose build -> docker-compose up
Dockerfile:
FROM openjdk:8
ADD target/grades.jar grades.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "grades.jar"]
docker-compose.yaml
version: '3'
services:
mysql-standalone:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=grades
- MYSQL_USER=root
- MYSQL_PASSWORD=password
ports:
- "33061:3306"
volumes:
- /data/mysql
grades:
image: grades
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-standalone
ports:
- 8080:8080
volumes:
- /data/grades
And application.properties:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB:aws_eb_db}
spring.datasource.username=${MYSQL_USERNAME:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
localhost for you Docker container is not the localhost of the host machine (the one where your Docker containers live). Basically it points to your Docker container itself, where MySQL doesn't live. So you have to point to your MySQL instance, or the host for your containers, as you are mapping the 3306 port of your MySQL to your host's 3306 port.
I would definitely point to the MySQL itself as #LinPy suggested:
spring.datasource.url=jdbc:mysql://mysql-standalone:3306/grades
This should help somebody.
Make sure to run the command below to invalidate the current image:
docker rmi -f <your image id>
version: '3.1'
services:
db:
image: mariadb:10.5.5
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=softeasydb
- MYSQL_USER=root
- MYSQL_PASSWORD=root
volumes:
- ./db:/var/lib/mysql
ports:
- 3306:3306
networks:
- common-network
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
- common-network
api-users:
build: .
depends_on:
- db
ports:
- 9090:9090
environment:
- SPRING_PROFILES_ACTIVE=docker
- DATABASE_HOST=db
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=softeasydb
- DATABASE_PORT=3306
- SERVER_PORT=9090
restart: always
networks:
- common-network
networks:
common-network:
driver: bridge
In your docker-compose.yaml you have a typo in the port mappings for the mysql-standalone container, you need to change the following:
ports:
- "33061:3306"
to
ports:
- "3306:3306"

Docker compose. Networks. Spring Boot and MySQL connection

I try to connect spring boot web app to database container.
And I can ping db container from web. But web can't connect to db by exposed 3307 port. But I can connect to db by internal container port 3306. Project very simple. What is may wrong?
This is my docker-compose.yml:
version: '3'
services:
db:
build:
context: ./db
dockerfile: Dockerfile
image: db
ports:
- "3307:3306"
volumes:
- demo_volume:/var/lib/mysql
networks:
- my-backend
web:
build:
context: ./web
dockerfile: Dockerfile
image: web
depends_on:
- db
ports:
- "18080:8080"
networks:
- my-backend
environment:
- DATABASE_HOST=db
- DATABASE_USER=user
- DATABASE_PASSWORD=password
- DATABASE_NAME=demo
- DATABASE_PORT=3307
- SPRING_PROFILES_ACTIVE=container
- DEBUG=true
volumes:
demo_volume:
driver: local
networks:
my-backend:
driver: bridge
It's simple enough.
My application.yml for active profile - container
spring:
profiles:
container
datasource:
url: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?characterEncoding=UTF-8
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
driver-class-name: com.mysql.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
Also very simple
db Dockerfile:
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=admin
ENV MYSQL_DATABASE=demo
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=password
ADD dump.sql /docker-entrypoint-initdb.d/
And web Dockerfile:
FROM java:8-jre
COPY ./web.jar /app/web.jar
CMD ["java", "-jar", "/app/web.jar"]
CMD ["java", "-Xmx200m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/web.jar"]
Regarding Docker documentation all containers in the same bridge network (user defined) communicates via their internal ports. It's explained with image isolated_bridge_network
If you desire your containers should be available from external network you should publish ports for this one. Publish - means you should map internal ports to external environment. See on image
published_ports_from_isolated_bridge_network
You should link the Container to make it work.
web:
build:
context: ./web
dockerfile: Dockerfile
image: web
depends_on:
- db
ports:
- "18080:8080"
networks:
- my-backend
environment:
- DATABASE_HOST=db
- DATABASE_USER=user
- DATABASE_PASSWORD=password
- DATABASE_NAME=demo
- DATABASE_PORT=3307
- SPRING_PROFILES_ACTIVE=container
- DEBUG=true
links:
- db
Now inside your web Container /etc/hosts should have an entry for db Container