-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathImage.php
More file actions
68 lines (57 loc) · 2.09 KB
/
Copy pathImage.php
File metadata and controls
68 lines (57 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* Copyright (c) Default Value LLC.
* This source file is subject to the License https://github.com/DefaultValue/dockerizer_for_php/LICENSE.txt
* Do not change this file if you want to upgrade the tool to the newer versions in the future
* Please, contact us at https://default-value.com/#contact if you wish to customize this tool
* according to you business needs
*/
declare(strict_types=1);
namespace DefaultValue\Dockerizer\Docker;
use DefaultValue\Dockerizer\Shell\Shell;
use Symfony\Component\Process\Exception\ProcessFailedException;
class Image
{
/**
* @param \DefaultValue\Dockerizer\Shell\Shell $shell
*/
public function __construct(private \DefaultValue\Dockerizer\Shell\Shell $shell)
{
}
/**
* @param string $image
* @param bool $skipIfLocalImageExists
* @param bool $mustRun
* @return void
*/
public function pull(string $image, bool $skipIfLocalImageExists = true, bool $mustRun = true): void
{
if ($skipIfLocalImageExists) {
$process = $this->shell->run(['docker', 'images', '--format', '{{.Repository}}:{{.Tag}}']);
$output = $process->getOutput();
if (str_contains($output, $image)) {
return;
}
}
$process = $this->shell->run("docker pull $image", null, [], null, Shell::EXECUTION_TIMEOUT_LONG);
if (
PHP_OS_FAMILY === 'Darwin'
&& !$process->isSuccessful()
&& str_contains($process->getErrorOutput(), 'no matching manifest for')
) {
$dockerArch = trim($this->shell->run('docker version --format {{.Server.Arch}}')->getOutput());
if ($dockerArch === 'arm64') {
$process = $this->shell->run(
"docker pull --platform linux/amd64 $image",
null,
[],
null,
Shell::EXECUTION_TIMEOUT_LONG
);
}
}
if ($mustRun && !$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
}