@@ -254,56 +254,74 @@ Docker Integration
254254------------------
255255
256256The local Symfony server provides full `Docker `_ integration for projects that
257- use it. First, make sure to expose the container ports:
257+ use it.
258+
259+ When the web server detects that Docker Compose is running for the project, it
260+ automatically exposes environment variables according to the exposed port and
261+ the name of the ``docker-compose `` services.
262+
263+ Consider the following configuration:
258264
259265.. code-block :: yaml
260266
261- # docker-compose.override. yaml
267+ # docker-compose.yaml
262268 services :
263269 database :
264- ports :
265- - " 3306"
270+ ports : [3306]
266271
267- redis :
268- ports :
269- - " 6379"
272+ The web server detects that a service exposing port ``3306 `` is running for the
273+ project. It understands that this is a MySQL service and creates environment
274+ variables accordingly with the service name (``database ``) as a prefix:
275+ ``DATABASE_URL ``, ``DATABASE_HOST ``, ...
270276
271- # ...
272-
273- Then, check your service names and update them if needed (Symfony creates
274- environment variables following the name of the services so they can be
275- autoconfigured):
277+ If the ``docker-compose.yaml `` names do not match Symfony's conventions, add a
278+ label to override the environment variables prefix:
276279
277280.. code-block :: yaml
278281
279282 # docker-compose.yaml
280283 services :
281- # DATABASE_URL
282- database : ...
283- # MONGODB_DATABASE, MONGODB_SERVER
284- mongodb : ...
285- # REDIS_URL
286- redis : ...
287- # ELASTISEARCH_HOST, ELASTICSEARCH_PORT
288- elasticsearch : ...
289- # RABBITMQ_DSN
290- rabbitmq : ...
291-
292- If your ``docker-compose.yaml `` file doesn't use the environment variable names
293- expected by Symfony (e.g. you use ``MYSQL_URL `` instead of ``DATABASE_URL ``)
294- then you need to rename all occurrences of those environment variables in your
295- Symfony application. A simpler alternative is to use the ``.env.local `` file to
296- reassign the environment variables:
297-
298- .. code-block :: bash
299-
300- # .env.local
301- DATABASE_URL=${MYSQL_URL}
302- # ...
303-
304- Now you can start the containers and all their services will be exposed. Browse
305- any page of your application and check the "Symfony Server" section in the web
306- debug toolbar. You'll see that "Docker Compose" is "Up".
284+ db :
285+ ports : [3306]
286+ labels :
287+ com.symfony.server.service-prefix : ' DATABASE'
288+
289+ In this example, the service is named ``db ``, so environment variables would be
290+ prefixed with ``DB_ ``, but as the ``com.symfony.server.service-prefix `` is set
291+ to ``DATABASE ``, the web server creates environment variables starting with
292+ ``DATABASE_ `` instead as expected by the default Symfony configuration.
293+
294+ Here is the list of supported services with their ports and default Symfony
295+ prefixes:
296+
297+ ============= ===== ======================
298+ Service Port Symfony default prefix
299+ ============= ===== ======================
300+ MySQL 3306 ``DATABASE_ ``
301+ PostgreSQL 5432 ``DATABASE_ ``
302+ Redis 6379 ``REDIS_ ``
303+ RabbitMQ 5672 ``RABBITMQ_ `` (set user and pass via Docker ``RABBITMQ_DEFAULT_USER `` and ``RABBITMQ_DEFAULT_PASS `` env var)
304+ ElasticSearch 9200 ``ELASTICSEARCH_ ``
305+ MongoDB 27017 ``MONGODB_ `` (set the database via a Docker ``MONGO_DATABASE `` env var)
306+ Kafka 9092 ``KAFKA_ ``
307+ ============= ===== ======================
308+
309+ .. tip ::
310+
311+ To debug and list all exported environment variables, run ``symfony
312+ var:export ``.
313+
314+ .. tip ::
315+
316+ For some services, the web server also exposes environment variables
317+ understood by CLI tools related to the service. For instance, running
318+ ``symfony run psql `` will connect you automatically to the PostgreSQL server
319+ running in a container without having to specify the username, password, or
320+ database name.
321+
322+ When Docker services are running, browse a page of your Symfony application and
323+ check the "Symfony Server" section in the web debug toolbar; you'll see that
324+ "Docker Compose" is "Up".
307325
308326SymfonyCloud Integration
309327------------------------
0 commit comments