如何从码头化的nginx到达另一个容器 [英] how to reach another container from a dockerised nginx

查看:125
本文介绍了如何从码头化的nginx到达另一个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Docker容器中有nginx,在另一个docker容器中有一个nodejs webapp。
从8080端口的主机服务器可以访问nodejs服务器。



nginx docker容器正在侦听端口80(稍后会做证书,首先是基地必须工作)。



现在我想要一个子域名转发到这个8080的nodejs应用程序。让我们说app1.example.com



从外面我可以通过te服务器IP(或主机名)和端口8080到达应用程序,但不能在app1.example.com上。它可以在app1.example.com:8080上工作(我已经在主机服务器上打开了端口8080)。



我接近一个坏的网关nginx消息app1.example.com所以我进入第一个nginx容器,但是如何回到主机服务器代理将其传递到主机服务器的端口8080(而不是nginx容器的端口8080)。寻找反向EXPOSE语法。



主要的问题是,当然如果我使用ip和port 127.0.0.1:8080,它将尝试在nginx容器上..
那么如何让nginx容器路由回到主机127.0.0.1:8080?



我已经尝试了0.0.0.0并定义了一个上游,实际上是谷歌搜索很多,并尝试了很多配置...但还没有找到一个工作....



编辑
刚刚发现,这个停靠码头的命令可能有帮助:

  sudo docker network inspect bridge 

这显示了机器内部使用的Ip地址(在我的情况下是172.17..0.2),但不知道这个地址是否保留每次停靠码头将重新启动...(例如服务器重新启动)



修改
遵循碱性答案我现在有仍然不工作):



我的docker-compose.yml文件:

 版本:2
服务:
nginx:
container_name:nginx
image:nginx_img
build:../docker-nginx-1/
ports:
- 80:80
networks:
- 骨干
nodejs:
container_name:nodejs
image:merites / docker-simple-node-server
build:../ docker-simple-node-server /
网络:
- 骨干
曝光:
- 8080
网络:
骨干:
驱动程序:桥梁

和我的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;

位置/ {
proxy_pass http:// upsrv;
}

}
}

编辑31-08-2016



这个问题就是这个问题,名字不是骨干的,而是在文件夹启动之后调用srevice: p>

  sudo docker network ls 

out puts:

  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文件:

  ######################################### ################### 
#构建Nginx的Dockerfile安装的容器
#基于Ubuntu
######## ################################################## ##

#将基本图像设置为Ubuntu
从ubuntu

#文件作者/维护者
维护者维护人员名称

#安装Nginx

#将应用程序存储库URL添加到默认源
#RUN echodeb http://archive.ubuntu.com/ubuntu/ raring main universe>> /etc/apt/sources.list

#更新存储库
运行apt-get更新

#安装必需的工具
运行apt-get install -y nano wget对话框net-tools

#下载并安装Nginx
运行apt-get install -y nginx

#删除默认的Nginx配置文件
RUN rm -v /etc/nginx/nginx.conf

#从当前目录复制一个配置文件
ADD nginx.conf / etc / nginx /

#附加守护程序;到配置开始
RUN echodaemon off; >> /etc/nginx/nginx.conf

#露出端口
EXPOSE 80

#设置默认命令在创建新容器时执行

CMD服务nginx开始

我的最终解决方案2016



我现在使用这个撰写文件:

 版本:2
服务:
nginx:
图像:nginx
容器名称:nginx
卷:
- ./nginx-configs:/ etc / nginx / conf.d
ports:
- 80:80
networks:
- 骨干
nodejs:
container_name:nodejs
image:merites / docker-simple-node-server
build:../docker-simple-node-server/
networks:
- 骨干
曝光:
- 8080
网络:
骨干:
驱动程序:桥

在项目文件夹中,从运行docker-compose up -d开始,我添加了一个名为nginx-configs的文件夹。该文件夹将'覆盖'名为/etc/nginx/conf.d的$ n $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $添加了此卷安装。使用以下命令:



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;


