4

Jenkins 构建部分中的 Bash 脚本

#!/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. 在这里,我正在创建一个 docker 图像
  2. 向其添加适当的标签以推送到私有存储库
  3. 如果堆栈存在,则删除堆栈而不是再次部署它。

问题是它在第一次构建中失败,并在下面发布了一些网络问题。如果再次构建,它将在另一个构建上成功运行。我该如何解决这个问题。

我得到的错误:

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

正如我们所看到的,它删除了服务,并且在服务创建期间,它的投掷网络没有发现问题。

这是我的 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:
4

1 回答 1

7

问题是网络没有立即删除,docker stack rm smstake而是在此命令启动之后docker stack deploy -c docker-compose.yml smstake

您可以等待网络真正被删除,然后再次部署堆栈,如下所示:

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

while只要网络smstake_smstake仍然存在,它就会执行。

于 2018-03-29T08:58:48.627 回答