【发布时间】: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