gpt4 book ai didi

docker - 如何在docker-compose文件中用 `links`替换 `depends_on`?

转载 作者:行者123 更新时间:2023-12-02 18:02:19 25 4
gpt4 key购买 nike

docker compose documentation指定 links已弃用,应替换为 depends_on .我无法确切地知道如何。

这是具有 3 个服务的应用程序的工作 docker-compose 文件(带有 links 说明):

  • 一个 nginx 反向代理
  • 一个 nodejs 应用程序
  • 一个 php api

  • version: "3.1"

    services:

    nginx:
    image: nginx:alpine
    ports:
    - "8000:80"
    volumes:
    - ./php/content:/srv/www/content
    - ./static:/srv/www/static
    - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    links:
    - php:php-app
    - nodejs:nodejs-app

    nodejs:
    image: node:alpine
    environment:
    NODE_ENV: production
    working_dir: /home/app
    restart: always
    volumes:
    - ./nodejs:/home/app
    links:
    - php:php-app
    command: ["node", "index"]

    php:
    image: php:apache
    volumes:
    - ./php:/var/www/html

    相关的nginx default.conf :
    server {
    listen 80;
    root /srv/www;
    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;

    location / {
    try_files $uri @nodejs;
    }

    location /api {
    rewrite ^([^.\?]*[^/])$ $1/ break;
    proxy_pass http://php-app:80;
    }

    location @nodejs {
    proxy_pass http://nodejs-app:8080;
    }
    }
    links指令将 docker 服务名称的别名设为 proxy_pass nginx conf 中的名称。

    如何更换 links docker-compose 文件中的说明,使用 depends_on不修改 nginx 配置(并保留别名)?

    最佳答案

    The docker compose documentation specifies that links is deprecated and should be replaced with depends_on.



    它不是。文档只说,那

    links also express dependency between services in the same way as depends_on, so they determine the order of service startup.



    我不明白这是如何得出结论的,你应该使用 depends_on而不是 links .相反,它说,如果你需要在一个容器中从另一个容器运行某些东西,你应该使用 depends_on ,不是 links . (例如,您 command -指定在 php 容器中运行迁移并需要等待 postgres 容器)。

    另一方面, links有警告说

    Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.



    在这种情况下, --linkdocker cli 与 links 相同在 docker-compose.yml .

    现在,重点是,如果您的容器在一个网络上,则不需要任何进一步的特殊规范。除非另外指定,否则默认网络驱动程序是 bridge .所以,如果你指定你的 docker-compose.yml如下所示,您应该将所有容器都放在一个网络上并自动相互了解。
    version: "3.1"

    services:
    nginx:
    image: nginx:alpine
    ports:
    - "8000:80"
    volumes:
    - ./php/content:/srv/www/content
    - ./static:/srv/www/static
    - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    nodejs:
    image: node:alpine
    environment:
    NODE_ENV: production
    working_dir: /home/app
    restart: always
    volumes:
    - ./nodejs:/home/app
    command: ["node", "index"]
    php:
    image: php:apache
    volumes:
    - ./php:/var/www/html

    在这种情况下 nginx应该与
    location / {
    try_files $uri nodejs;
    }


    location /api {
    rewrite ^([^.\?]*[^/])$ $1/ break;
    proxy_pass http://php:80;
    }

    关于docker - 如何在docker-compose文件中用 `links`替换 `depends_on`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077797/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com