compose icon indicating copy to clipboard operation
compose copied to clipboard

Start containers as soon as they are ready

Open remcokranenburg opened this issue 1 year ago • 3 comments

Description

When using docker compose up (or watch), Compose first builds all required images and then starts containers for them. In some workloads, one container is ready to launch much earlier than another.

An example: a postgres container is ready almost immediately, while the backend container is still building. During development, it can be quite useful to already start working with the database.

Would it be possible to launch containers as soon as they are ready, not waiting for all images to be built first?

Some observations:

  • When one service depends_on another, it obviously must wait until the dependency is ready
  • This may not be a desirable behavior in production
  • This feature can be emulated by first starting the 'faster' services: docker compose up --wait database && docker compose up

remcokranenburg avatar May 01 '24 13:05 remcokranenburg

Would be a significant architectural change Also to be considered: what would be expected if build fails ? Should the already running services be stopped (like when you hit Ctrl+C to stop a compose up command) ?

For the scenario you describe, it seems to me relying on watch feature with a rebuild strategy already covers your need: even first time you run compose up you'll need to wait for build to complete before you can access database, then changes made to your backend source code will trigger a rebuild and restart so you don't have to wait more than required

ndeloof avatar May 01 '24 15:05 ndeloof

Yes I had a feeling this would not be an easy one, so I definitely don't expect anything unless it is useful for many people!

As said, I can mostly emulate this behavior by doing a smaller up before doing a full up (or watch), so that would solve my initial problem. Your suggestion of using the rebuild strategy doesn't completely cover it: if you open your laptop, do a git pull and then start watch, your colleagues could have changed a lot of code, so you may at any time face waiting for a rebuild that is out of your control.

That becomes especially aggravating when the changes are in a part you don't care about at that moment: you have 10 rebuilding containers, and you just want to work on that one container right now!

In any case, a workaround is available, but it could be more 'hands-off' by starting containers as soon as they are ready.

remcokranenburg avatar May 02 '24 14:05 remcokranenburg

Oh, I found another use case!

Some development containers have quite a long initialization phase: for example the Angular development server compiles source code on-the-fly (for hot-reloading support), but it can only start once the container starts. It would save quite some time if it could already start compiling even though some other services are still in the build phase.

remcokranenburg avatar May 02 '24 15:05 remcokranenburg

Adding support for this feature would require a significant redesign of compose architecture. I don't expect this to happen within the existing codebase. Tagged feature request for "future major release" assuming at some point we will consider a full re-implementation of compose with new usages in mind

ndeloof avatar Nov 08 '24 10:11 ndeloof

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Apr 09 '25 00:04 github-actions[bot]