Magento Testcontainers v2: 88% Smaller Images, 4.3× Faster Tests
How rebuilding our Magento 2 & Mage-OS Testcontainers from scratch with zstd OCI layers, custom jlink OpenSearch JREs, zero anonymous volumes, and PHP client v2.1 cut CI downloads by 84% and brought 120 integration tests down to 1.45s.

We just shipped v2 of action-build-magento-image, rebuilt all daily images in testcontainer-magento-data, and released v2.1.0 of testcontainer-magento-data-php.
The headline result: you can now spin up a real MySQL 8.4 container pre-loaded with 2,040 Magento 2.4.9 sample-data products, execute a real PDO query, and tear it down in 0.97 seconds (down from 3.01s).
Even better, the entire 120-test suite in magento2-test-essentials, which connects Magento's real ResourceConnection to two live MySQL sample-data containers, now finishes in 1.45 seconds (down from 5.37s, 3.7× faster).
What Changed Under the Hood
1. Minimal FROM scratch Database Images (No Anonymous VOLUME)
In v1, our MariaDB and MySQL images layered pre-initialized /var/lib/mysql data on top of the full upstream base image. That came with two hidden costs:
- Shipping hundreds of megabytes of OS packages and client utilities (
mysql-shell,mysqldump, etc.) that integration tests never execute inside the container. - Inheriting upstream
VOLUME /var/lib/mysql, which forced Docker to copy the entire pre-initialized database directory into a brand-new anonymous volume on every singledocker run, adding ~0.5s beforemysqldormariadbdeven started.
In v2, we initialize the database natively on $BUILDPLATFORM and assemble a minimal FROM scratch runtime containing only mariadbd/mysqld, required shared libraries, a tiny POSIX entrypoint, and /var/lib/mysql, with zero anonymous volumes.
2. Custom jlink OpenSearch 3.0 Image
Upstream OpenSearch images weigh over 1 GiB compressed because they ship the full JDK plus dozens of plugins Magento never uses (opensearch-ml, opensearch-knn, opensearch-sql, opensearch-security, GeoIP databases, and more).
In v2, we build a custom jlink JRE and keep only core OpenSearch plus analysis-icu and analysis-phonetic. OpenSearch 3.0 dropped from 1,006.3 MiB → 120.5 MiB (-88%) and starts ~40% faster.
3. zstd OCI Layer Compression (Level 9)
Switching default OCI layer compression from gzip to zstd level 9 shrank downloads across every image, even cutting media:latest-sampledata in half (142.0 MiB → 71.6 MiB).
Pulling a complete sample-data CI stack (mysql + opensearch + media) now downloads 230.4 MiB instead of 1,407.6 MiB, an 83.6% reduction (6.1× smaller).
4. Faster PHP Client Lifecycle (v2.1.0)
Benchmarking the PHP test suites uncovered two easy wins in ecomdev/testcontainers-magento-data:
WaitForLogdefaulted to a 500 ms poll interval, adding ~250 ms of wasted sleep on every container start. Lowering it to 50 ms eliminated that latency immediately.- Teardown called graceful
stop()(SIGTERM), waiting ~1.2s–3.4s for MySQL InnoDB shutdown on disposable test containers. Overridingstop()to force-remove (containerDelete($id, ['force' => true, 'v' => true])) dropped MySQL teardown to 0.18s.
Benchmark Summary (Magento 2.4.9 Sample Data)
| Metric | v1 Baseline | v2 + PHP 2.1.0 |
Improvement |
|---|---|---|---|
MariaDB 11.8 compressed (amd64) |
114.5 MiB |
18.7 MiB |
-83.7% (6.1×) |
MySQL 8.4 compressed (amd64) |
259.3 MiB |
38.3 MiB |
-85.2% (6.8×) |
OpenSearch 3.0 compressed (amd64) |
1,006.3 MiB |
120.5 MiB |
-88.0% (8.4×) |
Media assets compressed (amd64) |
142.0 MiB |
71.6 MiB |
-49.6% (2.0×) |
MySQL PHP lifecycle (build → query → unset) |
3.01s |
0.97s |
-68.0% (3.1×) |
OpenSearch PHP lifecycle (build → query → unset) |
10.18s |
6.25s |
-38.6% (1.6×) |
magento2-test-essentials (120 tests, ParaTest) |
5.37s |
1.45s |
-73.0% (3.7×) |
IntegrationUtilityTest (Magento DB + MySQL) |
5.27s |
1.23s |
-76.6% (4.3×) |
DbContainerTest (5 MySQL containers) |
22.09s |
8.39s |
-62.0% (2.6×) |
Try It in Your Own Tests
Install the PHP package and spin up a pre-populated Magento 2.4.9 MySQL container in a few lines:
composer require --dev ecomdev/testcontainers-magento-data:^2.1use EcomDev\TestContainers\MagentoData\DbContainerBuilder;
$container = DbContainerBuilder::mysql()
->withMagentoVersion('2.4.9')
->withSampleData()
->build();
$settings = $container->getConnectionSettings();
$pdo = new PDO($settings->dsn(), $settings->user, $settings->password);
$products = $pdo->query('SELECT COUNT(*) FROM catalog_product_entity')->fetchColumn();
// 2040 (ready and torn down in under a second)If you want to build custom data images with your own store's modules and schema, check out our Magento GitHub Actions ([email protected] and [email protected]).
Dealing with a similar bottleneck in production?
Reach out directly to Ivan Chepurnyi for a performance audit, indexer redesign, or second opinion.