kamal icon indicating copy to clipboard operation
kamal copied to clipboard

kamal 2: fixing docker user permission automatically on setup?

Open imWildCat opened this issue 1 year ago • 14 comments

this issue has been existing from kamal 1

  ERROR (SSHKit::Command::Failed): Exception while executing on host 20.64.146.45: docker exit status: 1
docker stdout: Nothing written
docker stderr: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.47/images/create?fromImage=registry.gitlab.com%2Fcat-studio%2Fdemo_rails8_tailwind_auth%2Fraills-app&tag=aed00caeedff437959b8a1638346d1843680bdaa": dial unix /var/run/docker.sock: connect: permission denied

this is on a fresh setup

we have to run something like

azureuser@rails-8-demos:~$ sudo groupadd docker
sudo usermod -aG docker $USER
groupadd: group 'docker' already exists

if the team is ok with this I can open a PR

imWildCat avatar Sep 26 '24 18:09 imWildCat

This is when you're using a non-root ssh user? Would be nice if that case didn't require anything special, yeah. Please do explore a PR.

dhh avatar Oct 04 '24 23:10 dhh

yes. going to draft a PR!

imWildCat avatar Oct 05 '24 02:10 imWildCat

The documentation notes that if we're using a non-root user, we need to manually bootstrap the server (https://kamal-deploy.org/docs/configuration/ssh/).

I looked into codebase, and came up with this pseudo-code approach

https://github.com/basecamp/kamal/blob/e34031f70cf933384fe0467ade20d6653f3ea908/lib/kamal/cli/server.rb#L33-L35

After line 34

unless KAMAL.config.ssh.user == 'root'
  info "Adding User #{KAMAL.config.ssh.user} to group docker"
  execute *KAMAL.docker.add_group(KAMAL.config.ssh.user)
end

#lib/kamal/commands/docker.rb
def add_group(username)
  shell ["sudo usermod -aG docker #{username}"]
end

nikhilbhatt avatar Oct 06 '24 19:10 nikhilbhatt

@nikhilbhatt thanks! do you want to create this PR?

imWildCat avatar Oct 06 '24 20:10 imWildCat

@imWildCat Please go ahead as you have already opened up the issue and working on it.

nikhilbhatt avatar Oct 07 '24 04:10 nikhilbhatt

Probably better just to add another command sequence to docker.rb following the pattern of the superuser? method and run that on the servers.

def add_group 
  [ '[ "${EUID:-$(id -u)}" -eq 0 ] || sudo usermod -aG docker "${USER:-$(id -un)}"' ]
end

then add execute *KAMAL.docker.add_group after the install line in server.rb

That's far easier to add to the existing Kamal::Commands::Docker tests than anything conditional.

NeilW avatar Oct 08 '24 14:10 NeilW

I hope you'll forgive me for butting in here and opening the PR to fix this.

Comments welcome.

NeilW avatar Oct 10 '24 16:10 NeilW

I hope you'll forgive me for butting in here and opening the PR to fix this.

Comments welcome.

No worries at all! I’m too busy recent days. Apologies for the delay!

imWildCat avatar Oct 10 '24 20:10 imWildCat

+1 -- either having it automated as part of kamal setup or a warning / log message at least would be nice!

ashish-stargaze avatar Oct 14 '24 16:10 ashish-stargaze

Is there any progress with this?

eimantas avatar Jan 20 '25 07:01 eimantas

I am facing the same issue. Is there any progress with this? Or a workaround?

ps-ruby avatar May 20 '25 17:05 ps-ruby

I am facing the same issue. Is there any progress with this? Or a workaround?

you can run the groupadd command manually:

kamal server exec "sudo usermod -aG docker {{ USER }}"

stanvanrooy avatar Aug 22 '25 12:08 stanvanrooy

Thanks @stanvanrooy for this solution. Seems better than SSH-ing into the VM 😅 still tho, why run the command manually when it could be run automatically ;)

eimantas avatar Sep 03 '25 09:09 eimantas

We’ve been using a workaround that’s close to an automated solution and works reasonably well.

Our .kamal/hooks/docker-setup file looks like this:

#!/bin/sh
# Remove once https://github.com/basecamp/kamal/issues/980 is resolved
echo "Making docker command available to user '${USER}'..."
kamal server exec 'sudo usermod -a -G docker ${USER}' -d ${KAMAL_DESTINATION} -h ${KAMAL_HOSTS} 2>/dev/null
echo "Docker set up on $KAMAL_HOSTS..."

On a fresh host, the first run of kamal setup -d destination fails once with a permission error:

ERROR (SSHKit::Command::Failed): Exception while executing on host X.X.X.X: docker exit status: 1
docker stdout: Nothing written
docker stderr: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.49/images/create?fromImage=docker.io%2Fxxxxxxxx%2Fyyyyyyyyyy&tag=zzzzzzzzzzzzzzz": dial unix /var/run/docker.sock: connect: permission denied

However, re-running kamal setup -d destination immediately afterwards succeeds. This makes the workaround usable in the interim, but it would be ideal if Kamal handled adding the non-root SSH user to the docker group during setup.

willzoltan avatar Sep 03 '25 09:09 willzoltan