0

I am trying to run docker image inside ec2 instance using gitlab CI/CD.

Trying to expose 5000 port for the application.

But i am aware of the face this job will work for the first time, but for the susequent runs the job will fail, as docker does not allow to run image on the same port, so i am trying to implement a fail safe mechanism where where running it checks for the process, if it exist, it will stop and remove container and then run the image on port 5000.

Here i am facing the problem that if this job runs for the first time docker stop needs at least one argument in the current command.

is there a way to run this command in a if condition basis, like if process exist then only run otherwise dont.

deploy:
  stage: deploy
  before_script:
    - chmod 400 $SSH_KEY
  script: ssh -o StrictHostKeyChecking=no -i $SSH_KEY ec2-user@ecxxxxx-xxxx.ap-southeast-1.compute.amazonaws.com "
          docker login -u $REGISTRY_USER -p $REGISTRY_PASS &&
          docker ps -aq | xargs docker stop | xargs docker rm  &&
          docker run -d -p 5000:5000 $IMAGE_NAME:$IMAGE_TAG"

error on pipeline

"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers

The problem is with xargs docker stop | xargs docker rm command. is there a way to solve this kind of problem

Edit :- This doesn't exactly answer my question because what if a junior engineer is assigned this task to setup a pipeline who doesn't know the name of image, this solution requires us to know the name of the image, in this case this won't work.

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
  • Does this answer your question? [How to execute a Bash command only if a Docker container with a given name does not exist?](https://stackoverflow.com/questions/38576337/how-to-execute-a-bash-command-only-if-a-docker-container-with-a-given-name-does) – Paolo Jul 25 '22 at 18:37
  • This doesn't exactly answer my question because what if a junior engineer is assigned this task to setup a pipeline who doesn't know the name of image, because this solution requires us to know the name of the image, in this case this wont work. – Jatin Mehrotra Jul 26 '22 at 02:15
  • Try a single pipe into `xargs` of a single command/argument with the instead of `| xargs ... | xargs ...` as shown here: [Piping commands after a piped xargs](https://unix.stackexchange.com/a/209250/453397) – sytech Jul 27 '22 at 00:43

1 Answers1

0

Here what I understood is you are not stopping image but you are stopping container and removing it and then creating new container with the expose port 5000. So give a variable constant name to container which will be same whenever it creates. The "|| true" helps you to stop the container only if it exists if not it won't stop any container

    variables:
      CONTAINER_NAME: <your-container-name> #please give a name for container to be created for this image

    deploy:
      stage: deploy
      before_script:
        - chmod 400 $SSH_KEY
      script: ssh -o StrictHostKeyChecking=no -i $SSH_KEY ec2-user@ecxxxxx-xxxx.ap-southeast-1.compute.amazonaws.com "
              docker login -u $REGISTRY_USER -p $REGISTRY_PASS" &&
              docker stop $CONTAINER_NAME; docker rm $CONTAINER_NAME || true  &&
              docker run -d -p 5000:5000 --name $CONTAINER_NAME $IMAGE_NAME:$IMAGE_TAG"
nagasaiii
  • 11
  • 1