I cannot get my Docker container running a Java Spring Boot Api layer to communicate with my Docker container running a MySql database.
1) I created the MySql image as follows:
docker run -d
--name aname-mysql
-e MYSQL_ROOT_PASSWORD=root
-e MYSQL_DATABASE=db
-e MYSQL_USER=root
-e MYSQL_PASSWORD=root
mysql:5.7
2) I built the Docker image for the Java Spring Boot app as follows:
./gradlew build docker -x test
3) I created the Docker container for the Java Spring Boot app as follows:
docker run -d
-p 8080:8080 --name app-api
--link aname-mysql:mysql
-e DATABASE_HOST=aname-mysql
-e DATABASE_PORT:3306
-e DATABASE_NAME=db
-e DATABASE_USER=root
-e DATABASE_PASSWORD=root
com.app/my.api
4) My Dockerfile looks like this
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Dspring.profiles.active=container", "-jar","/app.jar"]
5) My application-container.yml file looks like this
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?autoReconnect=true&useSSL=false
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: validate
show_sql: true
After running these commands and then entering
http://localhost:8080/api/users
I get an error stating that the site refuses to connect and only the default browser error page is shown. Please tell me what I'm doing wrong. Thanks.
create a docker-compose.yaml and put the following
version: '2'
services:
database:
container_name:db
restart: always
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: example
java:
image: <YOUR JAVA IMAGE>
links:
- "database"
start services:
docker-compose up
try to connect to it through localhost:3306 from java or get the ip of db container:
docker inspect db
look for the public ip then connect to it
将两个应用程序都放入docker组成,并通过其docker名称相互引用。
I just noticed a typo. Instead of DATABASE_PORT=3306, I used a colon. With this correction, the Java Spring Boot container is now linked to and working properly with the MySql container.
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.