mark.sagikazar
mark.sagikazar

Reputation: 1042

Recompile Symfony container manually

I am trying to create a dockerized symfony application. The problem is that the DI container gets built during the Docker container build which means I can't really inject runtime parameters into it. So far my solution was to clear the cache in the container entrypoint, but I realized that in some cases this might be a quite heavy operation, so I created a custom compilation function in the AppKernel based on the kernel's boot function:

/**
 * Recompiles the container without warming up the whole cache.
 *
 * Can be called upon docker container start to inject custom parameters.
 */
public function compile()
{
    // Load class cache
    if ($this->loadClassCache) {
        $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
    }

    // Initialize bundles to be able to parse configurations
    $this->initializeBundles();

    $class = $this->getContainerClass();
    $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);

    $container = $this->buildContainer();
    $container->compile();
    $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
}

This function would now be called upon each Docker container start (before the application starts).

Is this a safe operation? Should I assume that any cache warmers might rely on the container parameters? (Since I only change the container parameters runtime, the services and everything else should remain the same).

Originally asked in a symfony repo issue: https://github.com/symfony/symfony/issues/19525

PR to my custom repository: https://github.com/webplates/symfony-standard/pull/42

Upvotes: 4

Views: 1422

Answers (1)

aferber
aferber

Reputation: 1151

This can't be answered in general, it completely depends on the bundles you use and the parameters that are changed. For example, if your routing depends on a parameter (to set a host on a route for example), it is not enough to only rebuild the container, a warm up is needed to rebuild the routing.

Upvotes: 0

Related Questions