> ## Documentation Index
> Fetch the complete documentation index at: https://docker-php.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Upgrade to 4.0

> Migrate from the original 2.x client, the Beluga fork or 3.x.

Most applications come from the original `docker-php/docker-php` 2.x client
or from `beluga-php/docker-php` 1.45.x. Follow the section for your current
package, then apply the [changes from 3.x](#from-3-x), because 4.0 also changes
defaults that 2.x and Beluga applications rely on.

## From the original 2.x client

The new client requires PHP 8.2 or later, the `mbstring` extension, PSR-7 v2 and
Jane 7 runtimes. Check your application's other dependencies against these
requirements before changing its Composer constraints.

Change the client requirement to `docker-php/docker-php:^4.0`. Remove an explicit
`docker-php/docker-php-api:4.1.*` requirement, or change it to one
[API line](https://docker-php.mintlify.site/api-versions.md) if your application depends on the API package directly.

```json theme={null}
{
  "require": {
    "docker-php/docker-php": "^4.0"
  }
}
```

Resolve the package changes together:

```bash theme={null}
composer update docker-php/docker-php docker-php/docker-php-api --with-all-dependencies
```

Review endpoint signatures and generated model types. The PHP namespaces remain
`Docker` and `Docker\API`, but that does not make all old API models or calling
patterns compatible. The specification changes from the old API line to the one
you install. Then apply the [changes from 3.x](#from-3-x).

### Asynchronous client

The old `DockerAsync` client and its Amp/Artax examples do not apply to 4.0.
The maintained client uses synchronous endpoint calls and callback streams.
Applications using `DockerAsync` need to adapt those call sites; this is not a
drop-in replacement for the old promise-based client.

### HTTP clients

The default client now uses the maintained PSR-7 v2 dependency set. Review custom
HTTP adapters for PSR-18 compatibility, daemon connection settings, unbuffered
responses and upgraded Docker connections. See [Connection settings](https://docker-php.mintlify.site/connection.md).

### Log and exec consumers

Use `onStdout()` and `onStderr()` with `wait()` to consume decoded output. Do not
print non-TTY raw response bodies directly: they contain binary frame headers.
TTY output combines stdout and stderr, and callbacks receive chunks rather
than guaranteed complete lines. See [logs](https://docker-php.mintlify.site/guides/logs.md) and
[exec output](https://docker-php.mintlify.site/guides/exec.md).

## From Beluga

The new client continues the code maintained under `beluga-php/docker-php`.
For applications on the Beluga API v1.45 line, the package-name migration does
not require a PHP namespace change.

Remove both Beluga requirements from your application's `composer.json`, if
present. Add `docker-php/docker-php:^4.0`. If you require the API package
directly, add the matching line as well, for example
`docker-php/docker-php-api:>=7.1.45.0 <7.1.46.0` for the Beluga v1.45 models.

Do not install both package families together: they contain the same PHP
classes and the new packages declare conflicts with their Beluga counterparts.
Resolve the dependency changes together, rather than installing the new client
while keeping an old direct API requirement.

```bash theme={null}
composer update --with-all-dependencies
```

Run that broader update in a test checkout, review unrelated dependency changes,
and commit the tested lock file. If you use another Beluga Docker API line,
require the matching line here. Then apply the [changes from 3.x](#from-3-x).

## From 3.x

Change the client requirement to `docker-php/docker-php:^4.0`. Applications that
cannot upgrade yet can stay on `^3.3`, which keeps Docker Engine API v1.45.

### API version

4.1 works with API v1.45 to v1.57, and Composer installs the newest API line
unless you require one. Generated models and their methods differ between
versions. To keep the 3.x models, require the v1.45 line:

```bash theme={null}
composer require "docker-php/docker-php:^4.0" "docker-php/docker-php-api:>=7.1.45.0 <7.1.46.0"
```

To move to a newer line, check the models your code names. For example:

* API 1.46 renamed `NetworksCreatePostResponse201` to `NetworkCreateResponse`.
* API 1.48 renamed `ContainersIdJsonGetResponse200` to `ContainerInspectResponse`
  and added `ContainerStatsResponse`.
* API 1.52 removed `getError()` from `BuildInfo`, `CreateImageInfo` and
  `PushImageInfo`. Use `Docker\Stream\FrameError::message($frame)`, which works
  with every line.

See [API versions](https://docker-php.mintlify.site/api-versions.md) and Docker's
[API version history](https://docs.docker.com/reference/api/engine/version-history/).

### Request version negotiation

Without `api_version` or `DOCKER_API_VERSION`, the bundled factory now sends an
unversioned `/_ping` before its first request and uses the lower of the daemon's
API version and the installed line. Setting either keeps a fixed version, as in
3.x.

### Unexpected error statuses throw

An error status of 400 or above that the generated endpoint does not handle now
throws `UnexpectedClientErrorException` or `UnexpectedServerErrorException`.
In 3.x those calls returned `null`. To keep that behaviour:

```php theme={null}
$docker = Docker::create()->throwOnUnexpectedStatus(false);
```

See [unexpected error statuses](https://docker-php.mintlify.site/reference/requests-and-responses.md#unexpected-error-statuses).

### `.dockerignore` is applied

`Context` now leaves out the paths a context's `.dockerignore` excludes, as
`docker build` does. To archive the whole directory as 3.x did, call
`applyDockerignore(false)` on the context.

### Stats samples

From API 1.48, `containerStats()` returns `ContainerStatsResponse` models, for one
sample and for each `StatsStream` frame. Earlier lines still return decoded
`stdClass` objects.

### Subclasses of `Docker`

`Docker::systemEvents()` has a new optional `$accept` parameter. A subclass that
overrides it needs the same parameter.

## Verify the application

* Confirm only the original package family is installed with `composer show`.
* Test the daemon connection and representative inspect, list and create calls.
* Check log and exec output, including TTY and non-TTY containers.
* Review custom HTTP clients and nullable model fields.
* Commit `composer.lock` and keep the prior release and lock file available for
  rollback before deploying.

Existing bounded client constraints such as `^2.0` or `^3.0` do not
automatically admit 4.0. Existing API `4.1.*` constraints do not admit the new
7.x packages. Broad or
unbounded requirements do not provide that protection.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.