PHP code example of tiny-blocks / docker-container
1. Go to this page and download the library: Download tiny-blocks/docker-container 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/ */
tiny-blocks / docker-container example snippets
use TinyBlocks\DockerContainer\GenericDockerContainer;
$container = GenericDockerContainer::from(image: 'php:8.5-fpm', name: 'my-container');
$container->run();
$container->run(commands: ['ls', '-la']);
use TinyBlocks\DockerContainer\Waits\ContainerWaitForTime;
$container->run(commands: ['ls', '-la'], waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 5));
$container->runIfNotExists();
use TinyBlocks\DockerContainer\MySQLDockerContainer;
use TinyBlocks\DockerContainer\FlywayDockerContainer;
$mysql = MySQLDockerContainer::from(image: 'mysql:8.4', name: 'my-database')
->pullImage()
->withRootPassword(rootPassword: 'root');
$flyway = FlywayDockerContainer::from(image: 'flyway/flyway:12-alpine')
->pullImage()
->withMigrations(pathOnHost: '/path/to/migrations');
# Both images are downloading in the background.
# MySQL pull completes here, container starts and becomes ready.
$mySQLStarted = $mysql->runIfNotExists();
# Flyway pull already finished while MySQL was starting.
$flyway->withSource(container: $mySQLStarted, username: 'root', password: 'root')
->cleanAndMigrate();
$container->withNetwork(name: 'my-network');
$container->withPortMapping(portOnHost: 8080, portOnContainer: 80);
$ports = $started->getAddress()->getPorts();
$ports->firstExposedPort(); # 80 (container-internal)
$ports->firstHostPort(); # 8080 (host-accessible)
$container->withVolumeMapping(pathOnHost: '/host/data', pathOnContainer: '/container/data');
$container->withEnvironmentVariable(key: 'APP_ENV', value: 'testing');
$container->withoutAutoRemove();
$container->copyToContainer(pathOnHost: '/path/to/files', pathOnContainer: '/path/in/container');
$started = $container->run();
$result = $started->stop();
$result = $started->stop(timeoutInWholeSeconds: 60);
$started = $container->run();
$started->stopOnShutdown();
$started = $container->run();
$result = $started->executeAfterStarted(commands: ['php', '-v']);
$result->getOutput();
$result->isSuccessful();
use TinyBlocks\DockerContainer\Waits\ContainerWaitForTime;
$container->withWaitBeforeRun(wait: ContainerWaitForTime::forSeconds(seconds: 3));
use TinyBlocks\DockerContainer\GenericDockerContainer;
use TinyBlocks\DockerContainer\MySQLDockerContainer;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForDependency;
use TinyBlocks\DockerContainer\Waits\Conditions\MySQLReady;
$mySQLStarted = MySQLDockerContainer::from(image: 'mysql:8.4')
->withRootPassword(rootPassword: 'root')
->run();
$container = GenericDockerContainer::from(image: 'my-app:latest')
->withWaitBeforeRun(
wait: ContainerWaitForDependency::untilReady(
condition: MySQLReady::from(container: $mySQLStarted),
timeoutInSeconds: 30
)
)
->run();
use TinyBlocks\DockerContainer\MySQLDockerContainer;
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.4', name: 'my-database')
->withTimezone(timezone: 'America/Sao_Paulo')
->withUsername(user: 'app_user')
->withPassword(password: 'secret')
->withDatabase(database: 'my_database')
->withPortMapping(portOnHost: 3306, portOnContainer: 3306)
->withRootPassword(rootPassword: 'root')
->withGrantedHosts()
->run();
use TinyBlocks\DockerContainer\MySQLDockerContainer;
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.4', name: 'my-database')
->withRootPassword(rootPassword: 'root')
->withReadinessTimeout(timeoutInSeconds: 60)
->run();
$address = $mySQLContainer->getAddress();
$ip = $address->getIp();
$hostname = $address->getHostname();
$ports = $address->getPorts();
$containerPort = $ports->firstExposedPort(); # e.g. 3306 (container-internal)
$hostPort = $ports->firstHostPort(); # e.g. 49153 (host-accessible)
$environmentVariables = $mySQLContainer->getEnvironmentVariables();
$database = $environmentVariables->getValueBy(key: 'MYSQL_DATABASE');
$username = $environmentVariables->getValueBy(key: 'MYSQL_USER');
$password = $environmentVariables->getValueBy(key: 'MYSQL_PASSWORD');
$jdbcUrl = $mySQLContainer->getJdbcUrl();
use TinyBlocks\DockerContainer\MySQLDockerContainer;
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.4', name: 'my-database')
->withRootPassword(rootPassword: 'root')
->withDatabase(database: 'my_database')
->withPortMapping(portOnHost: 3306, portOnContainer: 3306);
$started = $mySQLContainer->runIfNotExists();
$address = $started->getAddress();
$host = $address->getHostForConnection(); # hostname inside Docker, 127.0.0.1 on host
$port = $address->getPorts()->getPortForConnection(); # container port inside Docker, host-mapped port on host
use TinyBlocks\DockerContainer\FlywayDockerContainer;
$flywayContainer = FlywayDockerContainer::from(image: 'flyway/flyway:12-alpine')
->withNetwork(name: 'my-network')
->withMigrations(pathOnHost: '/path/to/migrations')
->withSource(container: $mySQLStarted, username: 'root', password: 'root');
$flywayContainer
->withSource(container: $mySQLStarted, username: 'root', password: 'root')
->withSchema(schema: 'custom_schema')
->withTable(table: 'custom_history');
$flywayContainer->withMigrations(pathOnHost: '/path/to/migrations');
$flywayContainer->migrate();
$flywayContainer->cleanAndMigrate();
use TinyBlocks\DockerContainer\MySQLDockerContainer;
use TinyBlocks\DockerContainer\FlywayDockerContainer;
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.4', name: 'test-database')
->pullImage()
->withNetwork(name: 'my-network')
->withTimezone(timezone: 'America/Sao_Paulo')
->withPassword(password: 'secret')
->withDatabase(database: 'test_adm')
->withRootPassword(rootPassword: 'root')
->withGrantedHosts();
$flywayContainer = FlywayDockerContainer::from(image: 'flyway/flyway:12-alpine')
->pullImage()
->withNetwork(name: 'my-network')
->withMigrations(pathOnHost: '/path/to/migrations')
->withCleanDisabled(disabled: false)
->withConnectRetries(retries: 5)
->withValidateMigrationNaming(enabled: true);
$mySQLStarted = $mySQLContainer->runIfNotExists();
$mySQLStarted->stopOnShutdown();
$flywayContainer
->withSource(container: $mySQLStarted, username: 'root', password: 'root')
->cleanAndMigrate();