As mentioned in a previous blog post, my current project is deployed using Docker. However, during a recent login to the server, a strange phenomenon occurred: all the containers in Docker would restart from time to time. Also, every time I logged in to check the Docker status, it would show that the containers had just started (Up 15 seconds). After several investigations, I finally identified that the restart issue was related to SSH login. Every time I logged in via SSH, the containers would restart.
My server is an Oracle ARM instance running Ubuntu 22.04. After searching online, I found that the restart issue was related to the rootless mode of Docker. When containers are started in user mode, they terminate after the user session is logged out. This is controlled by the system component loginctl.
The solution to this issue is quite simple: set the user who starts Docker to enable linger. The specific method to enable it is as follows:
loginctl enable-linger $UID
By enabling linger for the Docker user, the user’s session will not end immediately after logging out, allowing the containers to continue running. This will prevent the containers from restarting every time the user logs in via SSH.
In conclusion, if you encounter a similar issue where Docker containers keep restarting after SSH login, try enabling linger for the Docker user using the above method. It is a quick and easy solution that can save you a lot of time and trouble.
This post polished by ChatGPT, with images generated by Stable Diffusion.
Reference:
- https://devops.stackexchange.com/questions/16949/docker-containers-are-being-restarted-after-logging-in-via-ssh?newreg=52235d2d8d214e7ca8349ffb95e1256f
- https://github.com/containers/podman/blob/main/troubleshooting.md#17-rootless-containers-exit-once-the-user-session-exits
- https://docs.docker.com/engine/security/rootless/