6

I am trying to follow this post, adding the current user to the Docker group:

sudo usermod -aG docker $(whoami)

but of course, there is no usermod command on macOS:

-bash: usermod: command not found

Now I was wondering if there is an equivalent of the above command on macOS? Probably using dscl?

P.S.1. I have used these instructions to set up Docker and docker-machine.

P.S.2. This question is not about Visual Studio Code (VSCode) in particular, but if I open a new terminal and run eval "$(docker-machine env default)" and then run VSCode with code the problem is solved.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 2
    What are you trying to do to begin with? Looks like that post was an attempt to answer an issue with not being able to connect with docker. – tblev Dec 15 '21 at 19:44
  • @tblev My side issue is that I see [this error message](https://i.imgur.com/Tgiszcy.png) on Microsoft Studio Code. But I hope to resolve [this issue](https://develop.openfoam.com/Development/openfoam/-/issues/2288) too. – Foad S. Farimani Dec 15 '21 at 19:51
  • You didn't install docker on your MacOS machine, you installed it in a VM that docker no longer supports and apparently VS code isn't designed to use. The install steps for Docker Desktop for Mac are available at https://docs.docker.com/desktop/mac/install/ – BMitch Dec 15 '21 at 20:50
  • @BMitch I rather not use Docker Desktop, because 1. it is not FLOSS 2. it usually conflicts with other hypervisors. – Foad S. Farimani Dec 15 '21 at 20:51
  • 1
    This is still an X-Y problem because there's no /var/run/docker.sock in MacOS when you run the deprecated `docker-machine` install. That file is inside of the VM. If you really want to run docker without Desktop, I'd skip `docker-machine` and spin up a linux VM, follow the linux install process, and configure VS code to remotely access that VM. – BMitch Dec 15 '21 at 20:54
  • @BMitch Are XY-problems against SO policy or off-topic? – Foad S. Farimani Dec 15 '21 at 20:57
  • @BMitch can you elaborate on the `/var/run/docker.sock` point? why does it matter? – Foad S. Farimani Dec 15 '21 at 20:58
  • 1
    It's right in the error message you're trying to solve, second comment to this post. – BMitch Dec 15 '21 at 21:01
  • @BMitch then maybe I can use any of the solutions on [this page](https://github.com/docker/machine/issues/1435), to solve the VSCode issue? – Foad S. Farimani Dec 15 '21 at 21:03
  • I think I need to run the `eval "$(docker-machine env )"` command inside the VSCode. – Foad S. Farimani Dec 15 '21 at 21:20
  • I've had this error like a thousand times before, it's always been that docker wasn't started. If you're not interested in the desktop version fair enough, I've only used that and whenever I got this issue I hadn't had the program running. Is docker able to run on your system without VSCODE? What do you get with `docker ps`? I wonder if this is a VSCODE issue or a docker issue. You don't really know what is going on in the integrated terminals they provide, so use your system terminal for this. – tblev Dec 15 '21 at 21:24
  • @tblev every time I open a new terminal I have to run `eval "$(docker-machine env dev)"` otherwise I get the `Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` error. – Foad S. Farimani Dec 15 '21 at 21:26
  • I still don't understand the reason for the question. How would adding your user to the docker group in MacOS help with accessing Docker when it's not running in MacOS (it's running in a VM)? – BMitch Dec 16 '21 at 03:14
  • I think I have found my answer on [this page](https://superuser.com/questions/214004/how-to-add-user-to-a-group-from-mac-os-x-command-line). I am gonna investigate and write an answer myself. – Foad S. Farimani Dec 16 '21 at 07:06
  • Try uninstalling and reinstalling, that works some times. – tblev Dec 16 '21 at 21:08
  • @tblev by uninstalling and installing what? – Foad S. Farimani Dec 16 '21 at 21:49

2 Answers2

13

This question, as others have also pointed out, is irrelevant. The process of adding a user to the docker group is only necessary on Linux where sudo privileges are required to run Docker commands, as explained here. On macOS, and using docker-machine, that is unnecessary.


But if one wants to add a user, or more specifically the current user, to the docker user group, for whatever reason, here are the instructions:

  • List the existing user groups with dscl . list /groups from here
  • To create a user group, if it doesn't exist use the command sudo dscl . create /Groups/<groupName> from here.
  • In the context of this discussion the <groupName> could be replaced with docker.
  • To add a user to a group one can use the command sudo dseditgroup -o edit -a <userName> -t user <groupName>. from here or sudo dscl . append /Groups/<groupName> GroupMembership <userName> from here.
  • One can replace the <userName> with $USER or $(whoami) to refer to the current user.
  • To test and see if the expected user has been added o the specific group one can use the command dscl . -read /Groups/<groupName> GroupMembership to list all the remembers. However, it is not guaranteed to deliver the correct result, as explained here.

And the another issue with the Visual Studio Code, also has barely anything to do with the user groups. By running the eval "$(docker-machine env <dockerMachineName>)" in a new terminal, and running the code editor from inside the terminal, the Docker extension works just fine.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
2

I also had to write sudo on mac to run docker commands (I don't know why). Thanks to Foad S. Farimani explanation I fixed it.

I just want to add commands which are ready for using. First one creates docker group. Second one adds current user into docker group.

sudo dscl . create /Groups/docker
sudo dseditgroup -o edit -a $USER -t user docker

If you need something different read Foad S. Farimani answer.

Konstantin
  • 221
  • 3
  • 7