【问题标题】:Docker Compose Containers with specific hostpath on nginxDocker Compose 容器在 nginx 上具有特定的主机路径
【发布时间】:2017-01-23 21:34:27
【问题描述】:

我有两个使用 docker-compose 和 nginx 运行的 docker 容器。 我想在它特定的主机路径上运行每一个。 (在它自己的端口上运行它们也对我有用)

http://host/container1
http://host/container2
or 
http://host:80
http://host:81

我的 nginx 模板来自:https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl

我可以单独运行它们,因此它们可以作为单个容器与 nginx 一起运行。 我尝试修改location / { proxy_pass .. } 以发送到不同的端口(localhost:80,localhost:81)。

添加到 L237 https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl

location /container1 {
    proxy_pass {{ trim $proto }}://{{ trim $host }};
}

location /container2 {
    proxy_pass {{ trim $proto }}://{{ trim $host }}:81;
}

docker-compose.yml

version: '2'
services:
  nginx-container:
    image: nginx
    container_name: nginx-container
    ports:
    # "server_host_port:nginx_container_port"
      - "80:80"
      - "81:81"
    volumes:
      - /etc/nginx/conf.d
      - ./docker-gen/proxy.conf:/etc/nginx/proxy.conf
  docker-gen:
    image: jwilder/docker-gen
    command: -notify-sighup nginx-container -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    volumes_from:
      - nginx-container
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./docker-gen/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl
  container-one:
    build: ./container-one
    image: docker.repository.testing.com/container-one
# Exposing 8080 for dockergen
    expose:
      - 8080
    environment:
      - spring.profiles.active=docker,testing
      - nginx.host=nginx-container
      - nginx.port=80
      - VIRTUAL_HOST=host
    links:
      - nginx-container
    depends_on:
      - nginx-container
  container-two:
    build: ./container-two
    image: docker.repository.testing.com/container-two
# Exposing 8080 for dockergen
    expose:
      - 8080
    environment:
      - spring.profiles.active=docker,testing
      - nginx.host=nginx-container
      - nginx.port=81
      - VIRTUAL_HOST=host
    links:
      - nginx-container
      - container-one
    depends_on:
      - nginx-container
      - container-one

据我了解: 我的容器一和容器二是这样运行的:

container-one -> nginx-container:80
container-two -> nginx-container:81

两者都使用 virtual_host “主机”

所以我应该能够将 nginx.tmpl 文件中的“位置”更改为指向容器上的主机:端口

但是这不起作用... 有时我得到 nginx 欢迎页面,有时我得到 503 或 404

我不确定我错过了什么或做错了什么。 这是配置它的正确方法吗?

参考资料: https://github.com/jwilder/nginx-proxy https://gist.github.com/soheilhy/8b94347ff8336d971ad0 https://github.com/nbellocam/sample-site-docker/blob/master/nginx/conf/nginx.conf http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

【问题讨论】:

  • 如果您想要 2 个不同的容器,您将需要不同的端口或第三个充当反向代理。
  • 每个容器都在它的端口上运行,而 nginx 是反向代理 :) 除非我有什么问题......
  • 我的服务器上有这样的设置,我给这个问题加注星标,如果你没有得到帮助,我明天早上把我的 compose 文件发给你。
  • 我会非常感激 :)
  • 不改变 nginx.tmpl 是否可以工作?

标签: nginx docker docker-compose reverse-proxy


【解决方案1】:

我最终使用了两个VIRTUAL_HOST,每个容器一个。

host.container1.com
host.container2.com

这很有效,尽管我必须添加一个新主机来匹配虚拟主机。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多