简体   繁体   English

Docker + nginx + angular +弹簧靴

[英]Docker + nginx + angular +Spring Boot

I am trying a simple angular app with a spring boot backend using docker compose.我正在尝试使用 docker 组合的 spring 引导后端的简单 angular 应用程序。 But my front end cant seem to find the backend api when called.但是我的前端在调用时似乎找不到后端 api。 Below are the relevant files.以下是相关文件。

Docker File for Backend Docker 后端文件

#
# Build
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/demo-0.0.1-SNAPSHOT.jar /usr/local/lib/demo.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/demo.jar"]

Docker File for frontend angular Docker 前端文件 angular

FROM node:alpine AS builder

WORKDIR /app

COPY . .

RUN npm install && \
    npm run build

FROM nginx:alpine

COPY --from=builder /app/dist/* /usr/share/nginx/html/
COPY /nginx.conf /etc/nginx/conf.d/default.conf

NGINX conf file used使用 NGINX 配置文件

server {
    include /etc/nginx/extra-conf.d/*.conf;
    
    listen 80;
    server_name frontend;
    
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files $uri $uri/ /index.html =404;
    }
    
    location /api/ { 
        proxy_http_version 1.1;
        #proxy_pass http://<ContainerName>:<PortNumber>; 
        # In our case Container name is as we setup in docker-compose `beservice` and port 8080
        proxy_pass http://backend:8080/api/;   
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_cache_bypass $http_upgrade;
    } 
}

Docker Compose Yml Docker 组成 Yml

version: "3.9"

services:
    backend:
        image: demo
        container_name: demo
        build:
            context: ./demo
        ports:
            - 8080:8080
    frontend:
        image: demo-ui
        container_name: demo-ui
        build:
            context: ./my-demo-ui
        ports:
            - 80:80
        depends_on:
            - backend
        links: 
            - backend

The Front end application comes up but when I hit the backend app (through angular/nginx) it gives a 502 Bad Gateway Error.前端应用程序出现,但是当我点击后端应用程序(通过 angular/nginx)时,它给出了 502 Bad Gateway Error。

Also I see these printed in NGINX console in docker我还看到这些打印在 docker 的 NGINX 控制台中

 [error] 30#30: *1 connect() failed (110: Operation timed out) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /api/demoMethod HTTP/1.1", upstream: "http://172.17.0.2:8080/api/demoMethod", host: "localhost", referrer: "http://localhost/home"

In your docker-compose file you have在您的 docker-compose 文件中,您有

container_name: demo

but in the nginx configuration you're using the name backend .但是在 nginx 配置中,您使用的是名称backend You have to use the container name demo in the nginx configuration.您必须在 nginx 配置中使用容器名称demo The service name backend can only be used inside the docker-compose file.服务名称backend只能在 docker-compose 文件中使用。

Additionally:此外:

Warning警告

The --link flag is a legacy feature of Docker. --link 标志是 Docker 的遗留功能。 It may eventually be removed.它最终可能会被删除。 Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.除非您绝对需要继续使用它,否则我们建议您使用用户定义的网络来促进两个容器之间的通信,而不是使用 --link。

One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers.用户定义的网络不支持您可以使用 --link 执行的一项功能是在容器之间共享环境变量。 However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.但是,您可以使用其他机制(例如卷)以更可控的方式在容器之间共享环境变量。

Docker Composer doc Docker 作曲家文档

Here you're missing the network configuration in the Docker-compose file, you need to link your two container so they can see one another.在这里,您缺少 Docker-compose 文件中的网络配置,您需要链接您的两个容器,以便它们可以互相看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM