question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve the docker image.

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
iMericacommented, Mar 25, 2018

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

0reactions
webynetercommented, Mar 25, 2018

@iMerica that’s a dramatic improvement, would you mind sending a PR?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found