Skip to content

Commit bdc9c8a

Browse files
committed
Fix issues with docker-compose 2.0
1 parent 782be07 commit bdc9c8a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Now you can run `spm services:create` or `spm libraries:create` to create one of
7070
Once completed the library will be automatically registered in the project config and it will be
7171
updated.
7272

73-
By default a new git repository is initialised in the library folder and all files committed to
73+
By default, a new git repository is initialised in the library folder and all files committed to
7474
the master branch.
7575

7676
If you have existing libraries, manually configure them in the `project.yaml` file or, you can

src/Services/DockerManager.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
use Symfony\Component\Dotenv\Dotenv;
1111
use function array_combine;
1212
use function array_filter;
13+
use function array_shift;
1314
use function count;
1415
use function exec;
1516
use function explode;
1617
use function file_get_contents;
1718
use function implode;
19+
use function preg_match;
1820
use function sprintf;
1921
use function trim;
22+
use function version_compare;
2023

2124
/**
2225
* Class DockerManager
@@ -52,6 +55,11 @@ class DockerManager
5255
*/
5356
private $helper;
5457

58+
/**
59+
* @var string
60+
*/
61+
private $version;
62+
5563
/**
5664
* An array of ENV names that must not be passed through to other commands
5765
*
@@ -63,9 +71,18 @@ class DockerManager
6371
'SYMFONY_DOTENV_VARS' => false,
6472
];
6573

74+
private function detectVersion(): void
75+
{
76+
$matches = [];
77+
preg_match('/(\d+.\d+.\d+)$/', $this->helper->run('docker-compose -v'), $matches);
78+
79+
$this->version = $matches[1] ?? '0.0.0';
80+
}
81+
6682
public function bindConsoleHelper(ConsoleHelper $helper): void
6783
{
6884
$this->helper = $helper;
85+
$this->detectVersion();
6986
}
7087

7188
public function resolve(Service $service): void
@@ -74,8 +91,9 @@ public function resolve(Service $service): void
7491
return;
7592
}
7693

94+
$sep = version_compare($this->version, '2.0.0', '>=') ? '-' : '_';
7795
$env = (new Dotenv())->parse(file_get_contents($service->envFile()));
78-
$name = implode('_', array_filter([$env['COMPOSE_PROJECT_NAME'] ?? '', $service->appContainer()]));
96+
$name = implode($sep, array_filter([$env['COMPOSE_PROJECT_NAME'] ?? '', $service->appContainer()]));
7997

8098
try {
8199
$command = sprintf('docker ps --no-trunc --format="{{.ID}}" --filter=name="%s"', $name);
@@ -168,6 +186,17 @@ public function start(Service $service): bool
168186
if ($service->isRunning()) {
169187
return true;
170188
}
189+
// hack for docker-compose v2.0 that doesn't work properly with local contexts
190+
if (version_compare($this->version, '2.0.0', '>=')) {
191+
$ret = array_filter(explode("\n", $this->helper->run(sprintf('docker images "%s*/*"', $service->name()))));
192+
array_shift($ret);
193+
194+
if (count($ret) < 1) {
195+
// pre-build images before attempting to run
196+
// https://github.com/docker/compose/issues/8723
197+
$this->runCommand($service, 'docker-compose build');
198+
}
199+
}
171200

172201
return $this->runCommand($service, 'docker-compose up -d');
173202
}

0 commit comments

Comments
 (0)