Demystifying Docker Compose

Jenn on November 21, 2019

My latest project used Rails and Postgres. After fighting my Mac and the Postgres install for an hour, I gave up and used Docker instead. Docker h... [Read Full]
markdown guide

I hope this doesn't sound stupid but why didn't we just do

COPY . /my-api
RUN bundle install

instead of the longer version

COPY Gemfile /my-api/Gemfile
COPY Gemfile.lock /my-api/Gemfile.lock
Copying the Gemfiles to the working space in the container.

RUN bundle install
This will run bundler in the container.

COPY . /my-api
Copy all the other files into the working space.

Is there a huge difference between the two?

Great article btw!


Each line is cached, so if I only use COPY . /my-api I will have to run bundler every time anything in my code changes, which takes time and would be annoying.

By copying over the gemfiles first and running bundler those packages are cached and rebuilds will be faster. As docker-compose is smart enough to realize those files didn't change so it will skip the gemfile and bundler steps when building.


Ahh. That makes sense. Thanks for clearing things up for me 😁


You could instead use a volume in docker-compose

It wouldn't change the need to rebuild and rerun bundler if the gemfiles changed. I always prefer to rebuild the container when updating gemfiles. I don't want my docker-compose file to get out of sync with what happens in my container.


Because I'm picky, size matters with Docker images so you might want to clean up the apt cache after you're done installing those apps. Just add a line after the apt installs
rm -rf /var/lib/apt/lists/*

Also, I'm not familiar with Ruby but you may be able to do some cleanup too after the build command or even build the app in a throwaway container and use the artifact in this one. Check it out here. docs.docker.com/develop/develop-im...


May be a comment on would help further

$ docker-compose build db uses an image, skipping

That is the output from docker-compose build command when I reran it for this post.

It doesn't have to build anything for the db as it is pulling down the Postgres image (which it has cached). I have a comment about it in the docker-compose.yml section. I'll add a bit more info there.


Thanks for writing! I'm still yet to use docker myself. The whole thing is a bit of a mystery.

code of conduct - report abuse