我无法部署该项目 docker-compose和nginx

问题描述

yii2中有一个有效的脚本,我无法部署它。 Docker构建了项目,一切都很好,但是如果我将docker中的Nginx配置替换为Nginx / sites-available / default,则会出现错误Nginx:在/ etc / Nginx / sites-enabled / default:111的上游“ app:9000”中找不到[emerg]主机 我在论坛上阅读了我需要添加depends_on指令的信息: 取决于: -app 但是在这种情况下,错误开始出现在: docker-compose -f docker-compose.yml up -d

我替换了不同的版本-从“ 1”到“ 3.4”,仍然出现错误。这是最后一个错误:撰写文件“ ./docker-compose.yml”无效,因为: services.environment的不支持的配置选项:'XDEBUG_CONfig' 无效的顶层属性“环境”。此撰写文件的有效顶级部分为:服务,版本,网络,卷和扩展名,以“ x-”开头。

您可能会看到此错误,因为您使用的是错误的撰写文件版本。指定受支持的版本(例如“ 2.2”或“ 3.3”),然后将服务定义放在services键下,或者省略version键并将服务定义放在目录的根文件以使用版本1。 有关Compose文件格式版本的更多信息,请参见https://docs.docker.com/compos e / compose-file /

该项目仅适用于PHP-7.0

这是原始的(来自项目)Nginx配置:

## FRONTEND ##
server {
    listen 80 default;

    root /app/frontend/web;
    index index.PHP index.html;

    server_name yii2-starter-kit.dev;

    charset utf-8;

    # location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|js)$ {
    #    access_log off;
    #    expires max;
    # }

    location / {
        try_files $uri $uri/ /index.PHP?$args;
    }

    client_max_body_size 32m;

    # There is a VirtualBox bug related to sendfile that can lead to
    # corrupted files,if not turned-off
    # sendfile off;

    location ~ \.PHP$ {
        fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass PHP-fpm;
        fastcgi_index index.PHP;
        include fastcgi_params;

        ## Cache
        # fastcgi_pass_header Cookie; # fill cookie valiables,$cookie_PHPsessid for exmaple
        # fastcgi_ignore_headers Cache-Control Expires Set-Cookie; # Use it with caution because it is cause SEO problems
        # fastcgi_cache_key "$request_method|$server_addr:$server_port$request_uri|$cookie_PHPsessid"; # generating unique key
        # fastcgi_cache fastcgi_cache; # use fastcgi_cache keys_zone
        # fastcgi_cache_path /tmp/Nginx/ levels=1:2 keys_zone=fastcgi_cache:16m max_size=256m inactive=1d;
        # fastcgi_temp_path  /tmp/Nginx/temp 1 2; # temp files folder
        # fastcgi_cache_use_stale updating error timeout invalid_header http_500; # show cached page if error (even if it is outdated)
        # fastcgi_cache_valid 200 404 10s; # cache lifetime for 200 404;
        # or fastcgi_cache_valid any 10s; # use it if you want to cache any responses
    }
}

## BACKEND ##
server {
    listen 80;

    root /app/backend/web;
    index index.PHP index.html;

    server_name backend.yii2-starter-kit.dev;

    charset utf-8;

    client_max_body_size 16m;

    # There is a VirtualBox bug related to sendfile that can lead to
    # corrupted files,if not turned-off on Vagrant based setup
    # sendfile off;

    location / {
        try_files $uri $uri/ /index.PHP?$args;
    }

    # location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|js)$ {
    #     access_log off;
    #    expires max;
    # }

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

}

