Ignoring files per environment with Docker

And why you might want to do this

Joel Clermont
Joel Clermont
2024-02-27

We really like using Docker for development and CI, but there are additional considerations when using Docker in production. One of those is determining which files you want to end up in the built image.

You can specify individual paths and files to copy in the Dockerfile, but often it's more convenient to specify a top-level directory to copy and then use a dockerignore file to selectively exlcude files and folders from the built image.

By default, the .dockerignore file sits in the root of the folder where you're building the image, but you can go a step further and have a .dockerignore file per environment.

For example, if you're building a production image based on prod.Dockerfile, Docker will look for that root .dockerignore file, but it will also look for prod.Dockerfile.dockerignore in the same directory.

Very handy! Now I can globally ignore files from all environments, but then strip down the production image to have even fewer files than our staging image includes.

Here to help,

Joel

P.S. Want some help getting Docker setup with your Laravel app? Let's pair on it!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.