DEV Community

Cover image for Advanced Dockerfile Directives

Advanced Dockerfile Directives

Kostas Kalafatis on July 08, 2024

In this post, we are going to discuss more advanced Dockerfile directives. These directives can be used to create more advanced Docker images. For...
Collapse
 
rouqe profile image
Jimben Honeyfield

Great article writeup! I especially liked the ONBUILD COPY part!

I always felt having a single container image that copies the contents of a file didn't lend itself very well to an iterative approach for larger container sizes. For instance, write some code, test, build container over, and over. By splitting out the requirements into a separate container image, and using the ONBUILD COPY command I see how this can work effectively.

Thanks for the great article writeup! Looking forward to the next one!!

Collapse
 
kalkwst profile image
Kostas Kalafatis

Thank you for your feedback! I'm glad you found the article helpful, especially the discussion on ONBUILD COPY.

You're absolutely right about the challenges of maintaining larger container images in iterative development cycles. ONBUILD COPY offers a great solution by allowing us to separate the dependencies and build artifacts into a builder image. This approach not only streamlines the Docker build process but also enhances flexibility and repeatability in deployments.

By decoupling requirements handling from the main application image, we can manage changes and updates more efficiently. It also ensures that each build incorporates the latest dependencies without bloating the final image size unnecessarily.

Keep in mind that the same can be achieved with multi-stage Docker builds though.

In multistage builds, you define multiple FROM instructions in your Dockerfile, each representing a different stage or phase of the build process. Typically, you start with a builder stage where you compile or build your application along with its dependencies. Once the build artifacts are ready, you can then copy only the necessary files from the builder stage into a smaller, final stage image. This final image is what you ultimately use for deployment.

For example, you first create a builder stage image. This stage is where you install dependencies, compile code, and generate any necessary artifacts. You can use a larger image with build tools and dependencies.

FROM base-image as builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
Enter fullscreen mode Exit fullscreen mode

And then you can use a final stage image. In this stage, you start with a minimal base image (like alpine or scratch) and copy only the built artifacts from the builder stage. This ensures that the final image is as small and efficient as possible.

FROM base-image
COPY --from=builder /app/dist /app
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

This approach not only reduces the size of your Docker image by excluding unnecessary build dependencies but also enhances security and build efficiency. Each stage runs in isolation, allowing Docker to cache intermediate images and speed up subsequent builds.

When choosing between ONBUILD COPY and multistage Docker builds, align your decision with your project's specific needs and complexity.

ONBUILD COPY:

  • Best for simpler projects with straightforward build processes.
  • Ideal for automating the copying of files into derived images.
  • Works seamlessly for single-stage builds, keeping Dockerfiles concise.
  • Well-suited for creating generic base images for wider use.

Multistage Builds:

  • Excels in complex environments with multiple stages and dependencies.
  • Offers fine-grained control over build stages and image layers.
  • Optimizes final image size by eliminating unnecessary build tools and dependencies.
  • Enhances security by reducing the attack surface.
  • Supports iterative development with efficient caching mechanisms.

In essence, if your project involves simple build steps and you want to streamline the process of inheriting files from a base image, ONBUILD COPY is a practical choice. However, if you're dealing with more intricate build processes, require greater control over image layers, or prioritize minimizing image size and maximizing security, multistage builds are the way to go.

Both approaches have their strengths and cater to different use cases.

If you have any more insights or experiences to share, I'd love to hear them. Feel free to reach out anytime.

Collapse
 
rouqe profile image
Jimben Honeyfield

Wow thank you for the detailed response! You answered my question before I had the chance to ask it! 😆

Collapse
 
leadsbuilds profile image
Wendell

Very useful, thanks

Collapse
 
kalkwst profile image
Kostas Kalafatis

Glad to hear you found the information helpful!

Collapse
 
amit_prajapati_b10f0eb8a8 profile image
Amit Prajapati

Wow

Collapse
 
jangelodev profile image
João Angelo

Hi Kostas Kalafatis,
Top, very nice and helpful !
Thanks for sharing.

Collapse
 
kalkwst profile image
Kostas Kalafatis

Thank you for your kind words! I'm glad you found the information helpful.

Collapse
 
clabnet profile image
Claudio Barca

Very nice.

Collapse
 
kalkwst profile image
Kostas Kalafatis

Thank you, I'm glad you liked it!

Collapse
 
adriens profile image
adriens

Thanks a lot for having made me discover the HEALTHCHECK, I did'nt know it was existing wthin docker.

Collapse
 
josephrana11 profile image
Joseph Rana • Edited

Need one about Optimizing Docker Images and Networking in Docker. Great Explanation!

Collapse
 
kalkwst profile image
Kostas Kalafatis

Hey! Thank you for your interest in my Docker series! I'm glad you enjoyed the explanations I provided.

Currently, I have a backlog of posts, but I've scheduled your suggestion on "Optimizing Docker Images and Networking in Docker" for later on in the series. I aim to cover this topic comprehensively to provide valuable insights.

If you have any specific aspects or questions you'd like me to address in the post, please feel free to share them. Your input helps tailor the content to meet your needs better.

Thank you again for your suggestion and patience!

Collapse
 
tgkprog profile image
Tushar Kapila

i think a good follow up would be to have an article with 5-6 real world examples of complex Docker files, a curated list from github and short explnations of what they do.

Collapse
 
oriroth profile image
אורי רוט

Loved it!
Please write the next one about multi layered Dockerfile

Collapse
 
kalkwst profile image
Kostas Kalafatis

Thank you so much for your feedback and enthusiasm! I'm really glad you enjoyed the article. I appreciate your interest in multi-layered Dockerfiles. They're indeed a powerful topic that deserves detailed exploration.

While the next article won't specifically cover multi-layered Dockerfiles, I'm planning to delve into that topic soon. If all goes as scheduled, you can expect it around August 12th. Stay tuned for more updates and thank you for your continued interest and support!