docker compose multiple projects

跟風遠走 提交于 2020-01-25 10:14:09

问题


I want to set up local DOCKER env for multiple projects under the same network. I want to have the main docker-compose config file which will create Nginx proxy and PHP container. And many child docker-compose files with own Nginx. I want all child Nginx containers listen to the main PHP container. But for some reason, child Nginx containers failing with error host not found in upstream "app-php" in /etc/nginx/vhost.d/site.cong25

main docker-compose:

version: '3.5'

services:
  app-nginx_proxy:
    build: ./nginx-proxy
    image: app/nginx-proxy:latest
    container_name: app-nginx_proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - appnetwork
  app-php:
    image: app/php:latest
    container_name: app-php
    build:
      context: ./php-fpm
      dockerfile: Dockerfile
    expose:
      - 9000
    volumes:
      - app_volume:/var/www/
    networks:
      - appnetwork
volumes:
  app_volume:
    driver: local
    driver_opts:
      type: bind
      device: "$PWD/../services"
      o: bind
networks:
  appnetwork:
    driver: bridge

and child docker-compose:

version: '3.5'

services:
  app-admin-nginx:
    build: ./nginx
    image: app-admin/nginx:latest
    container_name: app-admin-nginx
    volumes:
      - app_admin_volume:/var/www/app-admin
    networks:
      appnetwork:
        aliases:
          - app-admin.v2.local
    environment:
      - VIRTUAL_HOST=app-admin.v2.local
volumes:
  app_admin_volume:
    driver: local
    driver_opts:
      type: bind
      device: "$PWD/../services/app-admin"
      o: bind
networks:
  appnetwork:
    external: true

also, site.conf:

server {
    listen 80;
    server_name app-admin.v2.local;

    root /var/www/app-admin/web;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_log /var/log/nginx/abboom_admin_error.log;
    access_log /var/log/nginx/abboom_admin_access.log;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app-php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location ~ /\.ht {
        deny all;
    }
}

If I move all configs to the single docker-compose file all is working fine, but it's not flexible to use a single file, as I want add the ability for developers to run only apps they need and not all the app we have


回答1:


You main nginx container has dependency on app-php service. You need to start app-php before nginx.

You also need to make sure both the services run in same project so that they use same network. You can use -p option of docker-compose.

https://docs.docker.com/compose/reference/overview/

Usage: docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help

Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)

You can also use custom network to start service from different compose files in same network.

https://docs.docker.com/compose/networking/#specify-custom-networks



来源:https://stackoverflow.com/questions/59657572/docker-compose-multiple-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!