I try to achieve the following on deployment:
- [if an old container is found] rename and stop
- run a new container from the newest image
I try it this way (this is code of my gitlab-ci.yml)
# these two work, so docker is running and reachable
- ssh deploy@$URL_STAGE docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
- ssh deploy@$URL_STAGE docker pull $CI_REGISTRY_IMAGE/$AMC_BACKEND_DOCKER_IMAGE_NAME:latest
# rename and stop ('docker inspect' to ensure the container exists, see https://stackoverflow.com/a/45171589)
- ssh deploy@$URL_STAGE docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME > /dev/null 2>&1 && docker rename $AMC_BACKEND_DOCKER_IMAGE_NAME $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp
- ssh deploy@$URL_STAGE docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp > /dev/null 2>&1 && docker stop -t 5 $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp # sends SIGTERM and after t seconds SIGKILL
The last lines of the log are
$ ssh deploy@$URL_STAGE docker pull $CI_REGISTRY_IMAGE/$AMC_BACKEND_DOCKER_IMAGE_NAME:latest
...
Status: Downloaded newer image for ...:latest
$ ssh deploy@$URL_STAGE docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME > /dev/null 2>&1 && docker rename $AMC_BACKEND_DOCKER_IMAGE_NAME $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp
/bin/sh: eval: line 155: docker: not found
- I copied the same command to the server (replacing the variables) and it works there.
- Also works with
ssh localhost docker ...
- Also does not work when removing
> /dev/null 2>&1
Somehow the command after && seems to be run "somewhere else" (where no command docker exists)...
Any idea?