Sobes.tech
Junior

What is your experience with Docker Compose?

sobes.tech AI

Answer from AI

Docker Compose is used for defining and running multi-container applications. The experience includes:

  • Developing a local environment: Creating a docker-compose.yml to run all necessary services (backend, frontend, database, cache) for local development. This allows for quickly setting up an environment isolated from the main system.
  • Testing: Using Docker Compose to create test environments with different service configurations.
  • Deployment to Staging/Production: Although more powerful orchestrators (Kubernetes) are often used for Production, Docker Compose was used in early stages or for small projects to deploy on Staging servers.

Examples of fields in docker-compose.yml:

  • services: Defining a set of containers.
  • image: Specifying the Docker image used.
  • build: Building an image from a Dockerfile.
  • ports: Mapping ports.
  • volumes: Mounting volumes for data persistence or source code mounting.
  • networks: Defining custom networks.
  • depends_on: Setting dependencies between services.
  • environment: Passing environment variables into containers.

A simple configuration example for a web application and database:

services:
  webapp:
    build: ./webapp # Building from Dockerfile in the webapp directory
    ports:
      - "80:80" # Mapping host port 80 to container port 80
    environment:
      DATABASE_URL: postgresql://user:password@db:5432/mydb # Passing environment variable
    depends_on:
      - db # Web app depends on the database

  db:
    image: postgres:13 # Using PostgreSQL image
    volumes:
      - db_data:/var/lib/postgresql/data # Persisting database data
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb

volumes:
  db_data: # Named volume definition

Common commands:

docker compose up -d # Run services in detached mode
docker compose down # Stop and remove services
docker compose build # Build images
docker compose logs <service_name> # View logs of a specific service
docker compose exec <service_name> <command> # Execute a command inside a container

Familiar with Compose syntax versions and their differences. Understands how to use .env files for managing environment variables and how to apply profiles for running different sets of services.