It is fairly common to build an application outside of Docker and inject the resulting binary. The most common example I see on SO is with Java-based applications. The Java class file format is designed to be portable across environments and so there aren't a lot of differences if a .jar file is built on a developer's workstation or not. You can run

mvn build
docker build -t myapp .
FROM tomcat:9
COPY target/myapp.war /usr/local/tomcat/apps

In this setup the target directory would be in .gitignore (you do not want build artifacts committed to source control) but it would not be in .dockerignore (it needs to be available to the image).

Some other patterns where this could be useful include:

  • In a compiled language in a developer environment with somewhat long build times, so that you can get a test image out without spending several minutes waiting for make in a multi-stage build
  • When an image needs to contain static assets that are hosted somewhere outside of source control, like Amazon S3
  • If there are data sets that get generated at build time, that are large enough to not want to be checked in but small enough that adding them to the image is still practical (5-500 MB perhaps)

(I mostly ignore .dockerignore but I also try to be explicit about what files I COPY into my images.)

Answer from David Maze on Stack Overflow
Top answer
1 of 3
15

It is fairly common to build an application outside of Docker and inject the resulting binary. The most common example I see on SO is with Java-based applications. The Java class file format is designed to be portable across environments and so there aren't a lot of differences if a .jar file is built on a developer's workstation or not. You can run

mvn build
docker build -t myapp .
FROM tomcat:9
COPY target/myapp.war /usr/local/tomcat/apps

In this setup the target directory would be in .gitignore (you do not want build artifacts committed to source control) but it would not be in .dockerignore (it needs to be available to the image).

Some other patterns where this could be useful include:

  • In a compiled language in a developer environment with somewhat long build times, so that you can get a test image out without spending several minutes waiting for make in a multi-stage build
  • When an image needs to contain static assets that are hosted somewhere outside of source control, like Amazon S3
  • If there are data sets that get generated at build time, that are large enough to not want to be checked in but small enough that adding them to the image is still practical (5-500 MB perhaps)

(I mostly ignore .dockerignore but I also try to be explicit about what files I COPY into my images.)

2 of 3
8

I do not think .dockerignore must be a superset of .gitignore. Docker ignore contains files which you want Docker build to ignore and in some cases it could be your source code as well. Take the example of a Java project that you are building with maven.

In this case when you are building the docker container you are probably only interested in the target folder and not any other folder. Whereas the .gitignore will have the target folder since you wont be checking in the compiled binaries (jar / war) to the source repo.

Similarly there could be other files that are generated / downloaded during your build which are required in the container but not in the source repo. So in a nutshell I don't think it is a good idea to enforce the superset rule, at least not in a generic all encompassing way.

Discussions

