我还在docker世界中探索。我正在尝试将我的小型宠物项目docker化。前端是vue-cli应用程序,与由spring boot创建的包含restful API的后端进行通信。
因此,我的方法是为后端和前端声明两个单独的dockerfile,并通过一个docker compose文件将它们组合起来。因此,文件夹结构如下 -
backend/
- Dockerfile
frontend/
- Dockerfile
docker-compose.yml
由于前端需要能够调用后端服务,因此我创建了一个桥接网络,并在它们之间共享了这个网络。以下是我的Docker Compose文件:
version: "2"
services:
backend:
build: backend/.
networks:
- movie-network
ports:
- "8098:8098"
frontend:
build: frontend/.
ports:
- "8812:8080"
networks:
- movie-network
networks:
movie-network:
driver: bridge
我在后端有一个名为
/api/findAll
的端点。如果前端服务通过主机网络调用API,如http://localhost:8098:/api/findAll
,我可以在浏览器(http://localhost:8812)中查看它,一切正常。但是,如果前端使用桥接网络调用相同的API:
backend:8098/api/findAll
,则我的浏览器会显示错误:
![error](https://istack.dev59.com/7XFjz.webp)
docker exec
并ping后端,则ping成功。那么,为什么我的浏览器会显示错误(http://localhost:8812)?我是否在概念上犯了什么错误?请求建议。
前端Dockerfile:
FROM node:lts-alpine
# install simple http server for serving static content
RUN npm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# build app for production with minification
RUN npm run build
EXPOSE 8080
后端Dockerfile -
FROM openjdk:11-jre-slim
# Creating app directory
WORKDIR /usr/src/movies
# Copying Jar (didn't use FROM maven to save some time)
COPY target/movies*.jar movies.jar
RUN pwd
RUN ls -la
# Exposing Port
EXPOSE 8098
# Running App
ENTRYPOINT ["java","-jar","/usr/src/movies/movies.jar"]