Skip to content

fix: build a profile's images when it starts, and refresh built ones on --no-cache, fixes #8817 (#8820)[skip ci] - #8820

Merged
rfay merged 3 commits into
ddev:mainfrom
rfay:20260912_rfay_build_all_images
Oct 2, 2026
Merged

rfay merged 3 commits into
ddev:mainfrom
rfay:20260912_rfay_build_all_images

Conversation

@rfay

@rfay rfay commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Short Summary (TL;DR)

ddev start --profiles=x now builds the profile's images when it starts, and ddev start --no-cache and ddev restart --no-cache also rebuild profile images that were built before. Profiles that are never started still cost nothing on ddev start.

The Issue

ddev start --profiles=x reused an existing image of a profile service with build:, because Compose's Up only builds missing images. ddev restart --no-cache never rebuilt profile images, because the build step loaded the project without any profiles. So after a Dockerfile or dockerfile_inline edit, or to pick up a new upstream latest as 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 start and CI for profiles nobody uses, so it's reverted here.

How This PR Solves The Issue

What ddev start --profiles=x does depends on what the profile contains:

  • On a running project, a profile whose services only use image:, such as xhgui, is started on its own, without touching the other services, as before.
  • A profile with a build: service, or one that isn't in the last rendered compose file (as right after ddev add-on get), gets a full ddev start that includes it, so its image is built with the cache and edits apply. This replaces the old ddev restart && ddev start --profiles=x.
  • On a stopped project, everything starts in one Up.

--no-cache rebuilds, without the cache, the started services plus 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, and only the started services' images are pulled.

app.NoCache is replaced by StartOptions{Profiles, NoCache}, passed to the new StartWith() and RestartWith(). That leaves ddev restart --profiles from #7904 as a flag passed to RestartWith().

Manual Testing Instructions

ddev utility download-ddev --pr 8820
export PATH="$HOME/tmp/ddev-download-ddev/pr-8820:$PATH"
mkdir -p ~/tmp/test-profile-rebuild && cd ~/tmp/test-profile-rebuild
ddev config --project-type=php
cat > .ddev/docker-compose.custom.yaml <<'EOF'
services:
  custom:
    container_name: ddev-${DDEV_SITENAME}-custom
    image: ddev-${DDEV_SITENAME}-custom-built
    build:
      context: .
      dockerfile_inline: |
        FROM alpine:latest
        RUN echo "build 1" > /version.txt
    command: tail -f /dev/null
    profiles:
      - custom
EOF

ddev start
docker image ls ddev-test-profile-rebuild-custom-built
# Expect no image: an unused profile isn't built

ddev start --profiles=custom
ddev exec -s custom cat /version.txt
# Expect "build 1"

perl -pi -e 's/build 1/build 2/' .ddev/docker-compose.custom.yaml
ddev restart --no-cache
ddev start --profiles=custom
ddev exec -s custom cat /version.txt
# Expect "build 2": the restart rebuilt the image without starting custom

perl -pi -e 's/build 2/build 3/' .ddev/docker-compose.custom.yaml
ddev start --profiles=custom
ddev exec -s custom cat /version.txt
# Expect "build 3" on a running project, without a restart

docker ps --filter name=ddev-test-profile-rebuild-web --format '{{.CreatedAt}}'
ddev xhgui on
docker ps --filter name=ddev-test-profile-rebuild-web --format '{{.CreatedAt}}'
# Expect the same time: an image-only profile leaves web's container alone

Automated Testing Overview

  • TestStartProfileImages starts a project with three profiles, covering:
    • a plain start builds and pulls no profile image;
    • --profiles=x works on a stopped project;
    • on a running project, Dockerfile and inline edits apply with the cache;
    • the image-only --profiles=z leaves web's container alone;
    • RestartWith({NoCache: true}) rebuilds the existing x1 image without starting it, and never builds y1;
    • COMPOSE_PROFILES=y in .ddev/.env starts y1.
  • TestStartOfflineWithBuiltImages gains a never-built profile service, which must not block the offline fallback.
  • TestUnitSplitProfiles checks that an empty --profiles value leaves COMPOSE_PROFILES in effect.

