Docker Compose

    Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience.

    Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single YAML configuration file. Then, with a single command, you create and start all the services from your configuration file.

    Docker compose architecture

     

    Commands#

    Before running these commands, navigate to the folder containing your configuration/compose files.

    • Start services
      • Build (if required) and start all services in foreground. 
        docker compose up
      • Start services in detached mode (recommended for normal usage). 
        docker compose up -d
      • Build specific file if multiple compose files are present in single folder. 
        docker compose -f <file-name.yml> up
         
    • Stop services
      • Stop running containers without removing them. 
        docker compose stop
      • Stop and remove containers, networks, and default resources. 
        docker compose down
         
    • Rebuild Service
      • Rebuild service images without starting containers. Useful when Dockerfile changes or Dependencies updated. 
        docker compose build
      • Rebuild images and starts services in one command, recommended after code or configuration changes. 
        docker compose up --build
         
    • Check Service Status
      • Displays the status of all services like container name, state(up/expires), ports, etc., 
        docker compose ps
         
    • View logs
      • Stream logs from all running services in real time. 
        docker compose logs -f
      • Stream logs for a specific service. 
        docker compose logs -f <service-name>
         
    • Run a One-Off Command
      • Runs a temporary container to execute a command 
        docker compose run <service-name> <command>

    Docker Compose File – Syntax & Components#

    1. filename:

    We can create separate docker compose files for different services, applications, databases, etc., 

    <type-name>.docker-compose.yml 

    2. version:

    Specifies the compose file format version. Optional in newer Compose(v2+)

    version: “3.9”

    3. services:

    Defines the containers that make up your application. Each key under services: represents one container.

    services: key-name: image: #Specifies a pre-built image from Docker Hub or a registry. env_file: #used to store and inject secrets/passwords(optional). - .env build: #Builds an image from a Dockerfile instead of pulling one.(skip if pulling an image) context: . #Build directory dockerfile: #Custom Dockerfile name container_name: #Assigns a custom container name instead of auto-generated names. depends_on: #Controls startup order between services. Note: It does NOT wait for the service to be fully ready. environment: #Sets environment variables inside the container. ports: "HOST:CONTAINER" #Maps host machine port to container port. Used for port-forwarding networks: #Connects service to custom networks for inter-container communication. volumes: "HOST_PATH:CONTAINER_PATH" #Mounts files or directories into container. Used for persistent data restart: [no|always|on-failure|unless-stopped] #Defines container restart policy. healthcheck: #Defines container health monitoring logic. test: interval: timeout: retries:

    4. volumes: 

    Defines persistent volumes managed by Docker.

    volumes: mysql_data:

    5. networks:

    Defines custom networks for service isolation and communication.

    networks: backend: driver: bridge

     

    Minimal Example:

    When the below compose file will be run then it will create a container with database running by pull the mysql image. It will also a database with name testdb and set the username to admin and password to Root@123 using which we can connect our application to this conatainer.

    services: app: image: mysql:latest ports: - "8080:8080" environment: MYSQL_DATABASE: testdb MYSQL_USER: admin MYSQL_ROOT_PASSWORD: Root@123 # (this is just an example, not intended to be a production configuration) # use .env file to inject the secrets. depends_on: - mysql mysql: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: root volumes: - mysql_data:/var/lib/mysql volumes: mysql_data:

    Conclusion#

    Docker Compose simplifies the management of multi-container applications by allowing developers to define services, networks, and volumes in a single configuration file. With simple commands, entire application stacks can be built, started, stopped, and scaled efficiently, making development, testing, and deployment workflows much more streamlined.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more

    Last updated on Mar 16, 2026

    Was it helpful?

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.