Can I ignore a file using .gitignore if the file is used in the build process?
I'm using a multi stage dockerfile and I wish to ignore dist directory using .gitignore. But even though I'm using path context, the build is not successful when I have dist in my .gitignor... More on github.com
🌐 github.com
4
October 15, 2021
Verify parity between .dockerignore and .gitignore syntax
related: docker/for-linux#585 #40319 moby/buildkit#4132 Description When building images, sometimes ** will be matched, other times it'll be ignored. Changing .dockerignore line, might influenc... More on github.com
🌐 github.com
7
December 18, 2019
.dockerignore should a support including ignored content
If the only thing you want in your dockerfile is the Gemfile of a project it is tedious to use .dockerignore to exclude everything manually. With .gitignore there is support for including things al... More on github.com
🌐 github.com
19
June 18, 2018
Any way to copy .gitignore contents to .dockerignore
This seems like overkill. There are more important things to keep DRY. More on reddit.com
🌐 r/docker
11
4
January 29, 2020
🌐
8th Light
8thlight.com › home › insights › ieffective use and debugging of .gcloudignore, .dockerignore, and .gitignore
IEffective Use and Debugging of .gcloudignore, .dockerignore, and…
September 16, 2024 - These three ignore files can be used together if, for example, you want to build a Docker container from an architecture different than the one on your local machine: .gitignore is a must, .dockerignore dictates which files go into the container, and (using Cloud Builds to build the container ...
🌐
Codefresh
codefresh.io › home › blog › do not ignore .dockerignore (it’s expensive and potentially dangerous)
Do not ignore .dockerignore (it's expensive and potentially dangerous) | Codefresh
March 13, 2025 - If you don’t manage the build ... to control the docker build context, we can see how this is done. The .dockerignore file is similar to gitignore file, used by the git tool....
🌐
GitHub
github.com › LinusU › gitignore-to-dockerignore
GitHub - LinusU/gitignore-to-dockerignore: Generate an equivalent `.dockerignore` file from an existing `.gitignore` file · GitHub
Generate an equivalent .dockerignore file from existing .gitignore files. New in 3.0, handles multiple .gitignore files in a directory and its subdirectories, just like Git does.
Starred by 39 users
Forked by 3 users
Languages   JavaScript
🌐
Zzz
zzz.buzz › 2018 › 05 › 23 › differences-of-rules-between-gitignore-and-dockerignore
Differences of Rules Between .gitignore and .dockerignore
May 23, 2018 - Git, Docker and many other programs can have an ignore file to exclude files from being considered by them. For Git, it's .gitignore, and it's .dockerignore for Docker.
🌐
Medium
medium.com › @bounouh.fedi › mastering-the-dockerignore-file-boosting-docker-build-efficiency-398719f4a0e1
Mastering the .dockerignore File: Boosting Docker Build Efficiency | by Fedi Bounouh | Medium
November 9, 2024 - The syntax of the .dockerignore file is simple. It works much like .gitignore: • Each line defines a file or directory to ignore. ... • You can use !
🌐
TechRepublic
techrepublic.com › home › what are .dockerignore files, and why you should use them?
What are .dockerignore files, and why you should use them? - TechRepublic
November 7, 2022 - The .dockerignore file is very similar to the .gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process. This can come in really handy in certain instances.
Find elsewhere
🌐
.dockerignore
dockerignore.com › dockerignore-vs-gitignore
.dockerignore vs .gitignore: Key Differences & When to Use Each
The .gitignore file tells Git which files to exclude from version control, while the .dockerignore file tells Docker which files to exclude when building images. Though similar in concept, they have different syntax rules, operate at different stages of development, and serve distinct purposes.
🌐
OneUptime
oneuptime.com › home › blog › how to use .dockerignore to speed up docker builds
How to Use .dockerignore to Speed Up Docker Builds
January 16, 2026 - ... If you see a large number here, you're sending unnecessary files. Create a .dockerignore file in your project root (same location as your Dockerfile). It uses the same syntax as .gitignore.
🌐
MarsBased
marsbased.com › home › blog › always use a .dockerignore in your projects
Always use a .dockerignore in your projects - MarsBased
January 9, 2026 - As you all know, .gitignore files ... other sensitive information that should never be committed. .dockerignore files serve a similar purpose, but for Docker builds....
🌐
Sachin Ghait Blog
onthegoalways.com › blog › ignore-files
Ignoring Files - A Guide to .gitignore, .dockerignore, and More
Improve collaboration: By keeping ... Similar to .gitignore, the .dockerignore file is used in Docker projects to specify files and directories that should be excluded when building Docker images....
🌐
Vokal
engineering.vokal.io › Systems › docker.md.html
Docker best practices
You can easily exclude unnecessary files and directories from a Docker image you are building by using a .dockerignore file. An example in the linked documentation points out excluding the hidden .git directory from your image. This is a good way to cut down the size of the resulting image.
🌐
OneUptime
oneuptime.com › home › blog › how to use docker ignore files
How to Use Docker Ignore Files
January 25, 2026 - # .gitignore - excludes from version control dist/ # Don't commit build output node_modules/ # Don't commit dependencies # .dockerignore - excludes from build context .git/ # Not needed in container *.md # Docs not needed in container tests/ # Tests not needed in production image
🌐
Medium
medium.com › hackernoon › do-not-ignore-dockerignore-47f5fb67b448
Do not ignore .dockerignore. Tip: Consider to define and use… | by Alexei Ledenev | HackerNoon.com | Medium
October 9, 2019 - The .dockerignore file is similar to gitignore file, used by git tool. similarly to .gitignore file, it allows you to specify a pattern for files and folders that should be ignored by the Docker client when generating a build context.
🌐
GitHub
github.com › docker › build-push-action › issues › 484
Can I ignore a file using .gitignore if the file is used in the build process? · Issue #484 · docker/build-push-action
October 15, 2021 - node_modules npm-debug.log .git .gitignore .github .husky .DS_Store .vscode .dockerignore Dockerfile *.md .env*
Author   mt-empty
🌐
GitHub
github.com › balena-io-modules › dockerignore › blob › master › README.md
dockerignore/README.md at master · balena-io-modules/dockerignore
With .gitignore, when a parent ... the subtree as an optimization. With .dockerignore, a subdirectory can be re-added even if a parent directory has been ignored....
Author   balena-io-modules
🌐
GitHub
github.com › moby › moby › issues › 40318
Verify parity between .dockerignore and .gitignore syntax · Issue #40318 · moby/moby
December 18, 2019 - Describe the results you expected: ** should always match any number of directories (including zero) as per documentation. After using the first .dockerignore contents, docker context should have selected files from nested-dir/
Author   sbienkow
🌐
GitHub
github.com › moby › moby › issues › 9171
.dockerignore should a support including ignored content · Issue #9171 · moby/moby
June 18, 2018 - If the only thing you want in your dockerfile is the Gemfile of a project it is tedious to use .dockerignore to exclude everything manually. With .gitignore there is support for including things allowing you to do: /* !/Gemfile !/Gemfile...
Author   maxandersen
🌐
GeeksforGeeks
geeksforgeeks.org › devops › how-to-use-a-dockerignore-file
How to Use a .dockerignore File? - GeeksforGeeks
April 8, 2024 - Now, similar to a .gitignore file that is commonly used when you build Git repositories, a .dockerignore file is used to ignore files and folders when you try to build a Docker Image.