After I installed Docker to my Linux Mint machine with
sudo apt install docker.io, and then tried to devcontainer in vscode, it shows the following.
Let’s check which group the current user belongs to and add the user to docker group.
How to check which group a user belongs to
Let’s execute the following command.
$ groups yuto adm cdrom sudo dip plugdev lpadmin sambashare
The first one is the user name. The rest names are the group names to which the user belongs. If you need to check with other users, add user name(s) to the parameter.
$ groups root root : root
It can show multiple users too.
$ groups yuto root yuto : yuto adm cdrom sudo dip plugdev lpadmin sambashare root : root
id command can also be used. This command shows the groups with the corresponding ID.
$ id uid=1000(yuto) gid=1000(yuto) groups=1000(yuto),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),115(lpadmin),136(sambashare)
Add the desired user name if you want to check another user.
$ id root uid=0(root) gid=0(root) groups=0(root)
How to list all the existing groups
Read /etc/group file
/etc/group contains all the group names.
$ cat /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,yuto tty:x:5: ...
There are too many group names to look for the desired one. In this case, use grep command. I want to check if docker group exists.
$ cat /etc/group | grep docke* docker:x:139:
As you can see, an asterisk can be used there.
This command gets entries from administrative database. You could type the command a bit faster.
$ getent group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,yuto tty:x:5: ...
Add a group
Let’s add a group if the desired group has not existed yet. You can add a group with the following command.
$ sudo groupadd <group-name-here>
Let’s try to add
$ getent group | grep super* $ sudo groupadd super-test-group $ getent group | grep super* super-test-group:x:1001:
It was added after the command.
Delete a group
If you want to remove it use
groupdel command instead.
$ getent group | grep super* super-test-group:x:1001: $ sudo groupdel super-test-group $ getent group | grep super*
Add the desired user into a group
The command is the following.
$ sudo usermod -aG <group-name-here> $USER
Don’t forget to add
-a option! If you forget to add it, all the other groups are removed from the user. I have done that once. I could restore it because I checked which group the user belongs to. Don’t do the same mistake.
-G option is necessary to add the specified group(s) to the user.
Let’s add docker group to my user.
$ groups yuto adm cdrom sudo dip plugdev lpadmin sambashare $ sudo usermod -aG docker $USER $ groups yuto adm cdrom sudo dip plugdev lpadmin sambashare
It didn’t change without doing anything.
you need to either logout and login again or execute the following command.
$ newgrp docker
After this command…
$ groups docker adm cdrom sudo dip plugdev lpadmin sambashare yuto $ groups yuto yuto : yuto adm cdrom sudo dip plugdev lpadmin sambashare docker
docker appears. I’m not sure why docker group comes first though.
The group setting is not reflected after re-login
I could confirm that docker worked after
newgrp docker command but it didn’t work after logout/login. I got the following error.
$ docker run hello-world docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'.
However, it worked after restarting the computer.