Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 root@161.35.223.117 "
        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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" root@161.35.223.117 "
        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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading