Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Identify which files are included in the Docker build context

Stanislav KhromovStanislav Khromov

If you have ever run docker build or docker-compose up just to have Docker stuck on “Building” for minutes? This might be because you are including too many files in your Docker build context.

Normally Docker will send along files that might be unnecessary for your build process such as node_modules, vendor or even the .git folder. This can be remedied using the .dockerignore file.

But what if you’ve added files to .dockerignore and your build is still slow? In that case you can use this excellent script from GitHub user sgdan that will show you all of the files you are sending to your build context:

#!/bin/sh

# Based on BMitch's answer from:
# https://stackoverflow.com/questions/38946683/how-to-test-dockerignore-file

# Note: will create and delete temporary file "Dockerfile.build-context"

# 1. Copy to project folder where image is being built
# 2. Run script
# 3. You should see list of files in build context
# 4. If unwanted files in context, adjust .dockerignore file and go back to step 2

cat <<EOF > Dockerfile.build-context
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .
EOF

docker build -f Dockerfile.build-context -t build-context .
docker run --rm -it build-context

rm Dockerfile.build-context

Create the file as dockerignore-test.sh, run chmod +x dockerignore-test.sh and then ./dockerignore-test.sh and you will see all the included context files, like this:

./dockerignore-test.sh
[+] Building 1.9s (8/8) FINISHED                                                                                                         
 => [internal] load build definition from Dockerfile.build-context 
 => => transferring dockerfile: 125B
 => [internal] load .dockerignore 
...
./startpage.sh
./config.rb
./.gitignore
./ssl_migrate_local.sh
./Gruntfile.js
./composer.json
./Gemfile.lock
./Dockerfile.build-context
./deploy.rb

More good reading on .dockerignore files.

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 0
There are currently no comments.