Skip to main content

João Senger

Docker - Container stopping after closing SSH session

Table of Contents

Does your container stop when you end the SSH session with the server? Understand what might be happening.

# Introduction

## The Problem

Recently I had to deploy a very simple environment on AWS, it was an application with two containers that were run by Docker Compose. However, something strange was happening: when I ended the ssh session with the server, the containers immediately became unavailable.

# Investigating the problem

## Docker Logs

One of the first things I do to investigate an unexpected container stop is to analyze its logs, this can be easily analyzed through the command:

docker logs container_name

When analyzing the logs, I noticed the following error: [Tue Mar 18 19:13:05.229898 2025] [mpm_prefork:notice] [pid 1:tid 1] AH00169: caught SIGTERM, shutting down

At first glance, I realized that the action of disconnecting from the server was shutting down the containers, through a SIGTERM command, which is basically a kill command to stop a process.

## Looking for a solution

When searching the internet about the problem, I discovered that users who ran Docker as regular users and not as root faced this problem, which makes sense. When a user logs out, all their processes are terminated, meaning when closing the ssh connection all processes were terminated. Important: I was using the ubuntu user, which is created by EC2 when creating a VM with Ubuntu AMI. To solve the problem I would need to do something so that the processes wouldn’t be terminated, this would be my solution.

# Applying the solution

## Linger

Basically, this little guy makes the user’s processes continue running after logout.

# Makes processes continue running
sudo loginctl enable-linger $(whoami)

# Makes Docker start automatically for the current user
systemctl --user enable docker

After that, I paused the applications with the docker-compose down command and brought them up again with docker-compose up -d.

After that, problem solved. 😁