我有一个UI应用程序(Angular应用程序)和后端应用程序(Java Spring引导应用程序)。我想将它们部署在Docker容器中,并希望与后端和前端应用程序进行通信。如果没有对URL和其他内容进行硬编码,则应动态解析通信所需的所有内容。
因此,基本上我想创建的是本地开发环境,该环境与生产环境类似,而不是确切的副本,但具有类似于生产环境的功能。
因此,我选择解决此问题的方法如下所述。首先,需要了解目录结构。
E:.
│ .gitattributes
│ docker-compose.yml
│ README.md
│
├───beservice
│ Dockerfile
│
├───nginx
│ └───conf
│ ngnix.conf
│
└───ui-app
Dockerfile
后端应用程序具有其自己的docker文件,前端应用程序具有其自己的。一个重要的文件是Nginx文件nginx.conf。
让我们看一下文件中的内容。
└───beservice
Dockerfile
FROM openjdk:11.0.4-jre
LABEL APP_ID="beservice"
VOLUME /app
EXPOSE 8080
ENTRYPOINT java -Xdebug -Xrunjdwp:transport=dt_socket,address=*:8000,server=y,suspend=n -jar /app/$JAR
└───ui-app
Dockerfile
FROM nginx
LABEL APP_ID="ui-app"
RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
E:.
└───docker-compose.yml
version: "3"
services:
beservice:
build:
context: ./beservice # beservice1 -> backend Service 1
image: beservice:latest
container_name: beservice
volumes:
- [ REPLACE this with Backend Service path ]:/app # Like E:\repos\backend-service\build\libs
ports:
- 9002:8080 # App access Port, inside container it is 8080 and mappped with 9002
- 1111:8000 # to remote debug springboot application
environment:
JAR : [ jar file name - locate form build path ] # Just Jar Name like module-backend-0.0.1-SNAPSHOT.jar
uiapp:
build:
context: ./ui-app
image: ui-app:latest
container_name: ui-app
volumes:
- [ path of ui app build ]:/usr/share/nginx/html # We need to Map UI app build path here, Like my angular UI App, E:\repos\ui-app\dist\ui-app
- nginx\conf\ngnix.conf:/etc/nginx/conf.d/
depends_on:
- beservice
ports:
- 80:80
最重要的文件,ngnix.conf
├───nginx
│ └───conf
│ ngnix.conf
server {
listen 80;
server_name host.docker.internal;
# By default land on localhost:80 to root so in root we copied UI build to the ngnix html dir.
# have a look to docker-compose uiapp service.
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# after location add filter, from which every endpoint starts with or comes in endpoint
# so that ngnix can capture the URL and reroute it.
# like /backend/getUserInfo/<UserId>
# In above example /backend is that filter which will be captured by Ngnix and reroute the flow.
location /backend {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_pass http://<ContainerName>:<PortNumber>;
# In our case Container name is as we setup in docker-compose `beservice` and port 8080
proxy_pass http://beservice:8080;
}
}
此存储库包含所有必需的文件和配置,可帮助您设置容器化环境,这些环境在不同的容器中具有UI应用程序,在不同的容器中具有后端应用程序。这两个应用程序都使用Ngnix服务器进行通信。
详细描述Ngnix反向代理配置。
设置说明:
在当前Repo / localDevEnv的根目录中打开powershell并运行以下命令
docker-compose -f "docker-compose.yml" up -d --build
带有输出的命令完成:
Creating beservice ... done Creating uiapp ... done
有关更多详细信息,请访问:https://github.com/dupinder/NgnixDockerizedDevEnv