简体   繁体   English

使用nginx-proxy在Docker容器中运行多个开发项目

[英]Running multiple dev projects in docker containers with nginx-proxy

As I understand a docker-compose file, using the docker-compose up command, loads the images and starts the containers. 据我了解,一个docker-compose文件,使用docker-compose up命令,加载图像并启动容器。 Conversely using a Dockerfile file with the docker build command creates the image only. 相反,将Dockerfile文件与Dockerfile docker build命令一起使用Dockerfile创建映像。 I think I am missing something here as things aren't working as I'd like. 我想我在这里丢失了一些东西,因为事情并没有按照我的意愿进行。

Following the bitnami/wordpress instructions I got an install running fine using docker-compose up d . 按照bitnami / wordpress的说明,我使用docker-compose up d进行了良好的安装。 Can then access via localhost:81 然后可以通过本地主机访问:81

version: '2'
services:
  mariadb:
    image: bitnami/mariadb:latest
    volumes:
      - /path/to/mariadb-persistence:/bitnami/mariadb
  wordpress:
    image: bitnami/wordpress:latest
    depends_on:
      - mariadb
    ports:
      - '81:80'
      - '443:443'
    volumes:
      - ./wordpress-persistence:/bitnami/wordpress
      - ./apache-persistence:/bitnami/apache
      - ./php-persistence:/bitnami/php

Because I want to be able to access this as domain.com.dev, I looked at implementing nginx-proxy . 因为我希望能够以domain.com.dev的身份进行访问,所以我考虑实现nginx-proxy Following the instructions there, and with some inspiration from Docker nginx-proxy : proxy between containers , I came up with the following: 按照那里的说明,并从Docker nginx-proxy启发:容器之间的代理 ,我想到了以下内容:

version: '2'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    restart: always
    ports:
      - "88:80"
    volumes:
      - "/var/run/docker.sock:/tmp/docker.sock:ro"

  mariadb:
    image: bitnami/mariadb:latest
    volumes:
      - //c/websites/domain_com/mariadb-persistence:/bitnami/mariadb
  domain.com.dev:
    image: bitnami/wordpress:latest
    depends_on:
      - mariadb
    ports:
      - '81:80'
    environment:
      - VIRTUAL_HOST=domain.com.dev
    volumes:
      - //c/websites/domain_com/wordpress-persistence:/bitnami/wordpress
      - //c/websites/domain_com/apache-persistence:/bitnami/apache
      - //c/websites/domain_com/php-persistence:/bitnami/php

Running docker-compose up -d with this appears to complete without error. 使用此命令运行docker-compose up -d似乎已完成,没有错误。 However when I access domain.com.dev in a browser, I get a default Index of / page, which suggests I somehow got partway there but not all the way. 但是,当我在浏览器中访问domain.com.dev时,会得到默认的/ page索引,这表明我以某种方式进入了其中,但并非一路走来。 Looking at the local folders, they get created but it seems like the wordpress-persistence does not get populated, which could explain the default view in the browser. 查看本地文件夹后,便会创建它们,但似乎没有填充wordpress-persistence ,这可以解释浏览器中的默认视图。

Any thoughts on why this isn't coming up as expected? 关于为何未按预期进行的任何想法? Something obvious I missed? 我错过了明显的东西吗?

1) For the first approach, you need "to finish" the configuration. 1)对于第一种方法,您需要“完成”配置。 If you don't have a running webserver (nginx, apache, etc.) (on port 80) - just change the port from 81 to 80 : 如果您没有正在运行的网络服务器(nginx,apache等)(在端口80上)-只需将端口从81更改为80

ports:
      - '80:80'
      - '443:443'

and add the record " 127.0.0.1 domain.com.dev " to your hosts file ( /etc/host s in linux). 并将记录“ 127.0.0.1 domain.com.dev ”添加到您的hosts文件(Linux中为/etc/host )。

PS you may change port from 88 to 80 at the second approach - it will work without changing hosts file PS,您可以在第二种方法中将端口从88更改为80无需更改hosts文件即可使用

If you have a running wevserver on port 80 - then it is needed to you proxy directives at virtualhost config file. 如果您在端口80上有一个正在运行的wevserver-那么在virtualhost config文件中的代理指令就需要它。 Here is an example: 这是一个例子:

server {
    listen 80 default_server;
    server_name _;

    include expires.conf;

    location / {
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://172.17.0.1:81;
        proxy_http_version 1.1;
    }
}

2) The second approach is usually used with dnsmasq configuration. 2)第二种方法通常用于dnsmasq配置。 Use this and this links to get more detailed information and examples of configuration. 使用链接和链接可以获取更多详细信息和配置示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM