Skip to content

Commit

Permalink
Fix issues with docker-compose 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-redfern committed Oct 14, 2021
1 parent 782be07 commit bdc9c8a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Now you can run `spm services:create` or `spm libraries:create` to create one of
Once completed the library will be automatically registered in the project config and it will be
updated.

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

If you have existing libraries, manually configure them in the `project.yaml` file or, you can
Expand Down
31 changes: 30 additions & 1 deletion src/Services/DockerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use Symfony\Component\Dotenv\Dotenv;
use function array_combine;
use function array_filter;
use function array_shift;
use function count;
use function exec;
use function explode;
use function file_get_contents;
use function implode;
use function preg_match;
use function sprintf;
use function trim;
use function version_compare;

/**
* Class DockerManager
Expand Down Expand Up @@ -52,6 +55,11 @@ class DockerManager
*/
private $helper;

/**
* @var string
*/
private $version;

/**
* An array of ENV names that must not be passed through to other commands
*
Expand All @@ -63,9 +71,18 @@ class DockerManager
'SYMFONY_DOTENV_VARS' => false,
];

private function detectVersion(): void
{
$matches = [];
preg_match('/(\d+.\d+.\d+)$/', $this->helper->run('docker-compose -v'), $matches);

$this->version = $matches[1] ?? '0.0.0';
}

public function bindConsoleHelper(ConsoleHelper $helper): void
{
$this->helper = $helper;
$this->detectVersion();
}

public function resolve(Service $service): void
Expand All @@ -74,8 +91,9 @@ public function resolve(Service $service): void
return;
}

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

try {
$command = sprintf('docker ps --no-trunc --format="{{.ID}}" --filter=name="%s"', $name);
Expand Down Expand Up @@ -168,6 +186,17 @@ public function start(Service $service): bool
if ($service->isRunning()) {
return true;
}
// hack for docker-compose v2.0 that doesn't work properly with local contexts
if (version_compare($this->version, '2.0.0', '>=')) {
$ret = array_filter(explode("\n", $this->helper->run(sprintf('docker images "%s*/*"', $service->name()))));
array_shift($ret);

if (count($ret) < 1) {
// pre-build images before attempting to run
// https://github.com/docker/compose/issues/8723
$this->runCommand($service, 'docker-compose build');
}
}

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

0 comments on commit bdc9c8a

Please sign in to comment.