Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active October 4, 2023 17:03
Show Gist options
  • Save AlexAtkinson/96aea872b0af1ee367b9f24055421eb0 to your computer and use it in GitHub Desktop.
Save AlexAtkinson/96aea872b0af1ee367b9f24055421eb0 to your computer and use it in GitHub Desktop.
Docker Build - .dockerignore Interactions
#!/usr/bin/env bash
dir=$(mktemp -d)
cd $dir > /dev/null
export DOCKER_BUILDKIT=1
echo -e "FROM node:20-alpine\nCOPY . tmp/" > Dockerfile_foo
cp Dockerfile_foo Dockerfile_bar
touch foo bar
echo "foo" > .dockerignore
echo "bar" > Dockerfile_bar.dockerignore
docker build -t foo -f Dockerfile_foo .
docker build -t bar -f Dockerfile_bar .
echo -- foo --
docker run foo ls tmp | grep '^foo\|^bar'
echo -- bar --
docker run bar ls tmp | grep '^foo\|^bar'
echo -e "\nTEST: Observe how docker build handles .dockerignore files with BUILDKIT."
echo -e "\nRESULT: Docker build uses the <Dockerfile>.dockerignore and ignores .dockerignore."
echo -e "\nWARN: Disabling BUILDKIT results in the <Dockerfile>.dockerignore file being ignored."
cd - > /dev/null
rm -rf $dir
@AlexAtkinson
Copy link
Author

See this comment for original.

BUILDKIT or not to BUILDKIT... Just be cautious around .dockerignore dependencies and how they're handled both in the CI and the developer workflows.

ADVICE: Just keep a bunch of dockerignore configs and copy them to .dockerignore as needed. EG: cp .resources/dockerignore_template_prod .dockerignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment