Experience of Dockerfile
After using docker several months, here are some tips I want to share with you
Its usage and syntax is similar with
.gitignore, so we can igore those unneeded files. It can speed up the building time and compact the image
Docker Compose to run multi docker container instead of one. It's hard to handle logs from different apps and has a long building time and large image
every cmd will create a new layer in image and it will be cached and reused. If cmd in Dockerfile has changed, its cache will unusable and other layers relied on it as well. So we need to combine the cmd with same changing frequency
latest tag in based image
latest will point to a different image when updating
delete extra file after
some cmd create cache file under /var and we do not need it when building image
choose suitable based image(alpine)
alpine is a distrubution for docker, its package manament tool is
apk, which has
--virtual option contributed to reducing image size.
WORKDIR set the default dir, which is the place for
CMD set the default running cmd when creating container.
It's optional, because it make things more complicated. It's just a script and default running execution.
exec in entrypoint script
exec we cannot stop container because
SIGTERM will be ignored by bash script
COPY other than
ADD is more complicated, which can download remote file and decompress file.
adjust the order for
put cmd caused least change in the header of Dockerfile, which can utilise the cache.
set default env, mapping port and volume
good way to run container which need some tweak or configuration
LABEL to set meta-data
such as creator or a brief desc
periodically check the health of container for monitor.
Posted on Tue 20 November 2018 by Chong Yang | tags: