Docker 卷按字母顺序运行,而不是我放置它们的顺序

发布于 2025-01-09 02:20:10 字数 719 浏览 1 评论 0原文

我有一个看起来像这样的 docker-compose.yml 文件,

version: '3.7'
services:
    postgres:
        image: postgres:12.7
        restart: always
        environment:
          - POSTGRES_USER=xxxx
          - POSTGRES_PASSWORD=xxxx
        logging:
          options:
            max-size: 10m
            max-file: "3"
        ports:
          - '5438:5432'
        volumes:
          - ./A.sql:/docker-entrypoint-initdb.d/A.sql
          - ./B.sql:/docker-entrypoint-initdb.d/B.sql
          - ./D.sql:/docker-entrypoint-initdb.d/D.sql
          - ./C.sql:/docker-entrypoint-initdb.d/C.sql

我将卷中的文件这样放置,以便首先运行 A,第二运行 B,第三运行 D,最后运行 C。但是,当我运行 docker-compose up 时,它按字母顺序运行文件,而不是我放置它们的顺序。有没有解决这个问题的方法?

I have a docker-compose.yml file that looks like this

version: '3.7'
services:
    postgres:
        image: postgres:12.7
        restart: always
        environment:
          - POSTGRES_USER=xxxx
          - POSTGRES_PASSWORD=xxxx
        logging:
          options:
            max-size: 10m
            max-file: "3"
        ports:
          - '5438:5432'
        volumes:
          - ./A.sql:/docker-entrypoint-initdb.d/A.sql
          - ./B.sql:/docker-entrypoint-initdb.d/B.sql
          - ./D.sql:/docker-entrypoint-initdb.d/D.sql
          - ./C.sql:/docker-entrypoint-initdb.d/C.sql

I have the files in the Volumes placed like this so that A will be run first, B second, D third, and C last. However, when I run docker-compose up, it's running the files in alphabetical order as opposed to the order I placed them in. Is there anyway around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2025-01-16 02:20:10

您可以映射文件,以便它们在容器内具有名称,从而使它们按照您想要的顺序执行。像这样

volumes:
  - ./A.sql:/docker-entrypoint-initdb.d/A.sql
  - ./B.sql:/docker-entrypoint-initdb.d/B.sql
  - ./D.sql:/docker-entrypoint-initdb.d/C.sql
  - ./C.sql:/docker-entrypoint-initdb.d/D.sql

You can map the files, so they have names inside the container that'll cause them to be executed in the order you want. Like this

volumes:
  - ./A.sql:/docker-entrypoint-initdb.d/A.sql
  - ./B.sql:/docker-entrypoint-initdb.d/B.sql
  - ./D.sql:/docker-entrypoint-initdb.d/C.sql
  - ./C.sql:/docker-entrypoint-initdb.d/D.sql
淡淡绿茶香 2025-01-16 02:20:10

docker postgres 镜像按字母顺序运行初始化脚本:

如果您想在从此镜像派生的镜像中进行其他初始化,请在 /docker-entrypoint-initdb.d 下添加一个或多个 *.sql、*.sql.gz 或 *.sh 脚本(创建目录(如果需要)。在入口点调用 initdb 创建默认的 postgres 用户和数据库后,它将运行任何 *.sql 文件,运行任何可执行的 *.sh 脚本,并获取在该目录中找到的任何不可执行的 *.sh 脚本,以在之前进行进一步的初始化启动服务。

[...]

这些初始化文件将按照当前区域设置(默认为 en_US.utf8)定义的名称顺序执行。任何 *.sql 文件都将由 POSTGRES_USER 执行,默认为 postgres 超级用户。建议使用 --username "$POSTGRES_USER" 标志以 POSTGRES_USER 身份执行 *.sh 脚本内运行的任何 psql 命令。由于容器内建立的 Unix 套接字连接存在信任身份验证,因此该用户无需密码即可进行连接。

来源。

如果您想更改顺序,则必须更改文件的名称。它们安装到容器中的顺序将被忽略。

The docker postgres image runs the initialization scripts in alphabetical order:

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.

[...]

These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8. Any *.sql files will be executed by POSTGRES_USER, which defaults to the postgres superuser. It is recommended that any psql commands that are run inside of a *.sh script be executed as POSTGRES_USER by using the --username "$POSTGRES_USER" flag. This user will be able to connect without a password due to the presence of trust authentication for Unix socket connections made inside the container.

Source.

If you wish to change the order, you must change the names of the files. The order that they are mounted into the container is ignored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文