Enable docker commands run without sudo

How can I enable accessing Docker through terminal in Mac without always typing ‘sudo’?
If I type docker or docker-compose, I'm getting 'ZSH command not found, but when I’m adding sudo it works as intended.

I tried to do the tricks found through the internet, but no one actually works:
sudo dscl . delete /Groups/docker GroupMembership Myusername
sudo chmod a+x /usr/local/bin/docker-compose
All types of other advices are intended for Linux, not Mac.

The permissions seems to be okay:

lrwxr-xr-x@ 1 root  wheel     54 Apr 18 19:19 docker -> /Applications/Docker.app/Contents/Resources/bin/docker
lrwxr-xr-x@ 1 root  wheel     62 Apr 18 19:19 docker-compose -> /Applications/Docker.app/Contents/Resources/bin/docker-compose

Strangely, when I watched many videos of using Docker commands in Terminal, all users which streamed their videos didn’t used sudo or wasn’t typing this commands under the root user.

Docker latest 4.18
MacOS 13.2

Thank you.

Since the error message is “command not found” and not “permission denied”, I think there is nothing wrong with permissions and group membership. My guess is that /usr/local/bin is not in PATH variable in the ZSH shell. It could be for example because you tried to add a new fodler to PATH and acidentally overwrote it instead of appending or prepending a new value.

You can try the following ways to confirm it:

  1. The below command should show you if the necessary folder is added to the PATH.

    echo $PATH | tr ':' '\n' | grep '/usr/local/bin'
    
  2. Start a bash shell from zsh and try docker from there:

    bash -l
    

If /usr/local/bin is indeed missing from ZSH, you need to add it back or find out what and why removed it. Check your $HOME/.zshrc file for example.

Hi, Rimelek!
Thank you for your suggestions. Getting back with feedback of running commands, you mentioned above.

  1. The below command should show you if the necessary folder is added to the PATH.
    echo $PATH | tr ‘:’ ‘\n’ | grep ‘/usr/local/bin’

Runned this under my user acc without sudo:

Deemone:~ Deemone$ echo $PATH | tr ':' '\n' | grep '/usr/local/bin'
/usr/local/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin

Running this command under su has brought me the same:

sh-3.2# echo $PATH | tr ':' '\n' | grep '/usr/local/bin'
/usr/local/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin

So, as I take, the necessary folder is in the PATH and all is okay.

  1. Start a bash shell from zsh and try docker from there:
    bash -l

Here is an output. Switching zsh to bash hasn’t helped unfortunately. I made sure below that a switch of zsh → bash was done.

Deemone@Deemone ~ % echo $0       
-zsh
Deemone@Deemone ~ % bash -l
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Deemone:~ Deemone$ echo $0
bash
Deemone:~ Deemone$ docker info
bash: docker: command not found
Deemone:~ Deemone$ 

This works properly on my Mac without any sudo.

I would suggest re-install docker desktop with proper permission, it usually asks for credentials when you do drag and drop.

if you have installed docker using cli command and sudo then you have to give permission to your user to run docker.

Thank you for reporting.
I did reinstalling for several times after preceeding uninstall procedure, with no success.
I installed it straight like you showed it, by dragging and dropping to my applications folder and answered ‘yes’ when its asked for permissions.

Hello @deem0ne
below is my permission level for those binaries

and I’m already admin user

please check your user permission

1 Like

That is strange. I didn’t expect that. Please try this command too:

which docker

and also this to show the permissions of docker related binaries

ls -la /usr/local/bin/docker*

and the same in the Docker app folder:

ls -la /Applications/Docker.app/Contents/Resources/bin/

Note: While I was typing my message @alishah730 asked for whosing the permissions too, but I will keep it in my message too :slight_smile:

The strange thing is that if it is a permission issue, the message should be different and I tried to change thebroke the permissions and got different message.

1 Like

Rimelek, here is an output. I did under my user acc with admins rights without doing su/sudo:

