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