The .dockerignore file is used when we build a Docker image using the `docker build` command. This file tells Docker to ignore certain files and directories when sending the build context (your application’s code, dependencies) to the Docker daemon, similar to how a .gitignore file works.
Here is how you can use a .dockerignore file:
1. Create a file named `.dockerignore` in the root directory of your application (the directory where your Dockerfile is located).
1. Inside .dockerignore file, add the names or patterns of files and directories you want to exclude from the build context. For example:
```
In the example above, node\_modules, dist, .env file, and any files with .log extension won’t be sent to the Docker daemon when you run `docker build`.
Some common uses include ignoring:
- Building artifacts
- Configuration files that should not be included in the image
- Local environment files
- Git and other version control directories
- Dependency directories (like `node_modules` in a Node.js project)
1. After you have listed the files or directories, save and close the `.dockerignore file`.
1. Now, when you run the docker build command, the specified files and directories, and their respective contents will be ignored.
Remember, the use of .dockerignore file can greatly speed up the build process, especially when you have a large context. By excluding unnecessary files, you can trim down your build context to just what is needed.