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.

  • Secrets with containerized single-node kubernetes cluster
  • Docker registry and index
  • Docker PHP Sessions Not Persisting
  • Kubernetes delete secret docker-registry error Unable to connect to the server: x509
  • Translate docker run command to docker compose
  • How to use stdin, stdout and stderr streams after attaching to a container using Docker api?
  • 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.

  • Slow DNS resolution inside docker container
  • Docker for Mac Host Networking
  • Authentication issue in Docker-MongoDB
  • Zero Downtime app deployment with CoreOS
  • Communication between two Docker containers on macOS 10.12
  • Docker crash test with many containers of the same image
  • 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.