Minimizing your Docker images
As your project grows it can be challenging to make sure that only the necessary files get sent to the Docker daemon.
There are several tips to help you with this.
Use a „build“ directory
.dockerignore file to ignore all files that should not be sent to the docker daemon sounds easy but in large projects this can be quite challenging task, especially if your build tasks get more and grow. I often found it easier to create a single „build“ director during the build process (like grunt, gulp or similar), build and prepare everything into this directory and just use this „build“ directory as the root directory to build the Docker image.
Optimize your .dockerignore file by negation
Sometimes it is not possible to use such a dedicated „build“ directory. In that case you can use a
.dockerignore file to specify all files that should not be sent to the docker daemon and thus be ignored during the docker build step. In this
.dockerignore file you can list files and directories that should be ignored. But it is very easy to forget to add new files as your project is evolving.
I think it is better to explicitly specify which files should be sent to the docker daemon. You can do so, by adding a negated entry to the
.dockerignore for each file or directory that should be included.
It might sound strange to add a negative entry into an ignore-file but it works pretty well.
# ignore everything * # do NOT ignore docker-entrypoint.sh !docker-entrypoint.sh # do NOT ignore everything in the build !build/ # but DO ignore the nodemon.json file in the build directory build/nodemon.json
Check the amount of data you send to the Docker daemon
You can use a special Dockerfile to check which files and directories are sent to the Docker daemon. Create a file
Dockerfile-checksize in the directory of your
Dockerfile and use the following content:
FROM busybox RUN mkdir -p copyitems/ WORKDIR copyitems COPY * ./ RUN ls -als RUN du -h RUN du –sh
Use the following command to build the Docker image:
docker build . -f Dockerfile-checksize --no-cache
During the build process you will see which files are sent to the Docker daemon and the size of each directory:
Sending build context to Docker daemon 20.35 MB Step 1/7 : FROM busybox Step 2/7 : RUN mkdir -p copyitems/ Step 3/7 : WORKDIR copyitems Step 4/7 : COPY * ./ Step 5/7 : RUN ls -als total 1140 4 drwxr-xr-x 5 root root 4096 Feb 5 19:01 . 4 drwxr-xr-x 20 root root 4096 Feb 5 19:02 .. 4 -rw-rw-r-- 1 root root 49 Oct 1 12:46 .bowerrc 4 -rwxrwxr-x 1 root root 342 Feb 5 11:28 backupdb.sh 4 -rwxrwxr-x 1 root root 966 Oct 16 12:25 createdb.sh 40 -rw-rw-r-- 1 root root 40549 Oct 1 12:52 database-schema.sql 4 drwxrwxr-x 3 root root 4096 Oct 1 12:47 docker 4 -rw-rw-r-- 1 root root 958 Oct 20 12:06 docker-entrypoint.sh 4 -rw-rw-r-- 1 root root 1723 Feb 5 11:28 package.json 4 drwxrwxr-x 4 root root 4096 Feb 5 11:58 src Step 6/7 : RUN du -h 8.0K ./docker/docker-entrypoint-initdb.d 16.0K ./docker 40.0K ./src/public/html/backend 20.0K ./src/public/html/print 88.0K ./src/public/html/general 360.0K ./src/public/html 1.7M ./src/public/js/lib 2.6M ./src/public/js 148.0K ./src/public/css 2.2M ./src/public/img 5.5M ./src/public 68.0K ./src/lib/common 36.0K ./src/lib/config 8.0K ./src/lib/models 924.0K ./src/lib 6.4M ./src 20.6M . Step 7/7 : RUN du -sh 20.6M . Successfully built aa603e730b31
(Some Docker log messages are omitted for clarity)
With the output of this build process you can check if you only added the necessary files or if may rework your .dockerignore file.
geschrieben von onwerk am 5. Februar 2017 um 19:30