'Rootless-mode Docker daemon not running after logging back in (SSH)

I have set up rootless mode Docker for a non-root user on an Ubuntu 18.04 LTS box, according to the official instructions. I set DOCKER_HOST to unix://$XDG_RUNTIME_DIR/docker.sock in the .profile for the user. This is actually a non-root user account used by myself to remotely run containers over SSH. Previously I was doing this via the admin user with the system-wide docker daemon and sudo, and everything worked fine. But on this non-root user running rootless docker whilst I can start a container running in the background (-d flag), when I log back in the container has stopped.

What is going on? I have started the docker daemon using systemctl from within the user account (the system-wide docker daemon is disabled), and since I'm running the containers in the background I imagined it would be possible to log out and log back in and the containers would still be running. But this does not seem to be the case.

Am I missing a step? The docker page on rootless containers states that to launch the daemon on system startup it is necessary to enable the systemd service and lingering, with:

systemctl --user enable docker
sudo loginctl enable-linger $(whoami)

This confuses me - this user has no sudo privileges, and I don't want to grant it any, so I guess I need to run the first line from inside that user account and the second from inside my admin account, right? So if the username is jim, for the second command I guess I would execute:

sudo loginctl enable-linger jim


Solution 1:[1]

For anyone else who needs a set-up like this, you will need to enable docker inside the non-sudo account like this:

systemctl --user enable docker

Then (assuming that account's username is jim), from an account with sudo privileges run the following command:

sudo loginctl enable-linger jim

With this I was successfully able to run docker (detached) from a non-sudo account, logout, and the job was still running when I logged back in.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChrisM