I have the problem when running spring boot with mysql using Docker. It runs well locally without Docker. My dockerfile
FROM maven:3.3-jdk-8-alpine
ENV MAVEN_HOME /usr/lib/mvn
ENV PATH $MAVEN_HOME/bin:$PATH
ENV SPRING_DATASOURCE_URL jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
ENV SPRING_DATASOURCE_USERNAME vnphu
ENV SPRING_DATASOURCE_PASSWORD password
docker-compose.yml file
version: '3'
services:
my-db:
image: mysql/mysql-server:5.7
container_name: my-db
environment:
MYSQL_USER: vnphu
MYSQL_DATABASE: registration_springboot
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
ports:
- '3306-3306'
web:
build: .
working_dir: /app
volumes:
- .:/app
- ~/.m2:/root/.m2
ports:
- "8080:8080"
command: mvn clean spring-boot:run
depends_on:
- my-db
The error when run: docker-compose up --build
Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
web_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
web_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
web_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
web_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.19.jar:8.0.19]
web_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar:8.0.19]
web_1 | ... 115 common frames omitted
web
application is trying to connect to my-db
which is not yet available(database is still loading) you can use any of the following solutions depending on your preference.
Restart-on failure
modify docker-compose file to
web: ports: "8080:8080" restart:on-failure
sleep
modify docker-compose file under environment
web: ports: "8080:8080" environment: -SLEEP_LENGTH=5
wait-for-it.sh file
this approach needs you to modify your Dockerfile as well as docker-compose file
The problem is with my ENV in Dockerfile because I also have an environment variable with the same name in Shell which will take precedence.
ENV SPRING_DATASOURCE_URL jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
After adding the variable to environment in the docker-compose.yml, It worked
web:
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://my-db:3306/registration_springboot?createDatabaseIfNotExist=true
docker-compose.yml will look up as below order:
You can refer to this article for more details: https://vsupalov.com/override-docker-compose-dot-env/
Your service and db are not on the same network. Consider defining a network and adding to each of service and db
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.