Bash run docker command only if user has privileges (Bash if or statement)

I’m trying to only allow a script to be run if the user is allowed docker privileges. So this means the user either needs to be in the docker group or be root.

I’ve tried to do this but it doesn’t work.

  • Docker Cloud autotest cant find service
  • Executing bash then running commands in docker
  • Docker - Full functional WordPress (WP+DB+PHPMyAdmin)
  • Docker command fails during build, but succeeds while executed within running container
  • I would like to populate a entry in config file at run time via docker compose
  • Pulling image from local docker insecured Docker registry to Kubernetes
  • if [ $EUID -ne 0 ] || [ $(groups | grep -c docker) -ne 1 ]; then
      echo "You must have docker privileges to use this command."
      exit 1
    fi
    
    echo "Do docker stuff"
    

    Oddly, just the [ $EUID -ne 0 ] or the [ $(groups | grep -c docker) -ne 1 ] work fine by themselves but the OR condition doesn’t seem to work and I can’t figure out why.

  • Null port bindings from inspect, the first time. a second time good. why?
  • TensorFlow in nvidia-docker: failed call to cuInit: CUDA_ERROR_UNKNOWN
  • Is it possible to define Docker data volume on S3?
  • Creating docker image out of a production environment possible?
  • fluentd JSON Parser: get Messages that cannot be parsed
  • docker-compose volume on node_modules but is empty
  • One Solution collect form web for “Bash run docker command only if user has privileges (Bash if or statement)”

    You should be combining them with &&, not ||.

    Suppose the user is in the docker group, but not root. [ $EUID -ne 0 ] will be true, while [ $(groups | grep -c docker) -ne 1 ] will be false. || is true if either of its operands is true, so this will make the whole condition true, and the error message will be printed.

    && only succeeds if both its operands are true, so you get the message when the user is not root and they’re not in the docker group.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.