Rails server is still running in a new opened docker container

前端 未结 9 1333
面向向阳花
面向向阳花 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:08

    I was stumped by the same problem for a bit until I figured out what was really going on. The real answer is further down... At first, you may want to try something like the following command in your docker-compose.yml file:

    command: /bin/sh -c "rm -f /rails/tmp/pids/server.pid && rails server puma"
    

    (I'm using Alpine and busybox to keep things tiny, so no bash! But the -c will work with bash too) This will delete the file if it exists so that you don't get stuck with the problem that the container keeps exiting and sitting there unable to run commands.

    Unfortunately, that is NOT a good solution because you add an extra layer of /bin/sh ahead of the server and that prevents the server from getting the stop command. The result is that the docker stop commands won't give a graceful exit and the problem will always happen.

    You could just run the rm command using docker-compose up to remove the file and then change the command back to the server and carry on.

    However, the real answer is the create a simple docker-entry.sh file and make sure you call it using the exec form Entrypoint Documentation so that signals (like stop) get to the server process.

    #!/bin/sh
    set -e
    
    if [ -f tmp/pids/server.pid ]; then
      rm tmp/pids/server.pid
    fi
    
    exec bundle exec "$@"
    

    NOTE: we use exec on the last line to make sure that rails will be run as pid 1 (i.e. no extra shell) and thus get the signals to stop. And then in the Dockerfile (or the compose.yml) file add the entrypoint and command

    # Get stuff running
    ENTRYPOINT ["docker-entrypoint.sh"]
    CMD ["rails", "server", "puma"]
    

    And again, you MUST use the [] format so that it is exec'd instead of run as sh -c ""

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

    for my work:

    docker-compose run web bash
    

    and then go folder by folder with the command cd (i am using win 7 toolbox) so in the end i use in the bash :

    rm tmp/pids/server.pid

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

    This is an adapted version of Brendon Whateleys

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

    If your solution is using rm to delete the pid file on an entry point, I think a better solution is to tell rails to write the pid file to an ephemeral path, like:

    web:
       command: bundle exec rails server -b '0.0.0.0' -p 3000 -P /tmp/rails.pid 
    
    0 讨论(0)
  • 2021-01-31 08:20

    What I have done, Is to go on the bash shell of docker:

    docker-compose run web /bin/bash

    then remove the following file

    rm tmp/pids/server.pid

    Hope that it help!

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

    You are using an onbuild image, so your working direcotry is mounted in the container image. This is very good for developing, since your app is updated in realtime when you edit your code, and your host system gets updated for example when you run a migration.

    This also means that your host system tmp directory will be written with the pid file every time a server is running and will remain there if the server is not shut down correctly.

    Just run this command from your host system:

    sudo rm tmp/pids/server.pid 
    

    This can be a real pain when you are for example using foreman under docker-compose, since just pressing ctrl+c will not remove the pid file.

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