skip to Main Content

I have following docker file

FROM ruby:latest

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

WORKDIR /usr/src/app/

COPY Gemfile Gemfile.lock ./
RUN bundle install

ADD . /usr/src/app/

EXPOSE 3333

CMD ["ruby", "/usr/src/app/helloworld.rb"]

When I run image

docker run -t -d hello-world-ruby

Sinatra throws exception (which is OK), and container exits.

How can I keep it running so I can ssh into container and debug what’s happening?

2

Answers


  1. A trick you can use is to start the application with a script.

    The following will work:

    FROM ruby:latest
    
    # throw errors if Gemfile has been modified since Gemfile.lock
    RUN bundle config --global frozen 1
    
    WORKDIR /usr/src/app/
    
    COPY Gemfile Gemfile.lock ./
    RUN bundle install
    
    ADD . /usr/src/app/
    
    EXPOSE 3333
    
    CMD [/usr/src/app/startup.sh]
    

    and in startup.sh, do:

    #!/bin/bash
    
    ruby /usr/src/app/helloworld.rb &
    
    sleep infinity
    
    # or if you're on a version of linux that doesn't support bash and sleep infinity
    
    while true; do sleep 86400; done
    

    chmod 755 and you should be set.

    Login or Signup to reply.
  2. For debugging puproposes it is not needed to keep the container with a failing command alive, with loops or otherwise.

    To debug such issues just spawn a new container from the image with entrypoint/command as bash.

    docker run -it --name helloruby hello-world-ruby bash
    

    OR

    docker run -it --name helloruby --entrypoint bash hello-world-ruby 
    

    This will give you shell inside the container where you can run/debug the ruby app

    ruby /usr/src/app/helloworld.rb
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search