I have a simple Java application using a mysql.
This is my docker-compose.yaml
:
version: '2.1'
services:
docker-mysql:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test2
- MYSQL_PASSWORD=root
ports:
- 3306:3306
my-app:
image: 0935a6b56fe5
depends_on:
- docker-mysql
ports:
- 8080:8080
environment:
- DATABASE_HOST=docker-mysql
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test2
- DATABASE_PORT=3306
Image with id 0935a6b56fe5
is a mysql
image.
And this is my application.properties
:
## Server config
server.port=8080
server.tomcat.uri-encoding=utf-8
## MYSQL JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validationQuery=SELECT 1
#Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driverClassName=com.mysql.jdbc.Driver
When i try docker-compose up
application tries to run but it gives me a Connection Refused error.
What is wrong with this?
Your spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
refers to localhost. Can you try spring.datasource.url=jdbc:mysql://docker-mysql:3306/test2?characterEncoding=utf8
Also the spring.datasource.password=
is blank. How do you provide the password?
Most likely this line
spring.datasource.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
should read
spring.datasource.url=jdbc:mysql://docker-mysql:3306/test2?characterEncoding=utf8
because your my-app
is running inside of the docker container and localhost
is not what you think it is. You can access other docker containers inside of docker-compose with the name of the service
you have chosen inside your docker-compose.yml
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.