docker - 如何使用 docker compose 在 angular 和 spring 之间进行通信
问题描述
我正在尝试使用 docker-compose 将我的 Spring Boot、Angular 和 Mysql 放在一起(在本地它正在工作)。执行 docker-compose up 后,Spring Boot Image 和 Angular 图像正常工作。我可以在浏览器中看到我的 Angular 应用程序,并且可以成功地对我的 Spring API 进行 Rest Call。主要问题是,如果我从 Angular 向 API 发出请求,则不再有成功的 Rest Call ...问题可能出在 db 上...首先它说:
/usr/sbin/mysqld: ready for connections. Version: '8.0.21' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
在控制台中。但稍后作为 db 的最后一个控制台输出,它说:
mbind: Operation not permitted
我不知道这是否是一个问题,因为我可以像前面写的那样成功地从浏览器(不是角度)进行一些 Restcalls。
我的另一个假设是,端口必须以另一种方式配置。但我已经尝试了很多不同的组合,也包括 spring 应用程序 + 总是创建新的 spring 图像。
还有一个问题是,数据库会抛出一些 SQL 错误,例如
Error executing DDL "alter table userrole add constraint userIdReference foreign key (`user_id`) references `user` (`user_id`)" via JDBC Statement
但我仍然可以进行一些 RestCalls .. 例如在 MySql 工作台中,我可以毫无问题地导入 sql 文件并在本地启动 spring boot + angular 以成功启动项目。
springpart_1 | Hibernate: select * from product where product.current_name = ?
启动 docker compose-up 后,控制台上确实会出现上述消息,但不会将任何内容加载到 Angular 客户端中。
GET http://localhost:8077/products net::ERR_CONNECTION_REFUSED
除此之外,我不知道可能是什么问题..可能也是因为我是 docker 新手。预先感谢您的帮助。
码头工人撰写文件
services:
springpart:
image: ce153fc5b589
ports:
- '8077:8077'
environment:
- DATABASE_HOST=db
- DATABASE_PORT=3306:3306
networks:
- backend
- frontend
depends_on:
- db
restart: on-failure
db:
image: mysql:8.0
volumes:
- .src/main/resources/guitarshop/currentGuitarshopData:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=mypassword
- MYSQL_DATABASE=guitarshop
networks:
- backend
angularpart:
image: b8140c7fedec
ports:
- '4200:80'
networks:
- frontend
networks:
frontend:
backend:
角度图像创建 docker_file
FROM node:alpine As builder
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build --prod
FROM nginx:alpine
COPY --from=builder /usr/src/app/dist/guitarShopAngular/ /usr/share/nginx/html
EXPOSE 80
application.properties_file
spring.datasource.url=jdbc:mysql://db:3306/guitarshop?serverTimezone=UTC&useLegacyDatetimeCode=false?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.initialization-mode=never
spring.datasource.username = root
spring.datasource.password = mypassword
spring.datasource.platform=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql = true
server.port = 8077
spring.main.banner-mode=off
spring.jackson.serialization.fail-on-empty-beans=false
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=500KB
spring.servlet.multipart.max-request-size=500KB
spring.servlet.multipart.resolve-lazily=false
如果您需要更多信息,请告诉我..
解决方案
尝试像这样更改卷绑定挂载:“./src/main/resources/guitarshop/currentGuitarshopData:/var/lib/mysql”
就像评论中提到的那样,我在启动和运行应用程序方面没有任何问题。就数据而言,项目中包含的 db 文件似乎没有任何数据,但我能够手动添加数据,然后通过应用程序查看该数据。撰写中唯一真正的问题是绑定挂载路径,但是一旦我修复了我添加的数据在重新创建容器后仍然存在。
这里有一些建议,因为理想情况下您应该能够克隆 repo 并运行“docker-compose up -d”并让应用程序运行。现在这是不可能的,因为您首先必须在本地构建您的 spring 应用程序,然后手动构建 spring 和 angular docker 图像,然后运行 compose up。
- 创建一个新的根文件夹并将后端和前端文件夹移动到其中。
- 将撰写文件移动到根文件夹。
- 使用此处解释的多阶段构建来构建您的 Spring 应用程序:https ://spring.io/blog/2018/11/08/spring-boot-in-a-container/#multi-stage-build 。
- 将您的撰写文件修改为如下内容:
"
version: '3'
services:
springpart:
build: ./GuitarShopBackend
ports:
- '8077:8077'
environment:
- DATABASE_HOST=db
- DATABASE_PORT=3306
networks:
- backend
- frontend
depends_on:
- db
restart: on-failure
db:
image: 'mysql:8.0.17'
volumes:
# This will initialize your database if it doesn't already exist with your provided sql file
- ./GuitarShopBackend/src/main/resources/guitarshop/initialGuitarshopData:/docker-entrypoint-initdb.d
# This will persist your database across container restarts
- ./GuitarShopBackend/src/main/resources/guitarshop/currentGuitarshopData:/var/lib/mysql
ports:
- '3306:3306'
environment:
- MYSQL_ROOT_PASSWORD=mypassword
- MYSQL_DATABASE=guitarshop
networks:
- backend
angularpart:
build: ./GuitarShopAngular
ports:
- '4200:80'
networks:
- frontend
networks:
frontend: null
backend: null