Deemone@Deemone ~ % which docker
docker not found
Deemone@Deemone ~ % ls -la /usr/local/bin/docker*
zsh: no matches found: /usr/local/bin/docker*
Deemone@Deemone ~ % ls -la /Applications/Docker.app/Contents/Resources/bin/
total 538688
drwxr-xr-x@ 13 Deemone  admin       416 Apr  5 11:21 .
drwxr-xr-x@ 16 Deemone  admin       512 Apr  5 11:21 ..
-rwxr-xr-x@  1 Deemone  admin  45914512 Apr  5 11:21 com.docker.cli
-rwxr-xr-x@  1 Deemone  admin  23461472 Apr  5 11:21 com.docker.vpnkit
-rwxr-xr-x@  1 Deemone  admin  65666176 Apr  5 11:21 docker
-rwxr-xr-x@  1 Deemone  admin   6802320 Apr  5 11:21 docker-compose
drwxr-xr-x@ 75 Deemone  admin      2400 Apr  5 11:21 docker-compose-v1
-rwxr-xr-x@  1 Deemone  admin   9788688 Apr  5 11:21 docker-credential-desktop
-rwxr-xr-x@  1 Deemone  admin   8891712 Apr  5 11:21 docker-credential-ecr-login
-rwxr-xr-x@  1 Deemone  admin   1898016 Apr  5 11:21 docker-credential-osxkeychain
-rwxr-xr-x@  1 Deemone  admin  49481488 Apr  5 11:21 docker-index
-rwxr-xr-x@  1 Deemone  admin  14185520 Apr  5 11:21 hub-tool
-rwxr-xr-x@  1 Deemone  admin  49704688 Apr  5 11:21 kubectl

Hi, alishah730!
Here it is. The whole output of this command is above:

-rwxr-xr-x@  1 Deemone  admin  23461472 Apr  5 11:21 com.docker.vpnkit
-rwxr-xr-x@  1 Deemone  admin  65666176 Apr  5 11:21 docker
-rwxr-xr-x@  1 Deemone  admin   6802320 Apr  5 11:21 docker-compose
drwxr-xr-x@ 75 Deemone  admin      2400 Apr  5 11:21 docker-compose-v1

And my permissions is admin for sure:
Under this acc I was able to activate Root user and make other system-critical changes, so its 100% admin.
Screenshot 2023-04-23 at 22.50.43

In your first post you showed docker and docker-compose binary linked to binaries (I missed it first) in the Docker App so you shouldn’t have got an empty result after running

ls -la /usr/local/bin/docker*

What if you try this?

ls -la /usr/local/bin | grep docker

(I just don’t want to you to list all the files in that folder, because there could be commands that you don’t wan to share)

I still don’t understand what is happening but if something happened during the installation of Docker Desktop, you could try to completely remove every component which I ams ure you tried, but I know from experience it is not always easy to find all the remained compontents. I have made a video about it, although it was for an odler version but I mention in the video too that I shared only ideas not a perfect solution for every case. You can still try it: Uninstalling Docker Desktop properly on macOS - YouTube

It already helped some people

1 Like

Did it even without grep. Permission denied.

Deemone@Deemone ~ % ls -la /usr/local/bin              
total 0
ls: /usr/local/bin: Permission denied

Under SU its all works. So, as I take, there is something wring with my system.
I runned a clear copy of MacOS in Virtual machine it it successfully does ls -la /usr/local/bin without su.

Yes, I tried to remove Docker completely with searching for its files + system files included + searching in stackoverflow for where it can leave its traces. I will investigate into your video, thanks.

Now that is a problem. It looks like you don’t have permission to see files in the folder so the fact that you could execute files inside doesn’t help.

Please, try this now:

ls -ld /usr/local/bin

It should show this:

drwxr-xr-x  30 root  wheel  960 Mar 13 01:06 /usr/local/bin

The fodler has to be “executable” by anyone otherwise you can’t list files inside. If it is the case, then giving back the permision to the fodler could solve the issue (let’s hope it does)

chmod +x /usr/local/bin
1 Like

I got following:

Deemone@Deemone ~ % ls -ld /usr/local/bin
drwx------  25 root  wheel  800 Apr 19 14:15 /usr/local/bin

Do I run chmod +x /usr/local/bin, right?

Yes, you need to run chmod as root, but also add read permission

1 Like

Did chmod +xr /usr/local/bin and the problem finally resolved!

Deemone@Deemone ~ % ls -ld /usr/local/bin  
drwxr-xr-x  25 root  wheel  800 Apr 19 14:15 /usr/local/bin

After reboot Docker’s and Kubernate’s commands are accessible and even Kubernete’s context menu works like a charm. So this is resolved too now: https://forums.docker.com/t/unable-to-run-embedded-kubernetes-on-docker-desktop-mac/135652
You are Godlike! Thank you so much!
The issue is resolved.