Skip to content

Commit

Permalink
[docker:init] Add command. (#3839)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Mar 21, 2018
1 parent 85cd754 commit 037dcec
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Command/DockerInitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Drupal\Console\Command;

use Drupal\Console\Core\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\GenerateCommand;
use Drupal\Console\Generator\DockerInitGenerator;
use Symfony\Component\Filesystem\Filesystem;

/**
* Class DockerInitCommand
*
* @package Drupal\Console\Command
*/
class DockerInitCommand extends GenerateCommand
{
/**
* @var DockerInitGenerator
*/
protected $generator;

/**
* InitCommand constructor.
*
* @param DockerInitGenerator $generator
*/
public function __construct(
DockerInitGenerator $generator
) {
$this->generator = $generator;
parent::__construct();
}


/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('docker:init')
->setDescription(
$this->trans('commands.docker.init.description')
)
->setHelp($this->trans('commands.docker.init.description'));
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$fs = new Filesystem();
$dockerComposeFiles = [
$this->drupalFinder->getComposerRoot() . '/docker-compose.yml',
$this->drupalFinder->getComposerRoot() . '/docker-compose.yaml'
];

$dockerComposeFile = $this->validateFileExists(
$fs,
$dockerComposeFiles,
false
);

if (!$dockerComposeFile) {
$dockerComposeFile = $this->drupalFinder->getComposerRoot() . '/docker-compose.yml';
}

$this->backUpFile($fs, $dockerComposeFile);

$parameters = [
'docker_compose_file' => $dockerComposeFile
];

$this->generator->setIo($io);
$this->generator->generate($parameters);
}

}
41 changes: 41 additions & 0 deletions src/Generator/DockerInitGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\Console\Generator;

use Drupal\Console\Core\Generator\Generator;

/**
* Class DockerizeGenerator
*
* @package Drupal\Console\Generator
*/
class DockerInitGenerator extends Generator
{

/**
* {@inheritdoc}
*/
public function generate(array $parameters)
{
$parameters['volume_configuration'] = $this->getVolumeConfiguration();

$dockerComposeFile = $parameters['docker_compose_file'];
unset($parameters['docker_compose_file']);

$this->renderFile(
'files/docker-compose.yml.twig',
$dockerComposeFile,
$parameters
);
}

protected function getVolumeConfiguration() {
$volumeConfiguration = [
'darwin' => ':cached'
];

$osType = strtolower(PHP_OS);

return array_key_exists($osType, $volumeConfiguration)?$volumeConfiguration[$osType]:'';
}
}
73 changes: 73 additions & 0 deletions templates/files/docker-compose.yml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
version: "2.3"

services:
mariadb:
image: wodby/mariadb:10.2-3.0.2
env_file: ./.env
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 'true'
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_USER: ${DATABASE_USER}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
volumes:
- mysqldata:/var/lib/mysql
# Uncomment next line and place DDb dump.sql file(s) here
# - ./mariadb-init:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10

php:
image: wodby/drupal-php:7.0-2.4.3
env_file: ./.env
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: ${DATABASE_HOST}
DB_USER: ${DATABASE_USER}
DB_PASSWORD: ${DATABASE_PASSWORD}
DB_NAME: ${DATABASE_NAME}
DB_DRIVER: mysql
volumes:
- ./:${DRUPAL_ROOT}{{ volume_configuration }}
depends_on:
mariadb:
condition: service_healthy

nginx:
image: wodby/drupal-nginx:8-1.13-2.4.2
env_file: ./.env
depends_on:
- php
environment:
NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: ${SERVER_ROOT}
volumes:
- ./:${DRUPAL_ROOT}{{ volume_configuration }}
labels:
- 'traefik.backend=nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:${HOST_NAME}'

mailhog:
image: mailhog/mailhog
env_file: ./.env
labels:
- 'traefik.backend=mailhog'
- 'traefik.port=8025'
- 'traefik.frontend.rule=Host:mailhog.${HOST_NAME}'

traefik:
image: traefik
env_file: ./.env
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '${HOST_PORT}:80'
volumes:
- /var/run/docker.sock:/var/run/docker.sock

volumes:
mysqldata:
driver: "local"

0 comments on commit 037dcec

Please sign in to comment.