27 décembre 2014

Multiple Dockerfiles for project

When you start to use docker for the application you're developing, you need to choose if the Dockerfile you write is designed to

  • Build from source and produce binary
  • Package built binary for production application
  • Build from source and hot-reload source code for quick development cycles

Docker doesn't let you (yet) set the file you want to use as a Dockerfile, and enforce your use Dockerfile.
A possible workaround is for you to define a "Build from source" Dockerfile at project root, so it can ADD the source directory, and build binary in a target directory, then add to this directory another Dockerfile designed to produce the production image, just adding the binary with runtime dependencies. You still miss the 3rd use case, that require the docker image you run to allow hot-reload of source code you bind-mount in container.

After some experiments with various approach, my preference is to build the Docker build context by myself. So I have 3 Dockerfiles : Dockerfile.dev, Dockerfile.build, Dockerfile.prod.

  • First one uses a VOLUME to access project source code and run the app with hot-reload enabled (typically, play run). This let you use your IDE to hack the code and see the resulting app running in Docker container.
  • Second one build the application and package it for production execution (mvn package). This is the reference build environment, the one you probably use for CI as well. You can setup Jenkins to archive the resulting artifacts, or can just run it and execute a cp or cat command to export it from Docker container.
  • Last one to use the built artifact (from previous step) and ADD it to another image that only define required runtime dependencies, so the image is as small as possible. Such an image can't be used isolated, as it relies on the build one, so for sample can't be used with trusted builds, until Docker team offer some way to support non-trivial build scenarios.

To work around lack of a --file option for docker build command (#2112), I'm passing the build context explicitly as a tar.gz archive - there is no overweight doing this, as docker build commands does the same with current folder.

gtar --transform='s|Dockerfile.dev|Dockerfile|' -cz * | docker build -t dev -

As I'm running OSX and the included tar command does not support --transform option (sic) I had to install gnu-tar with homebrew, so the gtar command I'm using. As this is not a trivial command this can be set within a makefile, so you can just run make dev|build|prod.

Hope this will be useful for you as well.

10 commentaires:

Unknown a dit…

thank you for the tip. reloading your play framework application works with docker on Mac? for me this is not the case

Unknown a dit…

Now you can do docker build -f Dockerfile.dev .

Anonyme a dit…

The cost of repairs, it is rising sharply since the iPhone 4 for screens. Indeed, since the sosav reparation iphone it is no longer possible to change the LCD screen and the touch screen separately because of the fragility and fineness of the screen Retina which must be assembled directly in the factory…..

johnson a dit…

This post is very interesting! I think it's important to take into account various viewpoints on this matter.I think it is important to respect everyone's opinions, regardless of what they are about.I concur that additional study in this area is necessary. Continue sharing.
leyes de divorcio en nueva jersey
virginia protective order violation

Mason a dit…

Accidente Fatal de Motocicleta en Virginia Beach
Working with multiple Dockerfiles in a project can improve flexibility and maintainability. Key considerations include clarity in purpose, version control, documentation, naming convention, layer organization, minimal base images, package managers, build arguments, multi-stage builds, environment variables, health checks, port exposure, volume mounts, base image (Alpine or Debian), dependency installation, security scanning, layer ordering, testing, versioning, validation scripts, secret handling, image tagging, orchestration readiness, custom entry points, graceful shutdown, cleanup commands, documentation updates, Docker Compose files, CI/CD integration, and periodic reviews. Clarity in purpose ensures each Dockerfile has a specific purpose, such as development, testing, or production. Version control stores Dockerfiles alongside code for better traceability. Maintaining clear documentation for each Dockerfile is crucial for better understanding and usage. Consistent naming conventions make them easily recognizable. Layer organization reduces layer duplication and promotes cache efficiency. Minimal base images can reduce image size and enhance security. Package managers like apt or npm should be cautious, pin versions to ensure reproducibility. Build arguments leverage build arguments to make Dockerfiles more versatile. Multi-stage builds can keep final images small while maintaining build-time dependencies.

Mark Antony a dit…

Smart approach to Dockerfile management! Having distinct Dockerfiles for different use cases and managing the build context effectively can streamline development and production workflows. Thanks for sharing this insightful strategy.
Impugnado Divorcio Estado de Nueva York

tomburke a dit…

Using multiple Dockerfiles within a project opens a gateway to versatility and customization, allowing tailored containerization for diverse application components. This approach permits precise configuration adjustments, optimizing each container's functionality, dependencies, and environment. It offers a nuanced solution, facilitating streamlined development, testing, and deployment of complex systems. Leveraging multiple Dockerfiles affords the flexibility to fine-tune containers, aligning with specific requirements, and ensuring an efficient, modularized architecture for the entire project.
from: embroidery digitizing services

Styrene a dit…

I like the theme of this blog. The post is interesting to read till the end of the blogs. Keep sharing more insights of this content in your future blogs. DUI Lawyer Fairfax

susan a dit…

Great blog! I just came across your post, and it's really helpful. Continue working.Abogado de DUI Condado de Prince William

shane a dit…

It seems like a flexible solution for managing diverse project requirements. I appreciate the examples and tips for managing complexity.
Truck accident lawyer chesapeake va offers expert legal representation for victims of truck collisions. They specialize in navigating complex laws, securing maximum compensation for injuries, property damage, and wrongful death. With deep knowledge of state regulations and a commitment to client advocacy, they strive to ensure justice is served for those affected by truck accidents.