Home > other >  Docker Volumes running in alphabetical order instead of the order I placed them
Docker Volumes running in alphabetical order instead of the order I placed them

Time:02-23

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related