mirror of
https://github.com/PhyreApps/PhyrePanel.git
synced 2024-11-21 23:20:24 +00:00
fix docker container env
This commit is contained in:
parent
9dee42d84b
commit
e1ea096ca6
5 changed files with 80 additions and 22 deletions
|
@ -59,10 +59,20 @@ class DockerContainer extends Model
|
|||
|
||||
$model->port = trim($model->port);
|
||||
$model->external_port = trim($model->external_port);
|
||||
$envCleaned = [];
|
||||
if (!empty($model->environment_variables)) {
|
||||
foreach ($model->environment_variables as $envKey=>$envValue) {
|
||||
$envKey = trim($envKey);
|
||||
$envKey = str_replace("\t", ' ', $envKey);
|
||||
$envValue = trim($envValue);
|
||||
$envValue = str_replace("\t", ' ', $envValue);
|
||||
$envCleaned[$envKey] = $envValue;
|
||||
}
|
||||
}
|
||||
|
||||
$dockerContainerApi = new DockerContainerApi();
|
||||
$dockerContainerApi->setImage($model->image);
|
||||
$dockerContainerApi->setEnvironmentVariables($model->environment_variables);
|
||||
$dockerContainerApi->setEnvironmentVariables($envCleaned);
|
||||
$dockerContainerApi->setVolumeMapping($model->volume_mapping);
|
||||
// $dockerContainerApi->setMemoryLimit($model->memory_limit);
|
||||
// $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory);
|
||||
|
|
|
@ -59,33 +59,28 @@ class DockerContainerApi
|
|||
{
|
||||
$commandId = rand(10000, 99999);
|
||||
|
||||
$shellFileContent = 'docker run --name ' . $this->name . ' ';
|
||||
$dockerComposeFileContent = view('docker::actions.docker-compose-yml', [
|
||||
'name' => $this->name,
|
||||
'image' => $this->image,
|
||||
'port' => $this->port,
|
||||
'externalPort' => $this->externalPort,
|
||||
'environmentVariables' => $this->environmentVariables,
|
||||
'volumeMapping' => $this->volumeMapping,
|
||||
])->render();
|
||||
|
||||
if (!empty($this->port)) {
|
||||
$shellFileContent .= ' -p ' . $this->externalPort . ':' . $this->port . ' ';
|
||||
$dockerContaienrPath = storage_path('docker/'.$this->name);
|
||||
if (!is_dir($dockerContaienrPath)) {
|
||||
shell_exec('mkdir -p ' . $dockerContaienrPath);
|
||||
}
|
||||
$shellFileContent .= '-d ' . $this->image . ' ';
|
||||
|
||||
// if (!empty($this->environmentVariables)) {
|
||||
// foreach ($this->environmentVariables as $key => $value) {
|
||||
// $shellFileContent .= ' -e ' . $key . '=' . $value . ' ';
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (!empty($this->volumeMapping)) {
|
||||
// foreach ($this->volumeMapping as $key => $value) {
|
||||
// $shellFileContent .= ' -v ' . $key . ':' . $value . ' ';
|
||||
// }
|
||||
// }
|
||||
$dockerComposeFile = $dockerContaienrPath . '/docker-compose.yml';
|
||||
file_put_contents($dockerComposeFile, $dockerComposeFileContent);
|
||||
|
||||
$shellFileContent .= PHP_EOL . 'rm -f /tmp/docker-run-container-'.$commandId.'.sh';
|
||||
|
||||
file_put_contents('/tmp/docker-run-container-'.$commandId.'.sh', $shellFileContent);
|
||||
$output = shell_exec('bash /tmp/docker-run-container-'.$commandId.'.sh');
|
||||
$output = shell_exec("cd $dockerContaienrPath && docker-compose up -d");
|
||||
|
||||
// Get docker container id from output
|
||||
$dockerContainerId = trim($output);
|
||||
$output = shell_exec('docker ps --format json --filter id='.$dockerContainerId);
|
||||
$output = shell_exec('docker ps --format json --filter name='.$this->name);
|
||||
$output = json_decode($output, true);
|
||||
|
||||
return $output;
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
version: '3.1'
|
||||
|
||||
services:
|
||||
|
||||
wordpress:
|
||||
image: wordpress
|
||||
restart: always
|
||||
ports:
|
||||
- 8080:80
|
||||
environment:
|
||||
WORDPRESS_DB_HOST: db
|
||||
WORDPRESS_DB_USER: exampleuser
|
||||
WORDPRESS_DB_PASSWORD: examplepass
|
||||
WORDPRESS_DB_NAME: exampledb
|
||||
volumes:
|
||||
- wordpress:/var/www/html
|
||||
|
||||
db:
|
||||
image: mysql:8.0
|
||||
restart: always
|
||||
environment:
|
||||
MYSQL_DATABASE: exampledb
|
||||
MYSQL_USER: exampleuser
|
||||
MYSQL_PASSWORD: examplepass
|
||||
MYSQL_RANDOM_ROOT_PASSWORD: '1'
|
||||
volumes:
|
||||
- db:/var/lib/mysql
|
||||
|
||||
volumes:
|
||||
wordpress:
|
||||
db:
|
|
@ -0,0 +1,22 @@
|
|||
@if(isset($version))
|
||||
version: '{{$version}}'
|
||||
@endif
|
||||
|
||||
services:
|
||||
|
||||
{{$name}}:
|
||||
image: {{$image}}
|
||||
restart: always
|
||||
ports:
|
||||
- {{$externalPort}}:{{ $port }}
|
||||
|
||||
@if(isset($environmentVariables))
|
||||
|
||||
environment:
|
||||
|
||||
@foreach($environmentVariables as $key => $value)
|
||||
|
||||
{{$key}}: {{$value}}
|
||||
|
||||
@endforeach
|
||||
@endif
|
|
@ -11,6 +11,6 @@ echo \
|
|||
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||
sudo apt-get update
|
||||
|
||||
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
|
||||
sudo apt-get install docker-compose docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
|
||||
|
||||
echo "Done!"
|
||||
|
|
Loading…
Reference in a new issue