首页 > 解决方案 > Docker-compose 入口点脚本使用出口 0 停止容器

问题描述

执行我的入口点脚本后,容器以 Exit 0 停止。在启动我们的网络服务器的 compose 文件中指定的命令被忽略。

我们使用 docker 和 docker-compose 作为 Rails 应用程序的环境。

入口点脚本:

#! /bin/bash
bundle exec rails assets:clobber
bundle exec rails assets:precompile

bundle exec rake db:exists && bundle exec rake db:migrate || bundle exec rake db:setup

rm -rf /aps/tmp/pids/server.pid

撰写文件:

version: '2'
services:
  app:
    image: registry.gitlab.com/.../.../master:latest
    command: bundle exec rails server
    entrypoint: /aps/rails-entrypoint.sh
    volumes:
      - /srv/app/log/:/app/log
      - /srv/app/public/:/app/public
    env_file: .env
    ports:
      - '0.0.0.0:3333:3000'
    links:
      - apppostgres

  apppostgres:
    image: postgres
    ...


volumes:
  pgdata:

当我在入口点脚本运行时连接到容器时,我可以看到执行的命令以ps auxas运行/bin/bash /app/rails-entrypoint.sh bundle exec rails server

当我将命令块添加到入口点脚本时,服务器已启动并正在运行,但这也不是它应该如何工作或者?

我该怎么做才能让入口点脚本和命令块正常运行?

标签: ruby-on-railsdockerdocker-composedocker-entrypoint

解决方案


entrypoint.sh当您启动容器时,将考虑启动容器的进程(在您的情况下) pid 1,只要该进程正在运行,您的容器就会保持启动并运行,并且如果它因任何原因死亡或停止,它将停止容器并退出status0或更高取决于主进程的实际退出状态。

您需要在入口点的末尾添加以下内容以使其与bundle exec rails server

exec "$@"

推荐阅读