## STORAGE ##
server {
    listen 80;
    server_name storage.yii2-starter-kit.dev;

    root /app/storage/web;
    index index.html;
    # expires max;

    # There is a VirtualBox bug related to sendfile that can lead to
    # corrupted files,if not turned-off
    # sendfile off;

    location / {
        try_files $uri $uri/ /index.PHP?$args;
    }

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

## PHP-FPM Servers ##
upstream PHP-fpm {
    server app:9000;
}

## MISC ##

### WWW Redirect ###
#server {
#    listen       80;
#    server_name  www.yii2-starter-kit.dev;
#    return       301 http://yii2-starter-kit.dev$request_uri;
#}

(来自项目)原始docker-compose.yml

data:
  image: busyBox:latest
  volumes:
    - ./:/app
  entrypoint: tail -f /dev/null

app:
  build: docker/PHP
  working_dir: /app
  volumes_from:
    - data
  expose:
    - 9000
  links:
    - db
    - mailcatcher
  environment:
    XDEBUG_CONfig: "idekey=PHPSTORM remote_enable=On remote_connect_back=On"

Nginx:
  image: Nginx:latest
  ports:
    - "80:80"
  volumes:
    - ./:/app
    - ./docker/Nginx/vhost.conf:/etc/Nginx/conf.d/vhost.conf
  links:
    - app

mailcatcher:
    image: schickling/mailcatcher:latest
    ports:
      - "1080:1080"

db:
  image: MysqL:5.7
  volumes:
    - /var/lib/MysqL
  ports:
    - "3306:3306"
  environment:
    MysqL_ROOT_PASSWORD: root
    MysqL_DATABASE: yii2-starter-kit
    MysqL_USER: ysk_dbu
    MysqL_PASSWORD: ysk_pass

帮助请为Nginx和docker-compose进行正确的配置。

在yii2新手处。 非常感谢您的帮助和建议。

提前谢谢!

解决方法

在顶层没有version:键的撰写文件是version 1 Compose file。这是Compose YAML文件格式的非常的旧版本,不支持网络,卷或其他现代Compose功能。您应该选择版本2或版本3。(版本3面向Docker的Swarm Orchestrator,因此对于单主机设置中的某些特定选项,您可能需要指定版本2。)您需要指定顶层version:键,然后将您需要的服务置于顶级services:键之下。

version: '3.8'
services:
  app: { ... }
  nginx: { ... }
  mailcatcher: { ... }
  db: { ... }

这实际上将直接解决您的直接问题。如Networking in Compose中所述,Compose(带有版本2或3的配置文件)将为您创建一个default网络并注册容器,以便它们的服务名称(如app)可以用作主机名。您不需要links:或其他配置。

您显示的“撰写”文件中还有许多其他不必要的选项。您无需将图像的WORKDIR重复为容器的working_dir:;您不需要expose:端口(与将ports:发布到主机不同);用来自主机的COPY覆盖将volumes:写入图像的代码并不是一个好习惯。

在现代Docker中,您还倾向于不使用数据量容器。取而代之的是,较新版本的Compose具有可以声明命名卷的top-level volumes: key。例如,您可以将其用于备份数据库存储。

所有这些的最终结果将是一个Compose文件,例如:

# Specify a current Compose version.
version: '3.8'

# Declare that we'll need a named volume for the database storage.
volumes:
  mysql_data:

# The actual Compose-managed services.
services:
  app:
    build:
      # If the application is in ./app,then the build context
      # directory must be the current directory (to be able to
      #   COPY app ./
      # ).
      context: .
      dockerfile: docker/php/Dockerfile
    environment:
      XDEBUG_CONFIG: "idekey=PHPSTORM remote_enable=On remote_connect_back=On"

  nginx:
    # Build a separate image that will also
    #   FROM nginx
    #   COPY app /usr/share/nginx/html
    #   COPY docker/nginx/vhost.conf /etc/nginx/conf.d
    build:
      context: .
      dockerfile: docker/nginx/Dockerfile
    ports:
      - "80:80"

  mailcatcher:
    image: schickling/mailcatcher:latest
    ports:
      - "1080:1080"

  db:
    image: mysql:5.7
    volumes:
      # Use the named volume we declared above
      - mysql_data:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: yii2-starter-kit
      MYSQL_USER: ysk_dbu
      MYSQL_PASSWORD: ysk_pass