Have you ever used the Docker ONBUILD instruction?

Here is how it works 👇 

Let’s say you want every project team to use standard commands and file paths in their Dockerfile.

One approach is to ask teams to follow documentation, but this often leads to inconsistencies.

A better way is to use the ONBUILD instruction to standardize tasks like dependency installation and file copying.

Here’s how it works:

- ONBUILD instructions are stored in the metadata of a base image.

- When a child image is built from this base image, Docker automatically executes the stored ONBUILD instructions in the defined order.

For example, instead of repeating the same steps in every Dockerfile, you can create a base image (e.g., flask-base:1.0) with ONBUILD instructions.

You can compare this to a startup script in a VM image, which runs automatically when the VM is started with that image.

Here’s a blog that explains the ONBUILD instruction with a practical example.

𝗗𝗲𝘁𝗮𝗶𝗹𝗲𝗱 𝗕𝗹𝗼𝗴:Have you ever used the ONBUILD instruction in your projects?

What was the use case?

Share your tips in the comments!

If you're looking for such guides and tips, 

Subscribe to the blog and join 8000+ other DevOps engineers receiving our tips straight to their inbox!

#devops #docker