位置/ {
proxy_pass http:// upsrv1;
}

}

这样,我可以轻松添加第二个应用程序...第三个等等。



所以基础现在正在工作...

解决方案

这是一个最佳实践。只能将端口80暴露在主机外部。 Nodejs应用程序可以在私人网络中,只能通过nginx访问。

 版本:2
服务:
nginx:
...
ports:
- 80:80
networks:
- 骨干
nodejs:
...
网络:
- 骨干
曝光:
- 8080

网络:
骨干:
驱动程序:桥

在您的nginx.conf文件中,上游服务器可以列为 nodejs :8080 。 docker守护进程会将其解析为正确的内部ip。


I have nginx in a docker container, and a nodejs webapp in another docker container. The nodejs server is reachable from the host server on port 8080.

The nginx docker container is listening to port 80 (will do the certificate later, first this base must be working).

And now I want a subdomain to be forwarded to this 8080 nodejs app. lets say app1.example.com

From outside I can reach the app by te server ip (or hostname) and port 8080 but not on app1.example.com. And it does work on app1.example.com:8080 (I have opened up port 8080 on the host server).

I get a bad gateway nginx message when approaching the app1.example.com So I get in the first nginx container, but how do i get back to the host server to proxy pass it to the port 8080 of the host server (and not port 8080 of the nginx container). looking for the reverse EXPOSE syntax.

the main problem is, of course if I use the ip and port 127.0.0.1:8080 it will try on the nginx container.... So how do I let the nginx container route back to the host 127.0.0.1:8080?

I have tried 0.0.0.0 and defining an upstream, actually been googling a lot, and have tried a lot of configurations... but not yet found a working one....

Edit Just found out, this command of docker might help:

sudo docker network inspect bridge

This shows the Ip address used inside the cotainers (in my case 172.17..0.2), but not sure this address stays the same every time the docker will restart... (e.g. server reboot)

Edit Followning alkaline answer I now have (but still not working):

my docker-compose.yml file:

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

and my nginx (skipped the incluse in the conf.d folder for simplicity):

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;
    }

  }
}

edit 31-08-2016

this migth be the problem, the name is not backbone, but called after the folder started the srevice from:

sudo docker network ls

out puts:

NETWORK ID          NAME                       DRIVER              SCOPE
1167c2b0ec31        bridge                     bridge              local               
d06ffaf26fe2        dockerservices1_backbone   bridge              local               
5e4ec13d790a        host                       host                local               
7d1f8c32f259        none                       null                local 

edit 01-09-2016

It might be caused by the way I have my nginx docker container setup?

this is the docker file I used:

############################################################
# 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

My final solution 1th sept. 2016

I used this compose file now:

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

In the project folder, from wich you run docker-compose up -d, I added a folder named nginx-configs. This folder will 'override' all the files in the nginx container named /etc/nginx/conf.d

Therefor I copied the default.cfg from the nginx container before I added this volume mount. using the command:

docker exec -t -i container_name /bin/bash

and than cat /etc/nginx/conf.d/default.conf

and added the same default.conf in the project folder with nginx configs.

Besides the default I added app1.conf with this content:

upstream upsrv1 {
    server nodejs:8080;
}

server {

    listen 80;
    server_name  app1.example.com;


    location / {
        proxy_pass http://upsrv1;
    }

}

This way, I can easily add a second app... third and so on.

So the basics is working now...

解决方案

Here's a best practice. Only expose port 80 outside of the host. The nodejs app can be in a private network only accessible through nginx.

version: "2"
services:
    nginx:
      ...
      ports:
        - "80:80"
      networks:
        - backbone
    nodejs:
      ...
      networks:
        - backbone
      expose: 
        - 8080

networks:
  backbone:
    driver: bridge

In your nginx.conf file, the upstream servers can be listed as nodejs:8080. The docker daemon will resolve it to the correct internal ip.

这篇关于如何从码头化的nginx到达另一个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