Search code examples
dockergitlab-ci

xargs docker stop needs at least 1 argument in GitLab CI job


I have a GitLab CI/CD YAML pipeline file with some jobs, the last stage has this code:

deploy:
  stage: deploy
  before_script:
    - chmod 400 $SSH_KEY
  script:
    - ssh -o StrictHostKeyChecking=no -i $SSH_KEY [email protected] "
        docker login -u $REGISTRY_USER -p $REGISTRY_PASS &&
        docker ps -aq | xargs docker stop | xargs docker rm && #BUG HERE!
        docker run -d -p 5000:5000 $IMAGE_NAME:$IMAGE_TAG"

xargs docker stop gives error

Login Succeeded
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
unknown shorthand flag: 'd' in -d
See 'docker rm --help'.

How to deal with it? I tried without xargs:

docker login -u $REGISTRY_USER -p $REGISTRY_PASS &&
docker stop $(docker ps -aq) | xargs docker rm &&
docker run -d -p 5000:5000 $IMAGE_NAME:$IMAGE_TAG"

but it did not work.


Solution

  • You could do something like this without the need of xargs:

      script:
        - ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" [email protected] "
            docker login -u $REGISTRY_USER -p $REGISTRY_PASS || exit;
            docker stop \$(docker ps -aq) || exit;
            docker rm \$(docker ps -aq) || exit;
            docker run -d -p 5000:5000 $IMAGE_NAME:$IMAGE_TAG"