Skip to content

fix: recreate service on ddev utility rebuild -s, support profile services - #8463

Merged
stasadev merged 1 commit into
ddev:mainfrom
stasadev:20260610_stasadev_rebuild_recreate_service
Jun 15, 2026
Merged

stasadev merged 1 commit into
ddev:mainfrom
stasadev:20260610_stasadev_rebuild_recreate_service

Conversation

@stasadev

@stasadev stasadev commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

The Issue

After the move to the Docker Compose SDK, ddev utility rebuild -s <service> broke for profile-gated services (for example xhgui, started via ddev xhgui on, or services behind ddev start --profiles=...): the build succeeds but the restart fails with no such service. The project was loaded without enabling compose profiles, so profile-gated services ended up in DisabledServices. Compose Build re-enables the services it is given, but Restart does not, so it could not find them.

A plain restart is also the wrong operation for a rebuild: it reuses the existing container (and therefore the old image), so the freshly built image is never applied, and restarting a previously-healthy container can trigger a long healthcheck startup delay (the ddev-xhgui healthcheck sleeps ~59s when it sees a previously-healthy marker).


We should figure out what's wrong with healthcheck https://discord.com/channels/664580571770388500/1385079277753597992/1385293828587655278

I also noticed that ddev debug rebuild may take longer with healthcheck. Some services may get stuck showing starting status. But I never found out why and how to solve it.

