Rails server is still running in a new opened docker container

前端 未结 9 1334
面向向阳花
面向向阳花 2021-01-31 08:05

I want to deploy my rails project using Docker. So I use Docker-Compose. But I get one weird error message. When run docker-compose up(this contains db-container with postgresql

相关标签:
9条回答
  • 2021-01-31 08:23

    For ones, who have problem when there is no tmp/pids/server.pid actually exists:

    (Docker version 1.11.2, build b9f10c9)

    1. Solution, which worked for me is to add exact command to docker-compose.yml, for example:

      command:
         rails s -b0
      

      Having this command, which in fact may simply duplicate Dockerfile's CMD - issue with stale .pid do not appear.

    2. Other option, when you need to use Dockerfile's CMD, is to run with option --build to rebuild image:

      docker-compose up --build
      

      Though it takes time, so first solution is more convenient

    0 讨论(0)
  • 2021-01-31 08:24

    If you are feeling adventurous feel free to search for server.pid in the codebase and check where the pid is getting written into. That way you can check if not allowing it to write fixes the root problem for you. Because seemingly the pid creation helps prevent you from running duplicate services in two different docker containers. But that should be taken care of in docker as it is. If for some reason, it does not, the port collision methodology should definitely take care of that and alert you. Note: I have not tried this approach as yet as that is moving the system in general:)

    Alternatively, you could have commands in your Makefile. Where the task does this:

    task:
    docker-compose stop service_name
    docker-compose rm service_name
    rm -f ./tmp/pids/server.pid (or relative pathname of your pid file)
    docker-compose up service_name

    And then vrooom make task in terminal and hit enter. The above task commands assume the service you want to run are in a file named docker-compose.yml

    If the name is different then the command

    docker-compose stop service_name --> docker-compose -f your-compose-filename.yml stop service_name

    so on and so forth.

    ==================== Came back to update this. So, I did try commenting out my write_pid method functionality. Basically keep the method definition, but not have it do anything. It is working well so far. It does not write the pid at all. If you are running your services in docker, I believe this is completely safe.

    0 讨论(0)
  • 2021-01-31 08:34

    In your docker-compose.yml file, under the application container itself, you can either use:

    command: ["rm /your-app-path/tmp/pids/server.pid && bundle exec bin/rails s -p 3000 -b '0.0.0.0'"]

    or

    command: ["rm /your-app-path/tmp/pids/server.pid; foreman start"]

    the reason for either ";" or "&&" is that the latter will send an exit signal if rm fails to find the file, forcing your container to prematurely stop. the prior will continue to execute.

    why is this caused in the first place? the rational is that if the server (puma/thin/whatever) does not cleanly exit, it will leave a pid in the host machine causing an exit error.

    0 讨论(0)
提交回复
热议问题