Home > Mobile >  Keep docker container running in case of error
Keep docker container running in case of error

Time:09-01

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?

CodePudding user response:

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.

Alternatively, you can use

CodePudding user response:

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
  • Related