Home > Net >  conditional docker command - docker not found
conditional docker command - docker not found

Time:10-05

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?

CodePudding user response:

I do it like this now (using a multiline command to just connect once)

- |
  ssh deploy@$URL_STAGE "
    # -x Makes the server print the executed commands to stdout. 
    # -e Makes the execution stop when one of the commands fails.
    set -x -e

    ### keep the last two containers for easy roleback ###
    # if there is a bkp2 container -> remove it
    docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp2 > /dev/null 2>&1   && docker rm $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp2
    
    # if there is a bkp container -> rename it to bkp2
    docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp  > /dev/null 2>&1   && docker rename $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp2        

    # if there is a former app container -> stop it and rename it to bkp
    docker container inspect $AMC_BACKEND_DOCKER_IMAGE_NAME      > /dev/null 2>&1   && docker stop -t 5 $AMC_BACKEND_DOCKER_IMAGE_NAME \
                                                                                    && docker rename $AMC_BACKEND_DOCKER_IMAGE_NAME $AMC_BACKEND_DOCKER_IMAGE_NAME-bkp
    
    ### run the new app container ###
    docker run \
      --name $AMC_BACKEND_DOCKER_IMAGE_NAME \
      -d \
      -p $AMC_BACKEND_LISTEN_ADRESS:3000 \
      -e DOTENV_CONFIG_PATH=$ENV_FILE_PATH_IN_CONTAINER \
      --mount type=bind,source=$ENV_FILE_PATH_ON_SERVER,target=$ENV_FILE_PATH_IN_CONTAINER,readonly \
      --restart unless-stopped \
      $CI_REGISTRY_IMAGE/$AMC_BACKEND_DOCKER_IMAGE_NAME:latest
  "
  • Related