r/docker • u/mariiiiiiiiiiiiiiiel • 18h ago
how to make migrations service just run once in docker stack per deploy
EDIT: this is the thing that i had to add
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120sin a production setting what would be the way to make the migrations service just run once when a deployment is made
currently my service gets restarted each time even if the migrations were succesful, which is not what i want (or expected) i just want the migration to run once per deployment with the docker stack
or if you have another whole workflow for this im very open to read it :D
migrations:
environment:
DB_HOST: db
DB_NAME_FILE: /run/secrets/db-name
DB_PASSWORD_FILE: /run/secrets/db-password
DB_PORT: "5432"
DB_USER_FILE: /run/secrets/db-user
GOOSE_DRIVER: postgres
GOOSE_MIGRATION_DIR: /migrations
image: mymigrationimage:latest
networks:
db_net: null
secrets:
- source: db-password
target: /run/secrets/db-password
- source: db-user
target: /run/secrets/db-user
- source: db-name
target: /run/secrets/db-name
volumes:
- type: bind
source: ./init/goose-entrypoint.sh
target: /scripts/goose-entrypoint.sh
bind:
create_host_path: truethis is my customish docker image for the migrations (i just made a script that sets up the credentials variables using secrets for goose)
FROM ghcr.io/kukymbr/goose-docker:3.28.0
WORKDIR /migrations
RUN apk add --no-cache bash
COPY db/migrations/* /migrations
ENTRYPOINT ["/scripts/goose-entrypoint.sh"]