PHPackages                             alvar01o/docker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [DevOps &amp; Deployment](/categories/devops)
4. /
5. alvar01o/docker

ActiveLibrary[DevOps &amp; Deployment](/categories/devops)

alvar01o/docker
===============

Run a docker container in your PHPUnit tests

1.6.3(5y ago)013MITPHPPHP ^7.4 || ^8.0

Since Feb 2Pushed 5y agoCompare

[ Source](https://github.com/Alvar01o/docker)[ Packagist](https://packagist.org/packages/alvar01o/docker)[ Docs](https://github.com/spatie/docker)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/alvar01o-docker/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (7)Versions (13)Used By (0)

Manage docker containers with PHP
=================================

[](#manage-docker-containers-with-php)

This package is a fork from spatie/docker and provides a nice way to start docker containers, docker-compose commands.

```
$containerInstance = DockerContainer::create($imageName)->start();

$process = $containerInstance->execute('whoami');

$process->getOutput(); // returns the name of the user inside the docker container
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require alvar01o/docker
```

Usage
-----

[](#usage)

You can get an instance of a docker container using

```
$containerInstance = DockerContainer::create($imageName)->start();
```

By default the container will be daemonized and it will be cleaned up after it exists.

You can get a docker compose command using

```
$dockerComposeDirectory = './scripts/laravel-commands';
$dockerCompose = new DockerCompose($dockerComposeDirectory);
$dockerCompose->up();
$dockerCompose->start();
```

### Customizing the docker container

[](#customizing-the-docker-container)

#### Prevent daemonization

[](#prevent-daemonization)

If you don't want your docker being daemonized, call `doNotDaemonize`.

```
$containerInstance = DockerContainer::create($imageName)
    ->doNotDaemonize()
    ->start();
```

#### Prevent automatic clean up

[](#prevent-automatic-clean-up)

If you don't want your docker being cleaned up after it exists, call `doNotCleanUpAfterExit`.

```
$containerInstance = DockerContainer::create($imageName)
    ->doNotCleanUpAfterExit()
    ->start();
```

#### Priviliged

[](#priviliged)

If you want your docker being privileged, call `privileged`.

```
$containerInstance = DockerContainer::create($imageName)
    ->privileged()
    ->start();
```

#### Naming the container

[](#naming-the-container)

You can name the container by passing the name as the second argument to the constructor.

```
new DockerContainer($imageName, $nameOfContainer));
```

Alternatively, use the `name` method.

```
$containerInstance = DockerContainer::create($imageName)
    ->name($yourName)
    ->start();
```

#### Mapping ports

[](#mapping-ports)

You can map ports between the host machine and the docker container using the `mapPort` method. To map multiple ports, just call `mapPort` multiple times.

```
$containerInstance = DockerContainer::create($imageName)
    ->mapPort($portOnHost, $portOnContainer)
    ->mapPort($anotherPortOnHost, $anotherPortOnContainer)
    ->start();
```

#### Environment variables

[](#environment-variables)

You can set environment variables using the `setEnvironmentVariable` method. To add multiple arguments, just call `setEnvironmentVariable` multiple times.

```
$containerInstance = DockerContainer::create($imageName)
    ->setEnvironmentVariable($variableKey, $variableValue)
    ->setEnvironmentVariable($anotherVariableKey, $anotherVariableValue)
    ->start();
```

#### Setting Volumes

[](#setting-volumes)

You can set volumes using the `setVolume` method. To add multiple arguments, just call `setVolume` multiple times.

```
$containerInstance = DockerContainer::create($imageName)
    ->setVolume($pathOnHost, $pathOnDocker)
    ->setVolume($anotherPathOnHost, $anotherPathOnDocker)
    ->start();
```

#### Setting Labels

[](#setting-labels)

You can set labels using the `setLabel` method. To add multiple arguments, just call `setLabel` multiple times.

```
$containerInstance = DockerContainer::create($imageName)
    ->setLabel($labelName, $labelValue)
    ->setLabel($anotherLabelName, $anotherLabelValue)
    ->start();
```

#### Automatically stopping the container after PHP exists

[](#automatically-stopping-the-container-after-php-exists)

When using this package in a testing environment, it can be handy that the docker container is stopped after `__destruct` is called on it (mostly this will happen when the PHP script ends). You can enable this behaviour with the `stopOnDestruct` method.

```
$containerInstance = DockerContainer::create($imageName)
    ->stopOnDestruct()
    ->start();
```

#### Getting the start command string

[](#getting-the-start-command-string)

You can get the string that will be executed when a container is started with the `getStartCommand` function

```
// returns "docker run -d --rm spatie/docker"
DockerContainer::create($imageName)->getStartCommand();
```

### Available methods on the docker container instance

[](#available-methods-on-the-docker-container-instance)

#### Executing a command

[](#executing-a-command)

To execute a command on the container, use the `execute` method.

```
$process = $instance->execute($command);
```

You can execute multiple command in one go by passing an array.

```
$process = $instance->execute([$command, $anotherCommand]);
```

The execute method returns an instance of [`Symfony/Process`](https://symfony.com/doc/current/components/process.html).

You can check if your command ran successfully using the `isSuccessful` $method

```
$process->isSuccessful(); // returns a boolean
```

You can get to the output using `getOutput()`. If the command did not run successfully, you can use `getErrorOutput()`. For more information on how to work with a `Process` head over to [the Symfony docs](https://symfony.com/doc/current/components/process.html).

#### Installing a public key

[](#installing-a-public-key)

If you cant to connect to your container instance via SSH, you probably want to add a public key to it.

This can be done using the `addPublicKey` method.

```
$instance->addPublicKey($pathToPublicKey);
```

It is assumed that the `authorized_keys` file is located in at `/root/.ssh/authorized_keys`. If this is not the case, you can specify the path of that file as a second parameter.

```
$instance->addPublicKey($pathToPublicKey, $pathToAuthorizedKeys);
```

Note that in order to be able to connect via SSH, you should set up a SSH server in your `dockerfile`. Take a look at the `dockerfile` in the tests of this package for an example.

#### Adding files to your instance

[](#adding-files-to-your-instance)

Files can be added to an instance with `addFiles`.

```
$instance->addFiles($fileOrDirectoryOnHost, $pathInContainer);
```

#### Adding other functions on the docker instance

[](#adding-other-functions-on-the-docker-instance)

The `Spatie\Docker\ContainerInstance` class is [macroable](https://github.com/spatie/macroable). This means you can add extra functions to it.

```
Spatie\Docker\DockerContainerInstance::macro('whoAmI', function () {
    $process = $containerInstance->run('whoami');

    return $process->getOutput();
});

$containerInstance = DockerContainer::create($imageName)->start();

$containerInstace->whoAmI(); // returns of name of user in the docker container
```

### Testing

[](#testing)

Before running the tests for the first time, you must build the `spatie/docker` container with:

```
composer build-docker
```

Next, you can run the tests with:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Ruben Van Assche](https://github.com/rubenvanassche)
- [Freek Van der Herten](https://github.com/freekmurze)
- [Alvaro Mercado](https://github.com/Alvar01o)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 60.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~35 days

Recently: every ~14 days

Total

11

Last Release

1930d ago

Major Versions

0.0.1 → 1.0.02020-02-04

PHP version history (2 changes)0.0.1PHP ^7.4

1.5.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b414265a4efdb44ea785ae3b665d467f6759f5f9efcae8f6185a8617bf80c3d9?d=identicon)[Alvar01o](/maintainers/Alvar01o)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (67 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (18 commits)")[![Alvar01o](https://avatars.githubusercontent.com/u/1467463?v=4)](https://github.com/Alvar01o "Alvar01o (7 commits)")[![Galironfydar](https://avatars.githubusercontent.com/u/13006625?v=4)](https://github.com/Galironfydar "Galironfydar (7 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (3 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (3 commits)")[![exileed](https://avatars.githubusercontent.com/u/942898?v=4)](https://github.com/exileed "exileed (1 commits)")[![erjanmx](https://avatars.githubusercontent.com/u/4899432?v=4)](https://github.com/erjanmx "erjanmx (1 commits)")[![WalderlanSena](https://avatars.githubusercontent.com/u/14901080?v=4)](https://github.com/WalderlanSena "WalderlanSena (1 commits)")[![atefBB](https://avatars.githubusercontent.com/u/10966925?v=4)](https://github.com/atefBB "atefBB (1 commits)")[![tomwelch](https://avatars.githubusercontent.com/u/1016558?v=4)](https://github.com/tomwelch "tomwelch (1 commits)")[![fernandokbs](https://avatars.githubusercontent.com/u/17089683?v=4)](https://github.com/fernandokbs "fernandokbs (1 commits)")

---

Tags

spatiedocker

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alvar01o-docker/health.svg)

```
[![Health](https://phpackages.com/badges/alvar01o-docker/health.svg)](https://phpackages.com/packages/alvar01o-docker)
```

###  Alternatives

[spatie/docker

Run a docker container in your PHPUnit tests

478120.2k12](/packages/spatie-docker)[spatie/laravel-webhook-server

Send webhooks in Laravel apps

1.1k8.8M22](/packages/spatie-laravel-webhook-server)[spatie/laravel-horizon-watcher

Automatically restart Horizon when local PHP files change

2631.9M](/packages/spatie-laravel-horizon-watcher)[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[aschmelyun/fleet

Run multiple Laravel Sail websites on your local environment

33269.5k](/packages/aschmelyun-fleet)[ryoluo/sail-ssl

Laravel Sail plugin to enable SSL (HTTPS) connection with Nginx.

188672.6k2](/packages/ryoluo-sail-ssl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
