Exit code 1 occurs when Rails is stopped in Docker environment

problem

If you try to stop the container with docker stop while Rails (Puma) is running on Docker, you will get Exit 1 (SIGHUP). If you are using ECS or Kubernetes, the container may not terminate properly and may cause unexpected problems.

% docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                      PORTS               NAMES
xxx        rails                      "bundle exec rails s…"   44 seconds ago      Exited (1) 3 seconds ago                        api
xxx

Cause

The cause is the so-called "PID 1 problem [^ 1]". PID 1 is called the init process, which is a special process called by the kernel at system startup. The init process handles signals, spawns child processes, deletes zombie processes, and more. This time, when Rails ([^ 2]) was started, PID 1 was used, and there was a problem that the signal could not be handled correctly.

#The result of executing the top command on the container
  PID USER      PR  NI    VIRT    RES  %CPU  %MEM     TIME+ S COMMAND
    1 root      20   0    2.2m   1.5m   0.0   0.1   0:00.02 S /bin/bash /bin/docker-entrypoint.sh bundle exec rails s -b 0.0.0.

If you look at the process, you can see that Rails is running with PID 1.

Countermeasures

As a child process of PID 1 by using a program such as tini or [dumb-init](https://github.com/ Yelp/dumb-init) It will be possible to start the application. If docker-compose 3.7 or later is available, you can work around the problem by adding the init parameter to docker-compose.yml ([^ 3]).

#Rails starts as a child process of the init process
PID USER      PR  NI    VIRT    RES  %CPU  %MEM     TIME+ S COMMAND
1 root      20   0    1.0m   0.0m   0.0   0.0   0:00.03 S /sbin/docker-init -- /bin/docker-entrypoint.sh bundle exec rails s -b 0.0.0.0
6 root      20   0    2.2m   1.5m   0.0   0.1   0:00.00 S  `- /bin/bash /bin/docker-entrypoint.sh bundle exec rails s -b 0.0.0.0

Looking at the process after running docker-compose down, the container is stopped at 143 (SIGTERM).

% docker-compose ps
          Name                         Command                State     Ports
-----------------------------------------------------------------------------
api                 /bin/docker-entrypoint.sh  ...   Exit 143

If you are using ECS

Add initProcessEnabled: true ([^ 4]) to the task definition. initProcessEnabled is equivalent to --init in docker run.

If you are using Kubernetes

You can also use a lightweight init such as tini, but starting with Kubernetes 1.17 it seems that you can work around the problem by using Share Process Namespace ([^ 5]).

Recommended Posts

Exit code 1 occurs when Rails is stopped in Docker environment
Show Better Errors in Rails + Docker environment
(Basic authentication) environment variables in rails and Docker
[Rails] Solution when migration error occurs in acts-as-taggable-on
500 Internal Server Error occurs in Rails production environment
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
Rails Docker environment construction
Migration error after Activerecord association in Rails5 + Docker environment (2)
Migration error after Activerecord association in Rails5 + Docker environment
Regarding overcapacity when setting Rails tutorial environment in Cloud 9
SSL in the local environment of Docker / Rails / puma
When there is no output to stdout in docker log
exited with code 1 error resolution with docker-compose up in rails environment
[Docker] Rails 5.2 environment construction with docker
Docker does not work when DOCKER_HOST is specified in WSL2
An error occurs when codedeploy-agent is installed in Ubuntu Server 20.04
Install lsb_release from the command line when lsb_release fails in docker environment
When log data accumulates in Rails and the environment stops working
How to write the view when Vue is introduced in Rails?
Rails + MySQL environment construction with Docker
Java Spring environment in vs Code
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Install laravel/Dusk in docker environment (laravel6)
[Environment construction with Docker] Rails 6 & MySQL 8
Data is not registered in Rails.
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Check MySQL logs in Docker environment
Puma --Nignx is an escape route when rails s -e production -d doesn't work in the environment
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails] About the error that the image is not displayed in the production environment
[Docker] How to build when the source code is bind-mounted on the container
[Rails 5] Countermeasures for git commit failure from another branch in Docker environment
What to do if tomcat process remains when tomcat is stopped in eclipse
Alert slack with alert manager in Docker environment
[Rails] Annotate is not executed when migrating
[Docker] Use environment variables in Nginx conf
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Use docker in proxy environment on ubuntu 20.04.1
Command when dependency is broken in Ubuntu 20.04
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Build Redmine code reading environment on Docker
Image is not displayed in production environment
[Rails] Reset the database in the production environment
How to build Rails 6 environment with Docker
[Error resolution] Occurs when trying to build an environment for spring with docker
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)
Solved DB connection and CORS problem when developing Laravel × Nuxt.js in Docker environment
How to set when "The constructor Empty () is not visible" occurs in junit
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec