Improve the docker image.
See original GitHub issueThe docker image could be improved with a few tricks:
- Reduce the number of layers: Each instruction in the Dockerfile creates a new layer.
- Do not cache pip data: There is no point of caching the packages in a container,
--no-cache-dir
should be used - Remove clutter files
- Adding the code should be one of the last steps. Every step in the Dockerfile is cached so it has not to be executed everytime it changes. The code being the step that will change the most it must be one of the last steps.
Here’s an example of the optimized Dockerfile:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# Requirements have to be pulled and installed here, otherwise caching won't work
COPY ./requirements /requirements
COPY ./compose/django/gunicorn.sh ./compose/django/entrypoint.sh /
RUN pip install --no-cache-dir -r /requirements/production.txt \
&& groupadd -r django \
&& useradd -r -g django django \
&& sed -i 's/\r//' /entrypoint.sh \
&& sed -i 's/\r//' /gunicorn.sh \
&& chmod +x /entrypoint.sh \
&& chown django /entrypoint.sh \
&& chmod +x /gunicorn.sh \
&& chown django /gunicorn.sh \
&& rm -rf /requirements
COPY . /app
RUN chown -R django /app
USER django
WORKDIR /app
ENTRYPOINT ["/entrypoint.sh"]
If you would like those improvements I can create a pull request.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Tips for optimizing Docker builds - CircleCI
Optimizing the image build process · Ephemeral containers · Don't install unnecessary packages · Implement .dockerignore files · Sort multi-line ...
Read more >Docker development best practices - Docker Documentation
If you have multiple images with a lot in common, consider creating your own base image with the shared components, and basing your...
Read more >The Quickest Way to Improve Your Docker Images
The Quickest Way to Improve Your Docker Images. This might be the most frequent advice I give to folks: “Have you tried using...
Read more >Optimizing Docker Images - Linux Hint
Optimizing Docker Images · Select Proper Base Images · Use Multi-stage Builds · Reduce Number of Layers · Build Custom Base Images ·...
Read more >Three Easy Ways to Improve a Container's Performance
A Container Is Not a Virtual Machine · Chain Commands in the RUN Section of a Dockerfile · Use a Multi-stage Build ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to remove hundreds of megabytes from my Docker image by replacing:
RUN chown -R django /app
with
COPY --chown=django:django
on all COPY statements@iMerica that’s a dramatic improvement, would you mind sending a PR?