【发布时间】:2015-08-03 23:20:34
【问题描述】:
我有两个 Docker 容器(由自定义配置的 nginx 和 Ruby 映像构建),当我运行容器并发出请求时,它们似乎将请求代理到正确的位置(但其中一项被代理的服务没有' t 非常正确地处理请求)。
即当我尝试代理到我的 Ruby 容器时,我要么收到“Sinatra 无法识别此小曲”错误,要么收到“301 重定向”?
注意:代码也可以在这里找到https://github.com/Integralist/Docker-Examples/tree/master/Nginx
下面是 nginx 的 Dockerfile:
FROM ubuntu
# install nginx
RUN apt-get update && apt-get install -y nginx
RUN rm -rf /etc/nginx/sites-enabled/default
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
以下是 Ruby 应用程序的 Dockerfile:
FROM ruby:2.1-onbuild
CMD ["ruby", "app.rb"]
注意:
有人问我的app.rb和其他依赖项是如何加载的 (因为我的docker run没有安装它们,Dockerfile 也没有 出现以添加它们)。如果您查看带有 ruby “onbuild” 标记的图像版本,您会看到COPY为我们提供的所有这些文件 https://github.com/docker-library/ruby/2.0/onbuild/Dockerfile
Ruby 应用程序如下所示:
require "sinatra"
set :bind, "0.0.0.0"
get "/" do
"Hello World"
end
nginx.conf 文件看起来像:
user nobody nogroup;
worker_processes auto; # auto-detect number of logical CPU cores
events {
worker_connections 512; # set the max number of simultaneous connections (per worker process)
}
http {
upstream app {
server app:4567; # app is automatically defined inside /etc/hosts by Docker
}
server {
listen *:80; # Listen for incoming connections from any interface on port 80
server_name ""; # Don't worry if "Host" HTTP Header is empty or not set
root /usr/share/nginx/html; # serve static files from here
location /app/ { # catch any requests that start with /app/
proxy_pass http://app; # proxy requests onto our app server (i.e. a different container)
}
}
}
我像这样运行 Ruby 容器:
docker run --name ruby-app -p 4567:4567 -d my-ruby-app
我像这样运行 nginx 容器:
docker run --name nginx-container \
-v $(pwd)/html:/usr/share/nginx/html:ro \
-v $(pwd)/docker-nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
--link ruby-app:app \
-P -d my-nginx
如果我运行curl http://$(boot2docker ip):32785/app/,我会返回“Sinatra 不知道这个小曲”错误;如果我运行curl http://$(boot2docker ip):32785/app 我会返回301 Moved Permanently 消息?
我确定我遗漏了一些非常明显的东西(也许 Sinatra 是如何配置的?比如我需要设置一个/app 路由吗?或者我应该在nginx.conf 中使用alias 指令吗? )
任何帮助表示赞赏。
【问题讨论】:
-
您没有注意到您是如何在 ruby 容器中获取代码的?如果你访问 curl http://$(boot2docker ip):4567/ 你会得到什么?
-
@Dirk 有趣的是,我回来了
Hello World%!?虽然我实际上不知道这是怎么可能的,因为当我调用docker run时我没有将app.rb文件安装到容器中,我也没有在构建时将它烘焙到图像中(忘记我的愚蠢错误知道但是,容器如何运行该脚本并发送回“Hello World”?) -
@Dirk 好的,所以这个工作的原因是因为我是从
ruby:2.1-onbuild构建的,它专门将那些文件(Gemfile、Gemfile.lock 和 app.rb)复制到构建的图像中
标签: ruby nginx docker reverse-proxy