diff --git a/web/Modules/Docker/App/Models/DockerContainer.php b/web/Modules/Docker/App/Models/DockerContainer.php index 0d194bf..9b617d1 100644 --- a/web/Modules/Docker/App/Models/DockerContainer.php +++ b/web/Modules/Docker/App/Models/DockerContainer.php @@ -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); diff --git a/web/Modules/Docker/DockerContainerApi.php b/web/Modules/Docker/DockerContainerApi.php index 3c3aa55..37c30c0 100644 --- a/web/Modules/Docker/DockerContainerApi.php +++ b/web/Modules/Docker/DockerContainerApi.php @@ -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; diff --git a/web/Modules/Docker/resources/views/actions/docker-compose-simple.txt b/web/Modules/Docker/resources/views/actions/docker-compose-simple.txt new file mode 100644 index 0000000..ac4ca9a --- /dev/null +++ b/web/Modules/Docker/resources/views/actions/docker-compose-simple.txt @@ -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: diff --git a/web/Modules/Docker/resources/views/actions/docker-compose-yml.blade.php b/web/Modules/Docker/resources/views/actions/docker-compose-yml.blade.php new file mode 100644 index 0000000..4feb7f5 --- /dev/null +++ b/web/Modules/Docker/resources/views/actions/docker-compose-yml.blade.php @@ -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 diff --git a/web/Modules/Docker/shell-scripts/install-docker.sh b/web/Modules/Docker/shell-scripts/install-docker.sh index 4621136..f5ec9c1 100644 --- a/web/Modules/Docker/shell-scripts/install-docker.sh +++ b/web/Modules/Docker/shell-scripts/install-docker.sh @@ -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!"