【问题标题】:Unable to configure docker and nginx for spring boot无法为 spring boot 配置 docker 和 nginx
【发布时间】:2018-05-08 16:16:15
【问题描述】:

我正在尝试使用 nginx 反向代理在 docker 容器内运行测试 spring boot 应用程序。

Spring-boot 应用超级简单,仅供测试:

@Controller
public class IndexController {

    @GetMapping("/")
    public String index(){
       return "index.html";
    }
}

application.properties:

server.port=8088

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Index</title>
    </head>
    <body>
        <h1>Hello from Spring-boot and Docker! =)</h1>
    </body>
</html>

用于 spring-boot 的 Docker 文件:

FROM ubuntu:xenial
EXPOSE 8088

RUN apt-get update && apt-get install -y default-jre && apt-get install -y 
maven && apt-get install -y vim

COPY test.jar /usr/src/test.jar
ENTRYPOINT ["java", "-jar","/usr/src/test.jar"]

nginx 的 Docker 文件:

FROM ubuntu:xenial
RUN apt-get update && apt-get install -y nginx && apt-get install -y vim
RUN rm -f /etc/nginx/conf.d/default.conf
RUN rm -f /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx 配置:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
        upstream spring {
              server 0.0.0.0:8088;
    }
    server {
          listen 80;

          location / {
                     proxy_pass http://spring;
                     proxy_connect_timeout 500s;
          }
    }
}

那么我该怎么处理这一切:

1) 对于弹簧靴:

docker image build -t test/spring .
docker run -d --name=spring --network=bridge -p 8888:8088 test/spring

2) 对于 nginx:

docker image build -t test/nginx .
docker run -d --name=nginx --network=bridge -p 8880:80 test/nginx

docker ps 返回:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
158c70f8d3a0        test/nginx          "nginx -g 'daemon ..."   27 minutes ago      Up 27 minutes       0.0.0.0:8880->80/tcp     nginx
300ff69889e8        test/spring         "java -jar /usr/sr..."   13 hours ago        Up 13 hours         0.0.0.0:8888->8088/tcp   spring

docker network ls 返回:

NETWORK ID          NAME                DRIVER              SCOPE
186bb7934317        bridge              bridge              local
3b4750e698dc        host                host                local  
9acc0fd6bc9d        none                null                local

最后,当我转到 http://localhost:8888 时,我看到了 index.html 中的预期内容

Hello from Spring-boot and Docker! =)

但是当我去http://localhost:8880/我有502错误代码:

502 Bad Gateway
nginx/1.10.3 (Ubuntu)

我完全迷路了,尝试了很多不同的指南,但结果还是一样。所以我试图一步一步地展示我在做什么。 有什么想法有什么问题吗?

【问题讨论】:

    标签: ubuntu docker nginx spring-boot


    【解决方案1】:

    我做了 2 处更改,它开始正常工作。

    首先在 nginx.conf 文件中:

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        server {
              listen 80;
              server_name localhost;
    
        location / {
                         proxy_pass http://spring:8088;
    
              }
         }
    }
    

    我加了

    server_name localhost;
    

    以及带有spring-boot app的容器名称后的端口号

    proxy_pass http://spring:8088;
    

    第二个改动我还是不能完全理解,我补充了

    --link spring:spring 
    

    到 docker run 命令。在我尝试使用 docker 网络之前,因为不推荐使用链接。但是现在容器在运行后立即停止

    CMD ["nginx", "-g", "daemon off;"]
    

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 2016-03-03
      • 2021-04-27
      • 2017-12-29
      • 2020-07-24
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多