2016-12-29 64 views
0

我创建了两个运行两个不同Web应用程序的容器。我试图创建第三个容器,让nginx代理根据主机名将请求重定向到正确的容器。当我运行nginx官方镜像时,我的Nginx配置看起来是正确的,并且在容器内手动修改默认配置文件和代理设置。Nginx代理,链接到其他码头集装箱

我的两个Web应用程序容器已经在运行了,我开始代理容器如下:

docker run -i -t --link webapp1 --link webapp2 -p 80:80 nginx /bin/bash 

要做到最干净的方式,我认为,我想创建一个Dockerfile创建容器,和在构建它时将本地default.conf文件传递给容器。

这是我的nginx代理配置文件:

server { 
    listen  80; 
    server_name www.webapp1.ch; 

    location/{ 
     proxy_pass http://webapp1/; 
    } 
} 

server { 
    listen  80; 
    server_name www.webapp2.ch; 

    location/{ 
     proxy_pass http://webapp2/; 
    } 
} 

和代理多克尔文件:

# Set the base image to use to Ubuntu 
FROM nginx:latest 

# Set the file maintainer (your name - the file's author) 
MAINTAINER Me 


# Update the default application repository sources list 
RUN apt-get update && apt-get install -y \ 
     wget \ 
     vim 

RUN rm /etc/nginx/conf.d/default.conf 
COPY default /etc/nginx/conf.d/default.conf 
CMD /etc/init.d/nginx restart 

但不幸的是,当我试图建立它,容器不知道但webapp1和webapp2容器地址/ IP,因为它们尚未链接。我收到此错误:

Step 7 : RUN /etc/init.d/nginx restart 
---> Running in aef974e80e74 
Restarting nginx: nginx2016/12/29 17:07:17 [emerg] 11#11: host not found in upstream "webapp1" in /etc/nginx/conf.d/default.conf:6 
nginx: [emerg] host not found in upstream "webapp1" in /etc/nginx/conf.d/default.conf:6 
nginx: configuration file /etc/nginx/nginx.conf test failed 

我做错了什么,将是解决它的最佳方法?

回答

0

使用链接是legacy功能,您应该使用User defined networks

sudo docker network create mynetwork 
docker run -d --network mynetwork -p 80:80 -v /path/to/nginx/config/default.conf:/etc/nginx/conf.d/default.conf nginx 

然后我用我最近在docker run命令创建user defined network

我在那里做的另一件事,我mounted我的nginx config,而不是为它建立一个新的形象。

根据容器名称在同一个用户定义的网络中,其中一个优点是自动dns

现在回到你nginx问题:

proxy_pass http://webapp2:port/; 

我猜你正在运行在同一泊坞窗主机上的所有3个容器,它们都不能上port 80运行,所以你应该提供端口您nginx reverse proxy配置。

+0

谢谢Farhad,我已经检查了你的答案,并且通过建议的网络来修正它。它现在工作正常 – Fab