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 the 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-alpineLABEL maintainer="laprashant@gmail.com"RUN apt-get update -y && \-->we don't need this as Python image already include it
apt-get install -y python-pip python-devCOPY ./requirements.txt /app/requirements.txtWORKDIR /appRUN pip install -r requirements.txtCOPY . /appENTRYPOINT [ "python" ]CMD [ "app.py" ]
To read the complete blog
Please follow me with my Journey
- Website:http://100daysofdevops.com/
- Twitter:@100daysofdevops OR @lakhera2015
- Facebook:https://www.facebook.com/groups/795382630808645/
- Medium:https://medium.com/@devopslearning
- GitHub:https://github.com/100daysofdevops/21_Days_of_Docker
This time to make learning more interactive, I am adding
- Slack
- Meetup
Please feel free to join this group.
Slack:
Meetup Group
If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/