10
10
use Symfony \Component \Dotenv \Dotenv ;
11
11
use function array_combine ;
12
12
use function array_filter ;
13
+ use function array_shift ;
13
14
use function count ;
14
15
use function exec ;
15
16
use function explode ;
16
17
use function file_get_contents ;
17
18
use function implode ;
19
+ use function preg_match ;
18
20
use function sprintf ;
19
21
use function trim ;
22
+ use function version_compare ;
20
23
21
24
/**
22
25
* Class DockerManager
@@ -52,6 +55,11 @@ class DockerManager
52
55
*/
53
56
private $ helper ;
54
57
58
+ /**
59
+ * @var string
60
+ */
61
+ private $ version ;
62
+
55
63
/**
56
64
* An array of ENV names that must not be passed through to other commands
57
65
*
@@ -63,9 +71,18 @@ class DockerManager
63
71
'SYMFONY_DOTENV_VARS ' => false ,
64
72
];
65
73
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
+
66
82
public function bindConsoleHelper (ConsoleHelper $ helper ): void
67
83
{
68
84
$ this ->helper = $ helper ;
85
+ $ this ->detectVersion ();
69
86
}
70
87
71
88
public function resolve (Service $ service ): void
@@ -74,8 +91,9 @@ public function resolve(Service $service): void
74
91
return ;
75
92
}
76
93
94
+ $ sep = version_compare ($ this ->version , '2.0.0 ' , '>= ' ) ? '- ' : '_ ' ;
77
95
$ 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 ()]));
79
97
80
98
try {
81
99
$ command = sprintf ('docker ps --no-trunc --format="{{.ID}}" --filter=name="%s" ' , $ name );
@@ -168,6 +186,17 @@ public function start(Service $service): bool
168
186
if ($ service ->isRunning ()) {
169
187
return true ;
170
188
}
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
+ }
171
200
172
201
return $ this ->runCommand ($ service , 'docker-compose up -d ' );
173
202
}
0 commit comments