(ddev debug rebuild doesn't wait for anything if the project is stopped.)


DDEV v1.25.2:

(And it's stuck for around a minute while waiting Waiting for project container [xhgui] to become ready..., this is another bug, which is also fixed in this PR.)

$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
WARN[0000] No services to build
Rebuilt xhgui service cache for demo in 0s
Executing `/home/stas/.ddev/bin/docker-compose -f /home/stas/code/pet/demo/.ddev/.ddev-docker-compose-full.yaml --progress plain restart xhgui`
 Container ddev-demo-xhgui Restarting
 Container ddev-demo-xhgui Started
Waiting for project container [xhgui] to become ready...
ddev-router already running, pushing new config...
Waiting for ddev-router to become ready... ready in 0.5s
Restarted xhgui service for demo

DDEV HEAD:

$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
No services to build
Rebuilt xhgui service cache for demo in 4s
Restarting service xhgui...
Failed to restart service xhgui: no such service: xhgui

How This PR Solves The Issue

  • Load the compose project with all profiles enabled (Profiles: ["*"]) so profile-gated services are resolvable for the recreate step. This matches how the rest of the codebase loads the full rendered project for down/stop.
  • Recreate the service with Up + RecreateForce (scoped to the service via WithSelectedServices(..., IgnoreDependencies)) instead of Restart. This applies the freshly built image and starts a fresh container, which also avoids the previously-healthy healthcheck startup delay.
  • For a targeted -s <service> rebuild, pull only that service's base image instead of every project image (added DdevApp.FindServiceImages).
  • Use the project's container wait timeout instead of a hard-coded 60s, and show the elapsed wait time (Waiting for containers to become ready: [xhgui]... ready in 1.5s).

Manual Testing Instructions

This PR:

(And see that it's not stuck on the Waiting for containers to become ready line anymore.)

$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
No services to build
Rebuilt xhgui service cache for demo in 2s
Recreating service xhgui...
[+] up 1/1
 ✔ Container ddev-demo-xhgui Recreated          0.7s
Waiting for containers to become ready: [xhgui]... ready in 1.5s # timeout instead of a hard-coded 60s, and show the elapsed wait time
ddev-router already running, pushing new config...
Recreated xhgui service for demo

Automated Testing Overview

TestDebugRebuildCmd is extended:

  • After ddev utility rebuild --service db, it now asserts the new image is live immediately (reads /random-db.txt without an intervening app.Restart()), proving the recreate applies the freshly built image. The old restart-based code would have passed only because the test restarted before checking.
  • Adds a profile-gated case: ddev xhgui on then ddev utility rebuild --service xhgui, asserting success and the Recreated xhgui service message. This exercises the profile-aware project load that the bug fix depends on.

Release/Deployment Notes

ddev utility rebuild -s <service> now recreates the service (applying the freshly built image) instead of restarting it, and targeted rebuilds pull only the relevant service's base image rather than all project images. No config or data changes.

…ervices

## The Issue

- From Drupal Slack https://drupal.slack.com/archives/C5TQRQZRR/p1781083258776979

After the move to the Docker Compose SDK, `ddev utility rebuild -s <service>`
broke for profile-gated services (for example `xhgui`, started via
`ddev xhgui on`, or services behind `ddev start --profiles=...`): the build
succeeds but the restart fails with `no such service`. The project was loaded
without enabling compose profiles, so profile-gated services ended up in
`DisabledServices`. Compose `Build` re-enables the services it is given, but
`Restart` does not, so it could not find them.

A plain `restart` is also the wrong operation for a rebuild: it reuses the
existing container (and therefore the old image), so the freshly built image is
never applied, and restarting a previously-healthy container can trigger a long
healthcheck startup delay (the `ddev-xhgui` healthcheck sleeps ~59s when it sees
a previously-healthy marker).

DDEV v1.25.2:

(And it's stuck for around a minute while waiting `Waiting for project container [xhgui] to become ready...`, this is another bug, which is also fixed in this PR.)

We should figure out what's wrong with healthcheck https://discord.com/channels/664580571770388500/1385079277753597992/1385293828587655278

> I also noticed that `ddev debug rebuild` may take longer with healthcheck. Some services may get stuck showing starting status. But I never found out why and how to solve it.

> (`ddev debug rebuild` doesn't wait for anything if the project is stopped.)

```
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
WARN[0000] No services to build
Rebuilt xhgui service cache for demo in 0s
Executing `/home/stas/.ddev/bin/docker-compose -f /home/stas/code/pet/demo/.ddev/.ddev-docker-compose-full.yaml --progress plain restart xhgui`
 Container ddev-demo-xhgui Restarting
 Container ddev-demo-xhgui Started
Waiting for project container [xhgui] to become ready...
ddev-router already running, pushing new config...
Waiting for ddev-router to become ready... ready in 0.5s
Restarted xhgui service for demo
```

DDEV HEAD:

```text
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
No services to build
Rebuilt xhgui service cache for demo in 4s
Restarting service xhgui...
Failed to restart service xhgui: no such service: xhgui
```

## How This PR Solves The Issue

- Load the compose project with all profiles enabled (`Profiles: ["*"]`) so
  profile-gated services are resolvable for the recreate step. This matches how
  the rest of the codebase loads the full rendered project for down/stop.
- Recreate the service with `Up` + `RecreateForce` (scoped to the service via
  `WithSelectedServices(..., IgnoreDependencies)`) instead of `Restart`. This
  applies the freshly built image and starts a fresh container, which also
  avoids the previously-healthy healthcheck startup delay.
- For a targeted `-s <service>` rebuild, pull only that service's base image
  instead of every project image (added `DdevApp.FindServiceImages`).
- Use the project's container wait timeout instead of a hard-coded 60s, and show
  the elapsed wait time (`Waiting for containers to become ready: [xhgui]... ready in 1.5s`).

## Manual Testing Instructions

This PR:

(And see that it's not stuck on the `Waiting for containers to become ready` line anymore.)

```bash
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
No services to build
Rebuilt xhgui service cache for demo in 2s
Recreating service xhgui...
[+] up 1/1
 ✔ Container ddev-demo-xhgui Recreated          0.7s
Waiting for containers to become ready: [ximeout instead of a hard-coded 60s, and show
  the elapsed wait time (`Waiting for containers hgui]... ready in 1.5s
ddev-router already running, pushing new config...
Recreated xhgui service for demo
```

## Automated Testing Overview

`TestDebugRebuildCmd` is extended:

- After `ddev utility rebuild --service db`, it now asserts the new image is live
  immediately (reads `/random-db.txt` without an intervening `app.Restart()`),
  proving the recreate applies the freshly built image. The old `restart`-based
  code would have passed only because the test restarted before checking.
- Adds a profile-gated case: `ddev xhgui on` then
  `ddev utility rebuild --service xhgui`, asserting success and the
  `Recreated xhgui service` message. This exercises the profile-aware project
  load that the bug fix depends on.

## Release/Deployment Notes

`ddev utility rebuild -s <service>` now recreates the service (applying the
freshly built image) instead of restarting it, and targeted rebuilds pull only
the relevant service's base image rather than all project images. No config or
data changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@stasadev
stasadev requested a review from a team as a code owner June 10, 2026 11:41
@github-actions

Copy link
Copy Markdown

@tyler36 tyler36 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is great. This PR loads projects in 1/3 of the previous time!

Tests

Current Stable (v1.25.2)

  1. Check DDEV version
$ ddev -v
ddev version v1.25.2
  1. Enable XHGui and trigger rebuild
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
...
Restarted xhgui service for ddev-8462
# 1st: 1m33s
# 2nd: 1m29s

This PR

  1. Check DDEV version
$ ddev -v
ddev version v1.25.2-66-gcabb56401
  1. Enable XHGui and trigger rebuild
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
Restarted xhgui service for ddev-8462
# First result may off due to version change which may affect caching, image download
# 2nd: 26s
# 3rd: 19s
  1. Switch back to stable to confirm issue is present again.
$ ddev -v
ddev version v1.25.2
  1. Enable XHGui and trigger rebuild
$ ddev start && ddev xhgui on && ddev utility rebuild -s xhgui
Restarted xhgui service for ddev-8462
# First result may off due to version change which may affect caching, image download
# 2nd: 1m40s
# 2nd: 1m30s

@rfay rfay 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.

Thanks for taking care of these seemingly tiny details.

The code looks good. I was able to reproduce the problem on DDEV v1.25.2, and it's gone in this PR.

Thanks!

@stasadev
stasadev merged commit 66db6ba into ddev:main Jun 15, 2026
54 checks passed
@stasadev
stasadev deleted the 20260610_stasadev_rebuild_recreate_service branch June 15, 2026 15:36
rfay added a commit to rfay/ddev that referenced this pull request Sep 23, 2026
…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]>
stasadev pushed a commit to rfay/ddev that referenced this pull request Oct 1, 2026
…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]>
rfay added a commit to rfay/ddev that referenced this pull request Oct 1, 2026
…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]>
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.

3 participants