Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker Compose What is Docker Compose MultiContainer Orchestration for DevTest
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-compose-what-is-docker-compose-multicontainer-orchestration-for-devtest

Docker Docker Docker Compose What is Docker Compose MultiContainer Orchestration for DevTest

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Docker Compose allows you to define and run multi-container applications with a single command. docker-compose up

2. Step-by-Step

  1. Create a new directory for your project and navigate to it: mkdir myproject && cd myproject
  2. Create a new file named docker-compose.yml with the following content:
version: '3'
services:
  web:
build: .
ports:
- "8080:80"
  1. Run docker-compose up to start the containers: docker-compose up
  2. Check the containers are running: docker ps
  3. ⚠️ Stop all containers: docker-compose stop
  4. Remove all containers: docker-compose rm
  5. Remove the Docker network: docker network rm myproject_default
  6. Remove the Docker volume: docker volume rm myproject_default

3. Real Commands with Examples


Example 1: Running a simple web server

Purpose: Run a simple web server using the official Python image.
Command: docker-compose up -d --build Flags: - -d runs the containers in detached mode.
- --build builds the Docker image from the current directory.

Example 2: Running a database and web server

Purpose: Run a PostgreSQL database and a web server using the official Python image.
Command: docker-compose up -d Flags: - -d runs the containers in detached mode.

Example 3: Running a web server with environment variables

Purpose: Run a web server using the official Python image with environment variables.
Command: docker-compose up -d --env-file .env Flags: - --env-file loads environment variables from a file.

4. Common Errors


Error 1: Image not found

Error message: ERROR: Service 'web' uses an image, but 'docker-compose config' indicates support for Docker Compose files. Why it happens: The image is not defined in the docker-compose.yml file.
One-line fix: docker-compose config to check the configuration.

Error 2: Port already in use

Error message: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8080: bind: address already in use Why it happens: The port is already in use by another process.
One-line fix: docker-compose stop to stop the container.

Error 3: Volume not found

Error message: ERROR: The container name "/myproject_web_1" is already in use by container "/myproject_web_1". Why it happens: The volume is already in use by another container.
One-line fix: docker-compose rm to remove the container.

5. Quick Checks


Scenario 1: Check if a container is running

Situation: You want to check if a container is running.
Command: docker ps What a correct response looks like: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Scenario 2: Check the logs of a container

Situation: You want to check the logs of a container.
Command: docker-compose logs -f What a correct response looks like: The logs of the container.

Scenario 3: Check the status of a service

Situation: You want to check the status of a service.
Command: docker-compose ps What a correct response looks like: The status of the service.

6. Last-Minute Reference

  • docker-compose up starts the containers.
  • docker-compose down stops and removes the containers.
  • docker-compose restart restarts the containers.
  • docker-compose stop stops the containers.
  • docker-compose rm removes the containers.
  • docker-compose config checks the configuration.
  • docker-compose logs checks the logs.
  • docker-compose ps checks the status of the services.
  • docker-compose exec runs a command in a container.
  • docker-compose run runs a command in a new container.
  • ⚠️ docker-compose rm -f removes all containers without prompting.
  • ⚠️ docker-compose down -v removes all volumes without prompting.
  • ⚠️ docker-compose stop --time=1 stops the containers with a 1-second timeout.


ADVERTISEMENT