如何从dockerised nginx到达另一个容器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何从dockerised nginx到达另一个容器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4511字,纯文字阅读大概需要7分钟。
内容图文
我在docker容器中有nginx,在另一个docker容器中有一个nodejs webapp.
可以从端口8080上的主机服务器访问nodejs服务器.
nginx docker容器正在侦听端口80(稍后将执行证书,首先此基础必须正常工作).
现在我希望将子域转发到此8080 nodejs应用程序.让我们说app1.example.com
从外面我可以通过服务器ip(或主机名)和端口8080到达app,但不能在app1.example.com上.它确实适用于app1.example.com:8080(我在主机服务器上打开了端口8080).
当我接近app1.example.com时,我得到一个糟糕的网关nginx消息所以我进入第一个nginx容器,但是如何回到主机服务器代理将其传递到主机服务器的端口8080(而不是端口) 8080的nginx容器).寻找反向EXPOSE语法.
主要的问题是,当然如果我使用ip和端口127.0.0.1:8080它将尝试nginx容器….
那么如何让nginx容器路由回到主机127.0.0.1:8080?
我已经尝试了0.0.0.0并定义了一个上游,实际上一直在谷歌搜索,并尝试了很多配置……但还没有找到一个工作….
编辑
刚刚发现,这个docker命令可能会有所帮助:
sudo docker network inspect bridge
这显示了cotainers中使用的IP地址(在我的情况下为172.17..0.2),但不确定每次docker重启时该地址都保持不变…(例如服务器重启)
编辑
我现在跟着碱性答案(但仍然没有工作):
我的docker-compose.yml文件:
version: "2"
services:
nginx:
container_name: nginx
image: nginx_img
build: ../docker-nginx-1/
ports:
- "80:80"
networks:
- backbone
nodejs:
container_name: nodejs
image: merites/docker-simple-node-server
build: ../docker-simple-node-server/
networks:
- backbone
expose:
- 8080
networks:
backbone:
driver: bridge
和我的nginx(为简单起见,跳过conf.d文件夹中的包含):
worker_processes 1;
events {worker_connections 1024; }
http {
sendfile on;
upstream upsrv {
server nodejs:8080;
}
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://upsrv;
}
}
}
编辑31-08-2016
这个migth是问题,这个名字不是主干,而是在文件夹启动之后调用srevice:
sudo docker network ls
输出:
NETWORK ID NAME DRIVER SCOPE
1167c2b0ec31 bridge bridge local
d06ffaf26fe2 dockerservices1_backbone bridge local
5e4ec13d790a host host local
7d1f8c32f259 none null local
编辑01-09-2016
这可能是由于我的nginx docker容器设置方式造成的?
这是我使用的docker文件:
############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name
# Install Nginx
# Add application repository URL to the default sources
# RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start
我的最终解决方案1月1日. 2016
我现在使用这个撰写文件:
version: "2"
services:
nginx:
image: nginx
container_name: nginx
volumes:
- ./nginx-configs:/etc/nginx/conf.d
ports:
- "80:80"
networks:
- backbone
nodejs:
container_name: nodejs
image: merites/docker-simple-node-server
build: ../docker-simple-node-server/
networks:
- backbone
expose:
- 8080
networks:
backbone:
driver: bridge
在项目文件夹中,从你运行docker-compose up -d,我添加了一个名为nginx-configs的文件夹.该文件夹将“覆盖”名为/etc/nginx/conf.d的nginx容器中的所有文件
因此,在添加此卷装入之前,我从nginx容器中复制了default.cfg.使用命令:
docker exec -t -i container_name / bin / bash
而不是cat /etc/nginx/conf.d/default.conf
并使用nginx配置在项目文件夹中添加了相同的default.conf.
除了默认我添加了这个内容的app1.conf:
upstream upsrv1 {
server nodejs:8080;
}
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://upsrv1;
}
}
这样,我可以轻松添加第二个应用程序……第三个等等.
所以基础工作现在正在……
最佳答案:
这是一个最佳实践.仅将端口80暴露在主机外部. nodejs app可以位于只能通过nginx访问的专用网络中.
version: "2"
services:
nginx:
...
ports:
- "80:80"
networks:
- backbone
nodejs:
...
networks:
- backbone
expose:
- 8080
networks:
backbone:
driver: bridge
在nginx.conf文件中,上游服务器可以列为nodejs:8080. docker守护程序会将其解析为正确的内部ip.
内容总结
以上是互联网集市为您收集整理的如何从dockerised nginx到达另一个容器全部内容,希望文章能够帮你解决如何从dockerised nginx到达另一个容器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。