Repository navigation
fix: build a profile's images when it starts, and refresh built ones on --no-cache, fixes #8817 (#8820)[skip ci] - #8820
Conversation
|
Download this PR's build for your OS and architecture with: ddev utility download-ddev --pr 8820Or download the artifact for your OS and architecture directly:
See Testing a PR. |
|
I have followed the testing steps and I could confirm that before |
|
Does this work to solve the actual problem you encountered earlier @mxr576 ? |
Yes, I think it does, see my testing results below. Based on On 1.25.4on this commit |
14a8b0a to
4d070f6
Compare
stasadev
left a comment
There was a problem hiding this comment.
Claude Code built this branch on Linux (Docker) and ran the #8817 reproduction. The composeBuild() change works: a plain ddev start now builds the profile-gated image, and ddev restart --no-cache followed by ddev start --profiles=custom picks up the new build.
The NoCache: app.NoCache change in StartOptionalProfiles() doesn't fix ddev start --no-cache --profiles=x on a project that is already running, which the description says it does:
| Step | /version.txt |
|---|---|
ddev start --profiles=custom |
build 1 |
Change the Dockerfile to build 2, then ddev start --no-cache --profiles=custom while the project is running |
build 1 |
ddev restart --no-cache, then ddev start --profiles=custom |
build 2 |
The cause is in Compose's Up path. build() skips any service whose image already exists locally unless it has pull_policy: build (vendor/github.com/docker/compose/v5/pkg/compose/build.go:84-86), so the BuildOptions passed to Up are never used once the image exists. On a running project StartOptionalProfiles() skips app.Start(), so composeBuild() doesn't run either. The new NoCache only takes effect when the image is missing, and after this PR that rarely happens because every ddev start builds all profile images.
One way to fix it is to call app.composeBuild() for the profile's services before Up when app.NoCache is set. The other is to drop that claim from the description and leave the running-project case to #7904.
A smaller point for the description: every ddev start now builds the images of all profile-gated services, including profiles the user never activates. That adds build time, and a broken Dockerfile in an unused profile now fails ddev start where it used to be ignored. It's probably the right trade-off, but it's worth saying so, and custom-compose-files.md could mention that these images are built even though their services aren't started.
🤖 Developed with assistance from Claude Code
This is actually one of the reasons I leaned so heavily on profile-gated services when designing my add-ons, especially the PI Coding Agent wrapper. I specifically wanted to avoid any runtime overhead when a wrapped service is clearly not needed. For example, CI pipelines shouldn't get slower just because a project happens to use an AI coding agent, if that agent's service is never actually activated in that pipeline. I appreciate the fix for rebuilding a profile-gated image when its profile is activated. Could we preserve that behavior without including inactive profiles in every |
4d070f6 to
2a338c7
Compare
…ixes ddev#8817 `ddev start` and `ddev restart` (including `--no-cache`) silently skipped building images for custom services gated behind a Compose profile, because the internal build step never loaded those profiles. - Fixes ddev#8817 `composeBuild()`, the function `ddev start`/`ddev restart` use to build project images, loaded the Compose project without specifying any profiles. Compose-go drops services gated behind an unactivated profile from the loaded project entirely, so `docker compose build` never saw them, regardless of `--no-cache`. Separately, `StartOptionalProfiles()` (used by `ddev start --profiles=...` and xhgui) built with a Docker Compose `Up` call that never passed `NoCache`, so `--no-cache` was ignored when activating a profile on an already-running project. `composeBuild()` now loads the Compose project with `Profiles: []string{"*"}`, the same pattern already used by `ddev utility rebuild`, `compose_yaml.go`, and `utils.go`, so profile-gated services are always part of the build. `StartOptionalProfiles()` now passes `app.NoCache` through to its `Build` options. `ddev utility rebuild` already handled this correctly (from the earlier ddev#8463 fix) and needed no change. 1. Define a custom service in `.ddev/docker-compose.custom.yaml` with a `build:` section and a `profiles:` key, without ever passing `--profiles` on start. 2. Run `ddev start`, then `docker images` — the service's image is now built even though its profile was never activated. 3. Change the Dockerfile, run `ddev restart --no-cache`, and confirm the image is rebuilt. Added `TestDdevApp_ComposeBuildIncludesProfileGatedServices` in `pkg/ddevapp/start_test.go`, which starts a project with a profile-gated build-based service whose profile is never activated, then asserts the image was built anyway. Verified it fails against the pre-fix code and passes with the fix. 🤖 Developed with assistance from [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Sonnet 5 <[email protected]>
Loading every profile in composeBuild() made each `ddev start` build images for profiles the user never starts, which slows CI and lets a broken Dockerfile in an unused profile fail the start. The following commits build a profile's images when it starts, and refresh existing ones on `--no-cache`. This reverts commit 4ed7bd185a4f0b292e47abcbdecf14ee964e9ac8. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on --no-cache, fixes ddev#8817 `ddev start --profiles=x` reused an existing image of a profile service with `build:`, because Compose's `Up` only builds missing images, and `ddev restart --no-cache` never rebuilt profile images at all. Starting a profile now depends on what it contains. On a running project, a profile whose services only use `image:`, such as xhgui, is started without touching the other services. A profile with a `build:` service, or one missing from the last rendered compose file, gets a full start that includes it, so its image is built with the cache and compose file or Dockerfile edits apply. A stopped project starts everything in one `Up`. `--no-cache` rebuilds the started services and every profile service whose image already exists locally, so `ddev restart --no-cache` refreshes an add-on's image before its profile is started again. Profile images that were never built stay unbuilt, so unused profiles add nothing to `ddev start`, and only the started services' images are pulled. `--profiles` replaces `COMPOSE_PROFILES`, as in Compose. `app.NoCache` is replaced by `StartOptions`, passed to `StartWith()` and `RestartWith()`, which leaves room for `ddev restart --profiles` from ddev#7904. Co-Authored-By: Claude Opus 5.5 <[email protected]>
2a338c7 to
637a8f6
Compare
There was a problem hiding this comment.
Looks good to me.
Ignore this warning in manual testing instructions, it has nothing to do with the PR itself and just shows that it can't get something it didn't find:
...
$ ddev start --profiles=custom
Starting test-profile-rebuild...
[+] pull 1/1
✘ Image ddev-test-profile-rebuild-custom Error pull access denied for ddev-test-profile-rebuild-custom, repository d... 1.0s
Unable to pull Docker images: Error response from daemon: pull access denied for ddev-test-profile-rebuild-custom, repository does not exist or may require 'docker login'
...
On top of that, I installed this add-on:
ddev add-on get ddev/ddev-mongo
ddev restart
ddev start --profiles=mongo-express
No mongo-express pulled as expected:
$ ddev restart
Network ddev_default created
Restarting project test-profile-rebuild...
[+] pull 10/10
✔ Image mongo:latest Pulled 11.2s
...
And it's pulled here as expected:
$ ddev start --profiles=mongo-express
Starting test-profile-rebuild...
[+] up 12/12
✔ Image mongo-express:1.0 Pulled 6.4s
✔ Container ddev-test-profile-rebuild-mongo-express Created 0.4s
ddev-router already running, pushing new config...
Waiting for containers to become ready: [mongo-express]... ready in 0.5s
Started optional compose profiles 'mongo-express'
## Short Summary (TL;DR) `ddev restart -s solr,redis` gives just those services new containers with the current configuration and leaves the rest of the project running, so an unhealthy or crashed service comes back without a full restart. `ddev restart --profiles=x` replaces `ddev restart && ddev start --profiles=x`. `ddev utility rebuild -s` also takes several services now. ## The Issue - Fixes ddev#7904 Restarting one service meant remembering `ddev utility rebuild -s <service> --cache`, and an add-on with a profile needed `ddev restart && ddev start --profiles=<profile>`. ## How This PR Solves The Issue `--service` re-renders the compose files, then recreates the named services in one compose `Up`, as `ddev utility rebuild -s` has done since ddev#8463, so edits to `.ddev/docker-compose.*.yaml` apply. A new container also avoids the healthcheck delay the issue mentions. A named service has to have a container, so an unhealthy or crashed service is replaced like any other. Starting a service that has none is left to `ddev start --profiles` and `ddev xhgui on`, because they also run setup some services need, such as the database xhgui uses, and the error names the command to run. A `depends_on` service keeps its container, so naming a service that depends on `web` doesn't touch `web`. Like `ddev start`, it builds their images first, so Dockerfile edits apply too, and `--no-cache` builds them without the cache. Also like `ddev start`, it refuses to recreate `db` when `config.yaml` names a different database type or version than the volume holds, before building anything, instead of running the new image against the old data. Naming `web` restarts the whole project, since `ddev start` sets up more than its container, and starts the profiles of the services that were started, so xhgui or another optional service survives it. Plain `ddev restart` still drops them, as before. `--service` can't be combined with `--all`, `--profiles`, `--reset-database`, or `--seed-snapshot`. `--profiles` is passed to `RestartWith()`, which ddev#8820 prepared for it. `ddev utility rebuild --service` takes a comma-separated list too, and both commands share the new `RecreateServices()`. Rebuild still recreates only the services that have a container, and with `web` named it keeps the optional services that were started, as `ddev restart -s web` does. ## Manual Testing Instructions Set up a project with a profile service that has a `build:` and a dependency with a healthcheck in the same profile: ```bash ddev utility download-ddev --pr 8906 export PATH="$HOME/tmp/ddev-download-ddev/pr-8906:$PATH" mkdir -p ~/tmp/restart-7904 && cd ~/tmp/restart-7904 ddev config --project-type=php cat > .ddev/docker-compose.busybox.yaml <<'EOF' services: busybox1: build: context: . dockerfile_inline: | FROM busybox:stable RUN cat /proc/sys/kernel/random/uuid > /built ENV RESTART_BUILT=first init: true command: tail -f /dev/null environment: - RESTART_TEST=first depends_on: - busybox2 profiles: - busybox container_name: ddev-${DDEV_SITENAME}-busybox1 busybox2: image: busybox:stable init: true command: sh -c 'touch /tmp/healthy && exec tail -f /dev/null' healthcheck: test: test -f /tmp/healthy interval: 1s retries: 1 profiles: - busybox container_name: ddev-${DDEV_SITENAME}-busybox2 EOF containers() { docker ps --filter label=com.ddev.site-name=restart-7904 --format '{{.Names}} {{.ID}}' | sort; } busybox1() { docker exec ddev-restart-7904-busybox1 sh -c 'echo "$RESTART_TEST $RESTART_BUILT $(cat /built)"'; } ddev start containers ``` A profile service, before and after its profile is started: ```bash ddev restart -s busybox1 # Expect "service busybox1 is not running, use 'ddev start --profiles=busybox' to start it" ddev start --profiles=busybox containers ddev restart -s busybox1 containers # Expect only busybox1 to get a new ID busybox1 # Expect "first first <uuid>" ``` Compose file and Dockerfile edits, and `--no-cache`: ```bash sed -i 's/=first/=second/' .ddev/docker-compose.busybox.yaml ddev restart -s busybox1 busybox1 # Expect "second second" and the same uuid: the image was rebuilt from the cache ddev restart -s busybox1 --no-cache busybox1 # Expect "second second" and a new uuid: the image was rebuilt without the cache ``` `--profiles`: ```bash ddev restart containers # Expect only web and db ddev restart --profiles=busybox --no-cache # Expect "Started optional compose profiles 'busybox'" busybox1 # Expect a new uuid ``` Several services, including a built-in profile, and `ddev utility rebuild`: ```bash ddev restart -s xhgui # Expect "service xhgui is not running, use 'ddev xhgui on' to start it" ddev xhgui on ddev restart -s xhgui,db # Expect "Waiting for containers to become ready: [xhgui db]" and "Restarted xhgui, db in restart-7904" ddev utility rebuild --cache -s db,xhgui # Expect "Recreated db, xhgui for restart-7904" ``` An unhealthy service and a crashed one: ```bash ddev exec -s busybox2 rm /tmp/healthy sleep 2 docker inspect --format '{{.State.Health.Status}}' ddev-restart-7904-busybox2 # Expect "unhealthy" ddev restart -s busybox2 docker inspect --format '{{.State.Health.Status}}' ddev-restart-7904-busybox2 # Expect "healthy" docker kill ddev-restart-7904-db ddev restart -s db # Expect "Restarted db in restart-7904" ``` `web`, rejected flags, and a stopped project: ```bash ddev restart -s web # Expect "web can't restart on its own, restarting the whole project..." containers # Expect every container to have a new ID, xhgui, busybox1, and busybox2 included ddev restart -s db --profiles=busybox # Expect "if any flags in the group [service profiles] are set none of the others can be" ddev stop ddev restart -s db # Expect "service db is not running, use 'ddev start' to start it" ``` ## Automated Testing Overview - `TestRestartServices` covers restarting `db` alone and `db` with a profile service together, replacing an exited `db`, refusing a profile service that wasn't started or a `db` that doesn't match its volume, leaving its dependency's container alone, applying a compose file and Dockerfile edit, rebuilding a `build:` service's cached layers only with `NoCache`, an unknown service, and the full restart for `web` keeping the running profile services running. - `TestCmdRestartProfilesAndServices` runs `ddev restart --profiles` and `ddev restart -s db,busybox1`, checks that `--service` with `--profiles` is rejected, and checks that `-s db,<TAB>` completes the next service that has a container. - `TestDebugRebuildCmd` rebuilds `--service=db,xhgui`. It skips under `GOTEST_SHORT`, so CI runs it in the all-project-types job once ddev#8903 is merged. It passes locally with the skip removed. ## Release/Deployment Notes - `DdevApp.RecreateServices()`, `DdevApp.RestartServices()`, and `DdevApp.RestartKeepingProfiles()` are new in the Go API. - `ddev utility rebuild -s web` keeps the optional services that were started instead of stopping them. - `ddev utility rebuild` prints `Recreated <services> for <project>` instead of `Recreated <service> service for <project>`. - `ddev utility rebuild -s` now starts a dependency of the rebuilt service if it isn't running, instead of ignoring it. - `ddev utility rebuild -s db` refuses to recreate `db` when its configured type or version doesn't match the database volume, as `ddev start` does. 🤖 Developed with assistance from [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Short Summary (TL;DR)
ddev start --profiles=xnow builds the profile's images when it starts, andddev start --no-cacheandddev restart --no-cachealso rebuild profile images that were built before. Profiles that are never started still cost nothing onddev start.The Issue
ddev restart --no-cachedoes not rebuild profile-gated service images #8817ddev start --profiles=xreused an existing image of a profile service withbuild:, because Compose'sUponly builds missing images.ddev restart --no-cachenever rebuilt profile images, because the build step loaded the project without any profiles. So after a Dockerfile ordockerfile_inlineedit, or to pick up a new upstreamlatestas in ddev-pi, the profile kept running the old image.The first version of this PR built every profile's image on every start, which slows down
ddev startand CI for profiles nobody uses, so it's reverted here.How This PR Solves The Issue
What
ddev start --profiles=xdoes depends on what the profile contains:image:, such as xhgui, is started on its own, without touching the other services, as before.build:service, or one that isn't in the last rendered compose file (as right afterddev add-on get), gets a fullddev startthat includes it, so its image is built with the cache and edits apply. This replaces the oldddev restart && ddev start --profiles=x.Up.--no-cacherebuilds, without the cache, the started services plus every profile service whose image already exists locally, soddev restart --no-cacherefreshes an add-on's image before its profile is started again. Profile images that were never built stay unbuilt, and only the started services' images are pulled.app.NoCacheis replaced byStartOptions{Profiles, NoCache}, passed to the newStartWith()andRestartWith(). That leavesddev restart --profilesfrom #7904 as a flag passed toRestartWith().Manual Testing Instructions
Automated Testing Overview
TestStartProfileImagesstarts a project with three profiles, covering:--profiles=xworks on a stopped project;--profiles=zleaves web's container alone;RestartWith({NoCache: true})rebuilds the existingx1image without starting it, and never buildsy1;COMPOSE_PROFILES=yin.ddev/.envstartsy1.TestStartOfflineWithBuiltImagesgains a never-built profile service, which must not block the offline fallback.TestUnitSplitProfileschecks that an empty--profilesvalue leavesCOMPOSE_PROFILESin effect.Release/Deployment Notes
ddev start --profiles=xfor a profile with abuild:service now runs a fullddev start, so start hooks run again.--profilesreplacesCOMPOSE_PROFILES, as in Compose, so on a stopped projectddev start --profiles=xno longer also starts the profiles listed inCOMPOSE_PROFILES.DdevApp.NoCacheis gone from the Go API; useStartWith(StartOptions{NoCache: true}).🤖 Developed with assistance from Claude Code
Co-Authored-By: Claude Opus 5.5 [email protected]