Sobes.tech
Junior — Middle

How is interaction between containers organized inside a Docker Compose file?

sobes.tech AI

Answer from AI

In Docker Compose, interaction between containers within a single file occurs through a shared network that Compose creates automatically. All services described in docker-compose.yml are connected to this network and can refer to each other by service name as a hostname.

For example, if you have services web and db, then the web container can connect to the database using the hostname db and the appropriate port.

Example:

services:
  web:
    build: ./web
    ports:
      - "8000:8000"
  db:
    image: postgres

In the application code, web can use db:5432 to connect to the database.

Thus, Docker Compose provides convenient network interaction between containers without additional configuration.