How to choose base image Please refer to Docker base image tag meaning and selection .
My advice:
If the project already has a base image, I recommend keeping to use it.
If the project is a new project, I recommend to consider the alpine
base image. It’s can make the images extremely small.
If you are using compiled languages, like Golang, just use alpine
, It uses the least number of dependencies(Some images even use scratch
as their base. This images is for the tool used by every one, so it’s need to control the size)
If you are using Python, just use stretch
and jessie
, not need to use slim
. Sometimes, you may even need to install some dependencies using gcc. The size of the whole images will inevitably expand
Best practices for writing Dockerfile Please refer to Best practices for writing Dockerfiles .
A Docker image consists of read-only layers each of which represents a Dockerfile instruction.
If your build contains several layers, you can order them from the less frequently changed (to ensure the build cache is reusable) to the more frequently changed
Build cache 1 2 3 4 5 6 FROM python:3.7 -alpineCOPY requirements.txt requirements.txt RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ COPY main.py main.py ENTRYPOINT ["python" , "main.py" ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import tornado.ioloopimport tornado.webclass MainHandler (tornado.web.RequestHandler): def get (self ): self .write("Hello, world" ) def make_app (): return tornado.web.Application([ (r"/" , MainHandler), ]) if __name__ == "__main__" : app = make_app() app.listen(8888 ) tornado.ioloop.IOLoop.current().start()
first time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Sending build context to Docker daemon 4.096kB Step 1/5 : FROM python:3.7-alpine ---> b11d2a09763f Step 2/5 : COPY requirements.txt requirements.txt ---> 2936fbbc42a0 Step 3/5 : RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ---> Running in ac062d0f6a21 Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Collecting tornado==6.0.3 Downloading https://mirrors.aliyun.com/pypi/packages/30/78/2d2823598496127b21423baffaa186b668f73cd91887fcef78b6eade136b/tornado-6.0.3.tar.gz (482kB) Building wheels for collected packages: tornado Building wheel for tornado (setup.py): started Building wheel for tornado (setup.py): finished with status 'done' Created wheel for tornado: filename=tornado-6.0.3-cp37-cp37m-linux_x86_64.whl size=410711 sha256=80949481cc31a3ea333993961965fb109ad338102d14e8dd7715c55e31714cbc Stored in directory: /root/.cache/pip/wheels/df/a7/02/7a4c8f28d662723d5f5cc490138d18000b059e197ada8851b3 Successfully built tornado Installing collected packages: tornado Successfully installed tornado-6.0.3 Removing intermediate container ac062d0f6a21 ---> 21428e5cf880 Step 4/5 : COPY main.py main.py ---> 400b8bb0856f Step 5/5 : ENTRYPOINT ["python" , "main.py" ] ---> Running in f7bb65ee7c0d Removing intermediate container f7bb65ee7c0d ---> 04441e7b5051 Successfully built 04441e7b5051 Successfully tagged test :latest
What if we change the main.py, and build again
The steps before Step 4/5
display Using cache
, it means we reuse the layers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Sending build context to Docker daemon 4.096kB Step 1/5 : FROM python:3.7-alpine ---> b11d2a09763f Step 2/5 : COPY requirements.txt requirements.txt ---> Using cache ---> 2936fbbc42a0 Step 3/5 : RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ---> Using cache ---> 21428e5cf880 Step 4/5 : COPY main.py main.py ---> 2e9bc4b4f879 Step 5/5 : ENTRYPOINT ["python" , "main.py" ] ---> Running in 40991f958270 Removing intermediate container 40991f958270 ---> a38a8b6cb396 Successfully built a38a8b6cb396 Successfully tagged test :latest