strtCoding
strtCoding

Reputation: 391

Docker containers terminate on shell logout

I'm running a debian backed docker container created with a docker-compose up -d detached command. It starts without problems and serves requests without issues, but it requires I keep logged-in with an active shell in order to keep containers running.

My docker-compose.yml file is as follows:

services:
  db:
    image: postgres:alpine
    restart: always
    ports:
      - '5432:5432'    
    volumes:
      - /data/docker/postgresql/data:/var/lib/postgresql/data:Z
  pgadmin:
    image: dpage/pgadmin4:latest
    restart: always
    ports:
      - "5050:80"
    volumes:
      - /data/docker/postgresql/pgadmin-data:/var/lib/pgadmin:Z

Everytime I restart the server it doesn't appears to start up until I log in.

The status of docker service, as per systemctl status docker.service is:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-08-09 22:37:46 -03; 21min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 4359 (dockerd)
      Tasks: 11
        CPU: 1.396s
     CGroup: /system.slice/docker.service
             └─4359 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Even when all looks good, the moment I logout the shell session, the containers stop running.

Upvotes: 8

Views: 3844

Answers (1)

strtCoding
strtCoding

Reputation: 391

The problem I was having can be searched for:

Rootless containers exit once the user session exits

I finally could fix it after realizing i was using the rootless configuration for docker, and both containers where started using a normal user.

Linux stops processes started by a normal user if loginctl is configured to not use lingering, to prevent normal users to keep long-running processes executing in the system.

I've found this link that helped: 17) rootless containers exit once the user session exits

You need to set lingering mode through loginctl to prevent user processes to be killed once the user session completed.

Symptom

Once the user logs out all the containers exit.

Solution

You'll need to either:

# loginctl enable-linger $UID

Upvotes: 14

Related Questions