Release/Deployment Notes

  • On a running project, ddev start --profiles=x for a profile with a build: service now runs a full ddev start, so start hooks run again.
  • --profiles replaces COMPOSE_PROFILES, as in Compose, so on a stopped project ddev start --profiles=x no longer also starts the profiles listed in COMPOSE_PROFILES.
  • DdevApp.NoCache is gone from the Go API; use StartWith(StartOptions{NoCache: true}).

🤖 Developed with assistance from Claude Code

Co-Authored-By: Claude Opus 5.5 [email protected]

@rfay
rfay requested a review from a team as a code owner September 12, 2026 16:36
@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

Download this PR's build for your OS and architecture with:

ddev utility download-ddev --pr 8820

Or download the artifact for your OS and architecture directly:

See Testing a PR.

Open in GitHub Codespaces

@mxr576

mxr576 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

I have followed the testing steps and I could confirm that before dev restart --no-cache && ddev start --profiles pi HAD NOT rebuild the project specific pi image based on the date column in docker image ls | grep ddev-pi but the version in this PR did rebuild it.

@rfay

rfay commented Sep 15, 2026

Copy link
Copy Markdown
Member Author

Does this work to solve the actual problem you encountered earlier @mxr576 ?

@mxr576

mxr576 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Does this work to solve the actual problem you encountered earlier @mxr576 ?

Yes, I think it does, see my testing results below.

Based on docker image ls | grep ...:

On 1.25.4

ddev restart && ddev start --profiles pi # does not rebuilt the images
ddev restart --no-cache && ddev start --profiles pi # does rebuilt web and db images but PI has not been rebuilt!
ddev stop && ddev start --no-cache && ddev start --profiles pi # does rebuilt web and db images but PI has not been rebuilt!

on this commit

ddev restart && ddev start --profiles pi # does not rebuilt the images
ddev restart --no-cache && ddev start --profiles pi # does rebuilt web, db and PI images
ddev stop && ddev start --no-cache && ddev start --profiles pi # does rebuilt web, db and PI images

@rfay
rfay force-pushed the 20260912_rfay_build_all_images branch from 14a8b0a to 4d070f6 Compare September 23, 2026 02:31

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@mxr576

mxr576 commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

every ddev start now builds the images of all profile-gated services, including profiles the user never activates. That adds build time,

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 ddev start build?

@stasadev
stasadev self-requested a review September 28, 2026 13:11
@stasadev
stasadev force-pushed the 20260912_rfay_build_all_images branch from 4d070f6 to 2a338c7 Compare October 1, 2026 17:19
@stasadev
stasadev requested a review from a team as a code owner October 1, 2026 17:19
@stasadev stasadev changed the title fix: build profile-gated custom service images during start/restart, fixes #8817 fix: build a profile's images when it starts, and refresh built ones on --no-cache, fixes #8817 Oct 1, 2026
rfay and others added 3 commits October 1, 2026 16:36
…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]>
@rfay
rfay force-pushed the 20260912_rfay_build_all_images branch from 2a338c7 to 637a8f6 Compare October 1, 2026 22:36

@stasadev stasadev left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' 

@rfay rfay changed the title fix: build a profile's images when it starts, and refresh built ones on --no-cache, fixes #8817 fix: build a profile's images when it starts, and refresh built ones on --no-cache, fixes #8817 (#8820)[skip ci] Oct 2, 2026
@rfay
rfay merged commit d8e2143 into ddev:main Oct 2, 2026
46 of 47 checks passed
@rfay
rfay deleted the 20260912_rfay_build_all_images branch October 2, 2026 19:00
stasadev added a commit to stasadev/ddev that referenced this pull request Oct 6, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ddev restart --no-cache does not rebuild profile-gated service images

3 participants