Skip to content

Commit 43995c9

Browse files
committed
Add compatibility with Docker environment variables (and so docker-machine)
1 parent 832ed19 commit 43995c9

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/Command/SynchronizeHostsCommand.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ protected function configure()
1717
$this
1818
->setName('synchronize-hosts')
1919
->setDescription('Run the application')
20-
->addOption(
21-
'entrypoint',
22-
'p',
23-
InputOption::VALUE_REQUIRED,
24-
'The docker entrypoint',
25-
getenv('DOCKER_ENTRYPOINT') ?: 'unix:///var/run/docker.sock'
26-
)
2720
->addOption(
2821
'hosts_file',
2922
'f',
@@ -43,14 +36,43 @@ protected function configure()
4336

4437
protected function execute(InputInterface $input, OutputInterface $output)
4538
{
46-
$client = new DockerClient([], $input->getOption('entrypoint'));
47-
4839
$app = new Synchronizer(
49-
new Docker($client),
40+
new Docker($this->createDockerClient()),
5041
$input->getOption('hosts_file'),
5142
$input->getOption('tld')
5243
);
5344

5445
$app->run();
5546
}
47+
48+
/**
49+
* Copy of https://github.com/hxpro/docker-php/blob/master/src/Docker/Http/DockerClient.php
50+
* + disable peer_name check.
51+
*
52+
* @return DockerClient
53+
*/
54+
private function createDockerClient()
55+
{
56+
$entrypoint = getenv('DOCKER_HOST') ? getenv('DOCKER_HOST') : 'unix:///var/run/docker.sock';
57+
$context = null;
58+
$useTls = false;
59+
if (getenv('DOCKER_TLS_VERIFY') && getenv('DOCKER_TLS_VERIFY') == 1) {
60+
if (!getenv('DOCKER_CERT_PATH')) {
61+
throw new \RuntimeException('Connection to docker has been set to use TLS, but no PATH is defined for certificate in DOCKER_CERT_PATH docker environment variable');
62+
}
63+
$useTls = true;
64+
$cafile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'ca.pem';
65+
$certfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'cert.pem';
66+
$keyfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'key.pem';
67+
$context = stream_context_create([
68+
'ssl' => [
69+
'cafile' => $cafile,
70+
'local_cert' => $certfile,
71+
'local_pk' => $keyfile,
72+
],
73+
]);
74+
}
75+
76+
return new DockerClient([], $entrypoint, $context, $useTls);
77+
}
5678
}

0 commit comments

Comments
 (0)