fix docker container env

This commit is contained in:
Bozhidar 2024-04-27 02:50:04 +03:00
parent 9dee42d84b
commit e1ea096ca6
5 changed files with 80 additions and 22 deletions

View file

@ -59,10 +59,20 @@ class DockerContainer extends Model
$model->port = trim($model->port); $model->port = trim($model->port);
$model->external_port = trim($model->external_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 = new DockerContainerApi();
$dockerContainerApi->setImage($model->image); $dockerContainerApi->setImage($model->image);
$dockerContainerApi->setEnvironmentVariables($model->environment_variables); $dockerContainerApi->setEnvironmentVariables($envCleaned);
$dockerContainerApi->setVolumeMapping($model->volume_mapping); $dockerContainerApi->setVolumeMapping($model->volume_mapping);
// $dockerContainerApi->setMemoryLimit($model->memory_limit); // $dockerContainerApi->setMemoryLimit($model->memory_limit);
// $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory); // $dockerContainerApi->setUnlimitedMemory($model->unlimited_memory);

View file

@ -59,33 +59,28 @@ class DockerContainerApi
{ {
$commandId = rand(10000, 99999); $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)) { $dockerContaienrPath = storage_path('docker/'.$this->name);
$shellFileContent .= ' -p ' . $this->externalPort . ':' . $this->port . ' '; if (!is_dir($dockerContaienrPath)) {
shell_exec('mkdir -p ' . $dockerContaienrPath);
} }
$shellFileContent .= '-d ' . $this->image . ' ';
// if (!empty($this->environmentVariables)) { $dockerComposeFile = $dockerContaienrPath . '/docker-compose.yml';
// foreach ($this->environmentVariables as $key => $value) { file_put_contents($dockerComposeFile, $dockerComposeFileContent);
// $shellFileContent .= ' -e ' . $key . '=' . $value . ' ';
// }
// }
//
// if (!empty($this->volumeMapping)) {
// foreach ($this->volumeMapping as $key => $value) {
// $shellFileContent .= ' -v ' . $key . ':' . $value . ' ';
// }
// }
$shellFileContent .= PHP_EOL . 'rm -f /tmp/docker-run-container-'.$commandId.'.sh'; $output = shell_exec("cd $dockerContaienrPath && docker-compose up -d");
file_put_contents('/tmp/docker-run-container-'.$commandId.'.sh', $shellFileContent);
$output = shell_exec('bash /tmp/docker-run-container-'.$commandId.'.sh');
// Get docker container id from output // Get docker container id from output
$dockerContainerId = trim($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); $output = json_decode($output, true);
return $output; return $output;

View file

@ -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:

View file

@ -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

View file

@ -11,6 +11,6 @@ echo \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update 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!" echo "Done!"