无法为弹簧引导configurationdocker和nginx
我正尝试使用nginx反向代理在Docker容器中运行testing的spring启动应用程序。
Spring引导应用程序非常简单,只是为了testing:
@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>
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"]
Docker文件的nginx:
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;"]
nginxconfiguration:
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工人返回:
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
dockernetworkingls返回:
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)
我完全失去了,尝试了很多不同的指导,但结果仍然是一样的。 所以我试图展示我一步一步做的事情。 任何想法可能是错的?
我做了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;
和弹簧启动应用程序的容器名称后端口的数量
proxy_pass http://spring:8088;
我补充说,第二个变化我还不能完全理解
--link spring:spring
到docker运行命令。 之前,我试图使用dockernetworking,因为链接已弃用。 但是现在容器运行后,容器立即停止
CMD ["nginx", "-g", "daemon off;"]