Tara Prasad Gurung
Tara Prasad Gurung

Reputation: 3559

docker stack rm followed by deploy fails and runs on consecutive builds

Bash script in Jenkins build sections

#!/bin/sh

docker build -t smstake:latest .

#Push to the private  repository applying the proper tags for the image
docker tag smstake:latest reg01.dev.01cloud.com:5000/smstake:1.0.1
docker push reg01.dev.01cloud.com:5000/smstake:1.0.1

docker stack rm smstake
docker stack deploy -c docker-compose.yml smstake
  1. Here I am creating an docker image
  2. Adding the proper tags to it to push to private repository
  3. Removing the stack if it exists than deploying it again.

The problem is it fails in the first build with some networks issues which is posted bellow. If build again than it will run successfully on another build. How can I fix this issue.

Errors I get:

Removing service smstake_app
Removing service smstake_db
Removing service smstake_phpmyadmin
Removing network smstake_smstake
Creating service smstake_app
failed to create service smstake_app: Error response from daemon: network smstake_smstake not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

As we can see its removing the service and during the service creation its throwing network not found issues.

Here is my docker-compose.yml

version: '3.4'

networks:
  smstake: 
    ipam:
      config:
        - subnet: 10.0.10.0/24


services:

    db:
        image: mysql:5.7
        networks:
          - smstake
        ports:
          - "3306"
        env_file:
          - configuration.env
        environment:
          MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
          MYSQL_DATABASE: ${DB_NAME}
          MYSQL_USER: ${DB_USER}
          MYSQL_PASSWORD: ${DB_PASSWORD}
        volumes:
          - mysql_data:/var/lib/mysql
        deploy:
          mode: replicated
          replicas: 1

    app:

        image: smstake:latest
        ports:
          - 8000:80
        networks:
          - smstake

        depends_on:
          - db
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints:
              - node.role == manager

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        networks:
          - smstake
        depends_on:
          - db
        environment:
          PMA_HOST: db
          PMA_PORT: 3306
          MYSQL_USER: *****
          MYSQL_PASSWORD: *****
        ports:
          - '8082:80'
        deploy:
          mode: replicated
          replicas: 1

volumes:
    mysql_data:

Upvotes: 4

Views: 1954

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

The problem is that the network is not removed right away in the docker stack rm smstake but after this command starts docker stack deploy -c docker-compose.yml smstake.

You could wait for the network to be really removed and then deploy the stack again, like this:

docker stack rm smstake
while [ $(docker network ls | grep smstake_smstake |  wc -c) -ne 0 ] ; do sleep 1 ; done
docker stack deploy -c docker-compose.yml smstake

The while is executing as long as the network smstake_smstake still exists.

Upvotes: 8

Related Questions