21 Days of Docker-Day 17 -Building Effective Docker Image

Welcome to Day 17 of 21 Days of Docker. The topic for today is the Building Effective Docker Image.

What are containers layers?

On Day 8, I talked about Docker Images and layers.

Image is build of a bunch of image layers and when we boot the container using this image, container adds a read-write layer on the top of it.

Now at this stage, the question you need to ask yourself, why do I care how many layers I have?

  • More layers mean a larger image. The larger the image, the longer it takes to both build, push and pull from the registry.
  • Smaller images mean faster builds and deploys.

How can I reduce my layers?

  • Use shared base images where possible.
  • Limit the data written to the container layer.
  • Chain RUN statements
  • Prevent cache misses at build for as long as possible.

Other steps we can do, to build an optimized image

Choosing the right base image

centos                                                                                  latest              9f38484d220f        7 months ago        202MB

vs

python                                                                                  3.7.3-alpine        2caaa0e9feab        3 months ago        87.2MB
  • In this case, if I am building a Python application, I don’t need entire centos operating, I just need a minimal base OS(which is provided by alpine), with all the Python binaries installed on the top of it.
FROM ubuntu:latest  --> FROM python:3.7.3-alpine

LABEL maintainer="[email protected]"

RUN apt-get update -y && \-->we don't need this as Python image already include it
    apt-get install -y python-pip python-dev

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

NOTE: There might be cases where you need a full base OS

  • Security
  • Compliance
  • Ease of development

Use of Scratch

  • It’s a special empty Dockerfile.
  • Use this to build your own base images.
  • OR build a minimal image that run a binary and nothing else.
FROM scratch
COPY hello /
CMD ["/hello"]

More info: https://docs.docker.com/develop/develop-images/baseimages/

Use Multi-stage Build as I mentioned on Day7

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.

Slack: 

https://100daysofdevops.slack.com/join/shared_invite/enQtNzg1MjUzMzQzMzgxLWM4Yjk0ZWJiMjY4ZWE3ODBjZjgyYTllZmUxNzFkNTgxZjQ4NDlmZjkzODAwNDczOTYwOTM2MzlhZDNkM2FkMDA

Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/