0
  • I want to check whether a docker service is running or not.
    • if it's running I want to remove that service and create the new one
    • I am doing this task with shell script

I am providing the code snippet of my shell script where I am facing Error response from daemon: service data-mapper-service not found

if [[ "$(docker service inspect ${DOCKER_SERVICE_NAME}  2> /dev/null)" != "" ]]; then
  docker service rm ${DOCKER_SERVICE_NAME}
else echo "service doesn't exist or may have been removed manually"
fi
docker service create \
    --name ${DOCKER_SERVICE_NAME} \
    --network ${OVERLAY_NETWORK} \
    --reserve-memory ${10} \
    --constraint node.labels.run_images==yes \
    --mode global \
    --with-registry-auth \
    --restart-condition any \
    -p ${EXTERNAL_PORT}:${INTERNAL_PORT} \
    -e "SPRING_PROFILES_ACTIVE="${SPRING_PROFILE} \
    -e "JAVA_OPTS: -Xms256m -Xmx512m" \
    ${DTR_HOST_NAME}/${DTR_ORG_NAME}/${DTR_REP_NAME}:${BUILD_NUMBER}

I am getting the error on if statement line.

If the service is running and I trigger this shell script everything runs fine, But if the service is not running and I trigger this shell script I am facing above mention error.

  • You should probably just be testing the return code of the `docker inspect` command rather than looking at the output. It seems unlikely that the error is coming from the `if` statement line, because you're capturing `stdout` and redirecting `stderr` ( so that statement isn't going to generate any visible output). – larsks Mar 24 '17 at 12:58
  • 1
    @larsks : Can you please tell me what exactly I need to modify in my shell file –  Mar 24 '17 at 15:13
  • 1
    @larsks docker inspect -f {{.State.Running}} $ServiceName something like this you are talking ?? –  Mar 27 '17 at 04:27
  • Most docker commands return a shell exit code indicating success or failure. E.g., you can test if an image exists with something like `if docker image inspect myimage >/dev/null 2>&1; then echo the image exists; fi`. I am sure that `docker service inspect` can be used similarly. – larsks Mar 27 '17 at 13:06

0 Answers0