我创建了两个运行两个不同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
我做错了什么,将是解决它的最佳方法?
谢谢Farhad,我已经检查了你的答案,并且通过建议的网络来修正它。它现在工作正常 – Fab