0
0
Dockerdevops~5 mins

Why advanced Compose features matter in Docker - Why It Works

Choose your learning style9 modes available
Introduction
Docker Compose helps run multiple containers together easily. Advanced features let you handle complex setups like linking services, managing networks, and sharing data smoothly.
When you want to run a web app with a database and cache together without conflicts
When you need to share files between containers for data processing
When you want to control how containers restart automatically after failure
When you want to isolate containers using custom networks for security
When you want to scale services up or down easily with one command
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    volumes:
      - web-data:/usr/share/nginx/html
    networks:
      - front-net
    restart: always
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: exampledb
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - back-net
    restart: on-failure
volumes:
  web-data:
  db-data:
networks:
  front-net:
  back-net:

This file defines two services: web and db. The web service uses the nginx image and shares a volume for website files. It connects to a network called front-net and restarts always if it stops. The db service uses the postgres image with environment variables for setup, stores data in a volume, connects to back-net, and restarts only on failure. Volumes keep data safe outside containers. Networks separate traffic between web and database for security.

Commands
Starts all services defined in the docker-compose.yml file in detached mode so they run in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "example_front-net" with driver "bridge" Creating network "example_back-net" with driver "bridge" Creating volume "example_web-data" with default driver Creating volume "example_db-data" with default driver Creating example_db_1 ... done Creating example_web_1 ... done
-d - Run containers in the background (detached mode)
Shows the status of all running services to verify they started correctly.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- example_db_1 docker-entrypoint.sh postgres Up example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
Displays the logs of the web service to check for errors or confirm it is running well.
Terminal
docker-compose logs web
Expected OutputExpected
example_web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration example_web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ example_web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh example_web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf example_web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf example_web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
Stops and removes all containers, networks, and volumes created by docker-compose up to clean up the environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping example_web_1 ... done Stopping example_db_1 ... done Removing example_web_1 ... done Removing example_db_1 ... done Removing network example_front-net Removing network example_back-net
Key Concept

If you remember nothing else from this pattern, remember: advanced Compose features let you organize, connect, and keep your containers running smoothly together.

Common Mistakes
Not defining volumes for databases or data storage
Data will be lost when containers stop or are removed
Always use volumes to persist important data outside containers
Using the default network for all services without separation
Services can access each other freely, which can cause security risks
Create separate networks to isolate services that should not communicate
Running docker-compose up without -d and closing the terminal
Containers stop when the terminal session ends
Use -d flag to run containers in the background
Summary
Use docker-compose.yml to define multiple services, volumes, and networks for your app.
Run 'docker-compose up -d' to start all services in the background.
Check running services with 'docker-compose ps' and logs with 'docker-compose logs'.
Clean up with 'docker-compose down' to stop and remove all resources.