Q:

Docker: Do I need to rebuild the container from scratch whenever I need to add software?

So I'm experimenting with Docker, and I set up a Rails App talking to postgres. Everything works, I'm able to access the app, migrate the database, etc.
Dockerfile
FROM ruby:2.2.1

RUN apt-get update -qq && apt-get install -y build-essential
RUN apt-get install -y libxml2-dev libxslt1-dev
RUN apt-get install -y libqt4-webkit libqt4-dev xvfb
RUN apt-get install -y nodejs

ENV APP_HOME /code
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ADD . $APP_HOME
RUN bundle install

docker-compose.yml
web:
  build: .
  command: bin/rails server --port 3000 --binding 0.0.0.0
  ports:
   - "3000:3000"
  volumes:
   - .:/code
  links:
   - db
db:
  image: postgres
  ports:
    - "5432:5432"

After making a couple models and a controller, I decided to use Slim templates for the project and added it to my Gemfile
gem 'slim'

I would have expected to be able to update this dependency on the web container by running bundle install on it:
docker-compose run web bundle install

Which appears to work at first, it installs the gem and it's dependencies. However these are just on a throw-away copy of the built container. IT doesn't stick around when I run docker-compose up the next time.
The only way to get the gem in permanently is to build web from scratch again, which can take 3-4 minutes to install all the gems from scratch again, mostly due to nokigiri.
Is this the only way to "update" a base image? I would expect I would only need to rebuild if I'm modifying the Dockerfile itself, when it would make sense to rebuild from scratch.

A:

If you want, you can also update an existing image by "committing" the changes you've made to a container. The syntax is docker commit <container ID> <image>.
Alternatively you can take advantage of the build cache, by separating your bundle install command into individual gem installs. Then you can add your new gem to the end and it'll reuse all the previous layers when you rebuild the image. This may not be ideal considering that there may be overlapping dependencies.

