Docker Swarm deploy - wait for service / container to be present

I have deployed swarm deployment and update deployment. Since I have to perform some tasks after deployment (for example, database migration), I added the manager service to the stack. this service is limited to node-manager - so I always have a way to find it.

To get the current container id, I use this command:
export MANAGER_ID=$(docker --tls ps --filter label=com.docker.swarm.service.name=projectname-php-manager -q)

This works ... but not during deployment.

stack deploywill be released soon (before the container is closed) or even before the manager's container is updated. I also added sleep 10to get the container id, but the results are changing.

Is there a way to wait or find out when a particular service is being deployed?

The full deployment looks like this (performed in the gitlab-ci job, but this is not the root of the problem):

deploy:staging:
  variables:
    DOCKER_HOST: "tcp://swarm-manager.hostname.tld:2376"
    DOCKER_CERT_PATH: "/home/gitlab-runner/docker/swarm-manager.hostname.tld"
    VERSION_TAG: "$CI_COMMIT_TAG"
    MYSQL_PROD_PASSWORD: "$MYSQL_PROD_PASSWORD"
    SECRET_TOKEN: "$SECRET_TOKEN"
  script:
    - docker --tls stack deploy -c docker-compose.prod.yml project-name --with-registry-auth --prune
    - sleep 10
    - export MANAGER_ID=$(docker --tls ps --filter label=com.docker.swarm.service.name=project-name_php-manager -q)
    - docker --tls exec -t $MANAGER_ID bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration
  stage: deploy
  environment:
    name: staging
    url: http://projectname.com
  only: [tags]
  cache: ~
  dependencies:
    - build:app
  tags:
    - deploy

Part from docker-compose.prod.yml file:

php-manager:
    image: dockerhub.mydomain.tld/namespace/projectname/php:${VERSION_TAG}
    environment:
        DATABASE_URL: "mysql://projectname:${MYSQL_PROD_PASSWORD}@mysql:3306/projectname?charset=utf8mb4&serverVersion=5.7"
        APP_ENV: prod
        APP_SECRET: "${SECRET_TOKEN}"
        VERSION: "${VERSION_TAG}"
        REDIS_HOST: redis
    networks:
      - default
    deploy:
      placement:
        constraints: [node.role == manager]
      replicas: 1
      restart_policy:
        condition: on-failure
+4
source share
1 answer

Deploying the Docker stack creates tasks that try to force the system to the desired state. Sometimes tasks succeed, sometimes they fail, and the orchestra generates new tasks until the system matches the state described in your yml files.

The bad news is that deploying a docker stack does not support locking until the desired state is reached.

Here are some ways to get the information you want using docker cli and basic bash tools (which you can accurately implement in any other language)

bash docker service ls --format '{{.ID}} {{.Name}}' | grep ${serviceName}, ServiceId ( )

docs docker service ps :

, .

docker service ps ${ServiceId} --format '{{.CurrentState}} {{.Image}}' | grep Running.*${newImageName}

-, . :)

, . Docker service ps .

FYI: Swarm:

NEW .

.

ASSIGNED Docker .

ACCEPTED node. node , REJECTED.

Docker .

.

.

COMPLETE .

FAILED .

SHUTDOWN Docker .

REJECTED node .

ORPHANED node .

+4

Source: https://habr.com/ru/post/1688891/


All Articles