1. Go to this page and download the library: Download diego-ninja/docker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
diego-ninja / docker example snippets
use Ninja\Docker\Docker;
// Start a Nginx container on port 8080
$container = Docker::image('nginx:latest')
->port(8080, 80)
->name('my-nginx')
->start();
echo "Container started. IP: " . $container->getIp() . "\n";
// Execute a command inside the container
$output = $container->execute('ls -la /usr/share/nginx/html');
echo $output;
// Stop and remove the container
$container->stop();
// Before (and still works)
(new DockerContainer('nginx:latest'))->port(8080, 80)->start();
// Now (recommended)
Docker::image('nginx:latest')->port(8080, 80)->start();
// Encourages using Value Objects for clarity and safety
use Ninja\Docker\ValueObjects\HostPath;
use Ninja\Docker\ValueObjects\ContainerPath;
Docker::image('...')->volume(
HostPath::from(__DIR__ . '/content'),
ContainerPath::from('/usr/share/nginx/html')
);
// Nginx on port 80
Docker::nginx()->start();
// MySQL 8 with a root password
Docker::mysql(['password' => 'secret'])->start();
// PostgreSQL 16 with a password
Docker::postgres(['password' => 'secret', 'database' => 'my_app'])->start();
// Redis
Docker::redis()->start();
// MySQL with a database, user, and persistent data directory
Docker::mysql([
'password' => 'root_secret',
'database' => 'my_app',
'user' => 'app_user',
'user_password' => 'user_pass',
'data_dir' => '/path/on/host/for/data', // Volume mount for persistence
'port' => 3307, // Custom host port
])->start();
Docker::container('alpine:latest')
->command('sleep', '300') // Keeps the container running
->start();
// Register a service once
Docker::register('mailhog', [
'image' => 'mailhog/mailhog',
'ports' => [1025 => 1025, 8025 => 8025],
'name_prefix' => 'mailhog',
]);
// Use it anywhere in your code
Docker::mailhog()->start();
Docker::mysql(['password' => 'secret'])
->port(3307, 3306) // Override the default port
->namedVolume('mysql-data', '/var/lib/mysql') // Use a named volume
->network('backend') // Add to a network
->start();
->port(8080, 80)
use Ninja\Docker\ValueObjects\HostPath;
use Ninja\Docker\ValueObjects\ContainerPath;
->volume(HostPath::from('/path/on/host'), ContainerPath::from('/path/in/container'))
use Ninja\Docker\ValueObjects\VolumeName;
use Ninja\Docker\ValueObjects\ContainerPath;
->namedVolume(VolumeName::from('my-volume'), ContainerPath::from('/path/in/container'))
$container = Docker::nginx()->start();
// Get the container's IP address
$ip = $container->getIp();
// Execute a command
$output = $container->execute('whoami'); // returns "root"
// Get the container ID
$id = $container->getContainerId();
// Check if it's running
if ($container->isRunning()) {
// ...
}
// Stop the container
$container->stop();
$container = DockerContainerInstance::fromExisting('my-nginx');
$container->execute('echo "Hello from an existing container"');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.