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. Spring application. set & get environment variable
  • How to enable CLI history in docker container?
  • Docker swarm load balancing on AWS not working
  • How does Docker Swarm start a container
  • Docker --env-file - executable file not found in $PATH
  • Kubernetes flannel network does not work as expected
  • 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.

  • Docker, install postgresql in a container contain “ubuntu:14.04”, lost when out of the container
  • Angular2 cannot find module: './class' within docker
  • mvn jetty:run not reflecting changes when run inside docker
  • Among Kubernetes, OpenShift, Docker Swarm and Docker DataCenter deployment tools which all are having automatic rollback?
  • When starting WAS 8.5 app server from a docker container getting 502 Bad Gateway error
  • Cannot trace error in python pcapy wrapper
  • 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.