Skip to content

Commit 7572da3

Browse files
committed
feature #788 Automatically create Docker Compose files when needed (fabpot)
This PR was merged into the 1.13-dev branch. Discussion ---------- Automatically create Docker Compose files when needed If you want to use Docker support in Flex, you must configure it explicitly in a project (via `symfony composer config extra.symfony.docker true` for instance). But that's not enough. You must also create the `docker-compose.yaml` file. And if you do not also create the `docker-compose.override.yaml` file, you will end up with half of the configuration. In this PR, I propose to create those 2 files automatically if they don't exist when a service should be added by a recipe. That way, using Docker support is "only" a matter of opting-in via `composer.json`. /cc `@dunglas` Commits ------- e6c2e40 Automatically create Docker Compose files when needed
2 parents 2aae1ef + e6c2e40 commit 7572da3

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/Configurator/DockerComposeConfigurator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
4343

4444
$rootDir = $this->options->get('root-dir');
4545
foreach ($this->normalizeConfig($config) as $file => $extra) {
46-
if (
47-
(null === $dockerComposeFile = $this->findDockerComposeFile($rootDir, $file)) ||
48-
$this->isFileMarked($recipe, $dockerComposeFile)
49-
) {
46+
$dockerComposeFile = $this->findDockerComposeFile($rootDir, $file);
47+
if (null === $dockerComposeFile) {
48+
$dockerComposeFileName = preg_replace('/\.yml$/', '.yaml', $file);
49+
$dockerComposeFile = $rootDir.'/'.$dockerComposeFileName;
50+
file_put_contents($dockerComposeFile, "version: '3'\n");
51+
$this->write(sprintf(' Created <fg=green>"%s"</>', $dockerComposeFileName));
52+
}
53+
if ($this->isFileMarked($recipe, $dockerComposeFile)) {
5054
continue;
5155
}
5256

5357
$this->write(sprintf('Adding Docker Compose definitions to "%s"', $dockerComposeFile));
5458

55-
$offset = 8;
59+
$offset = 2;
5660
$node = null;
5761
$endAt = [];
5862
$lines = [];

tests/Configurator/DockerComposeConfiguratorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,41 @@ public function testConfigureFileInParentDir()
528528
$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
529529
$this->assertEquals(self::ORIGINAL_CONTENT, file_get_contents($dockerComposeFile));
530530
}
531+
532+
public function testConfigureWithoutExistingDockerComposeFiles()
533+
{
534+
$dockerComposeFile = FLEX_TEST_DIR.'/docker-compose.yaml';
535+
$defaultContent = "version: '3'\n";
536+
537+
$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);
538+
539+
$this->assertStringEqualsFile($dockerComposeFile, $defaultContent.<<<'YAML'
540+
541+
services:
542+
###> doctrine/doctrine-bundle ###
543+
db:
544+
image: mariadb:10.3
545+
environment:
546+
- MYSQL_DATABASE=symfony
547+
# You should definitely change the password in production
548+
- MYSQL_PASSWORD=password
549+
- MYSQL_RANDOM_ROOT_PASSWORD=true
550+
- MYSQL_USER=symfony
551+
volumes:
552+
- db-data:/var/lib/mysql:rw
553+
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
554+
# - ./docker/db/data:/var/lib/mysql:rw
555+
###< doctrine/doctrine-bundle ###
556+
557+
volumes:
558+
###> doctrine/doctrine-bundle ###
559+
db-data: {}
560+
###< doctrine/doctrine-bundle ###
561+
562+
YAML
563+
);
564+
565+
$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
566+
$this->assertEquals(trim($defaultContent), file_get_contents($dockerComposeFile));
567+
}
531568
}

0 commit comments

Comments
 (0)