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

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

问题描述

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

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.

nginx docker 容器正在监听 80 端口(稍后会做证书,首先这个基础必须工作).

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

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

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

从外部我可以通过服务器 ip(或主机名)和端口 8080 访问应用程序,但不能通过 app1.example.com 访问.它确实适用于 app1.example.com:8080(我在主机服务器上打开了端口 8080).

From outside I can reach the app by the 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).

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

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.

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

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?

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

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

编辑刚刚发现,docker的这个命令可能会有所帮助:

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

sudo docker network inspect bridge

这显示了容器内使用的 IP 地址(在我的例子中是 172.17..0.2),但不确定每次 docker 重启时这个地址是否保持不变......(例如服务器重启)

This shows the Ip address used inside the containers (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 Following alkaline answer I now have (but still not working):

我的 docker-compose.yml 文件:

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

和我的 nginx(为了简单起见跳过了 conf.d 文件夹中的包含):

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

worker_processes 1;

worker_processes 1;

事件{worker_connections 1024;}

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

这可能是问题所在,名称不是主干,而是在文件夹启动服务后调用:

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

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 容器的方式造成的?

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

这是我使用的docker文件:

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

我在 9 月 1 日的最终解决方案.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

在您运行 docker-compose up -d 的项目文件夹中,我添加了一个名为 nginx-configs 的文件夹.此文件夹将覆盖"名为/etc/nginx/conf.d 的 nginx 容器中的所有文件

In the project folder, from which 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

因此,在添加此卷挂载之前,我从 nginx 容器中复制了 default.cfg.使用命令:

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

docker exec -t -i container_name /bin/bash

而不是 cat/etc/nginx/conf.d/default.conf

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

并在带有 nginx 配置的项目文件夹中添加相同的 default.conf.

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

除了默认我添加了 app1.conf 这个内容:

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.

推荐答案

这是一个最佳实践.仅在主机外部公开端口 80.nodejs 应用程序可以位于只能通过 nginx 访问的私有网络中.

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

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

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.

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

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