Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize Dockerfile for caching #376

Merged

Conversation

Projects
None yet
2 participants
@elohmeier
Copy link
Contributor

elohmeier commented Jul 5, 2018

I've reordered the Dockerfile a bit. This helps users who change stuff in the Django-app and want to rebuild it using docker-compose up -d --build.
Docker is then noticing that e.g. the requirements.txt didn't change and that no package reinstallation is necessary. It'll quickly jump to the step where the application files are added, speeding up the development process for Docker users.

See the screenshot of a cached build for clarification:
image

@danielquinn

This comment has been minimized.

Copy link
Collaborator

danielquinn commented Jul 8, 2018

Neat trick! Honestly, I look at your changes and I don't understand how it works, but I've just tested this and sure enough, the build process is drastically reduced. I'll merge this now, but would you mind further explaining just what's going on here? I was under the impression that ENTRYPOINT and CMD had to be at the bottom of the file, so moving the COPY lines below them seems counterintuitive.

@danielquinn danielquinn merged commit f99db14 into the-paperless-project:master Jul 8, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls First build on dockerfile-caching at 61.92%
Details
@elohmeier

This comment has been minimized.

Copy link
Contributor Author

elohmeier commented Jul 8, 2018

@danielquinn
To explain what's going on: When building, Docker will keep track of the files added with each build step in it's cache. It's storing checksums for each file to skip build steps when they probably don't need to be repeated. E.g. if you would add a python package to the requirements.txt, Docker would notice the invalidated cache because of that checksum and start building again beginning from step 3 (COPY requirements.txt /usr/src/paperless/). Checkout Dockers best practices section for further details.

A side effect of this is, that only your local files are considered. E.g. Docker would not notice a newly uploaded package to PyPI which would be accepted by an cached requirements.txt. (you can use docker build --no-cache=true to force rebuild)

ENTRYPOINT or CMD can be placed anywhere in the Dockerfile, the last occurrence wins for each of them. Since it takes around 2 seconds on my machine to process each of these instructions, I've put them a bit up, so we can profit from another speedup there.

@danielquinn

This comment has been minimized.

Copy link
Collaborator

danielquinn commented Jul 8, 2018

Cool, thanks for the tutorial :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.