Docker Tip #19: How to Ignore Files from Your Docker Images
Here's how to ignore certain files and folders from your Docker images. This lets you copy in everything you want, while ignoring the cruft.
In most cases, you’ll be copying in the source code of your application into a
Docker image. Typically you would do that by adding COPY src/ dest/
or
similar to your Dockerfile
.
That’s a great way to do it, but that’s also going to include things like your
.git/
directory or /tmp
folders that belong to your project. That’s going
to severely bloat your Docker images.
Lucky for us, Docker makes it very simple to exclude files and folders of your
choice. All you have to do is create a .dockerignore
file alongside your Dockerfile
.
At this point it’s pretty similar to what a .gitignore
file does for your
git repos. You just need to tell it what you want to ignore.
For example, to ignore git directories and the .dockerignore
file itself you
you could make a .dockerignore
file and then add these lines:
.git
.dockerignore
It’s also a good idea to remove temporary files specific to your programming language, framework or code editor of choice.