DEV Community

Meshack Mulei
Meshack Mulei

Posted on Edited on

Docker Compose, Running Multiple Containers Together.

Introduction

Yesterday, I used Docker Volumes to sync live edits to a single Apache container. Today, I leveled up to Docker Compose, defining and running a multi-container stack (an Apache web server and a MySQL 8 database) together on an isolated private network with a single command.

1. Understanding Docker Compose

  • What is Docker Compose? Docker Compose is a tool for defining, configuring, and running multi-container Docker applications from a single YAML configuration file (docker-compose.yml).
  • Why would you use Docker Compose instead of multiple docker run commands? Running multiple docker run commands manually requires remembering long flags for ports, volumes, environment variables, and custom networks, and repeating them in the right order every time. Compose puts the entire architecture in one version-controlled file so everything starts or stops with one command.
  • What is a docker-compose.yml file? It is a human-readable YAML blueprint that declares every container (service), image, port mapping, volume mount, environment variable, and network your application needs.
  • What are Services in Docker Compose? Each service represents one container role in your application stack, For example, website (running Apache) and database (running MySQL).
  • What are Environment Variables, and why are they useful? Environment variables are dynamic key-value settings passed into a container at runtime (such as MYSQL_ROOT_PASSWORD or MYSQL_DATABASE). They keep configuration flexible and avoid hardcoding passwords or settings directly inside images.

2. Creating a New Workspace

I created a fresh Day4 folder and wrote a simple index.html welcome page introducing today's Docker Compose setup:

mkdir Day4 && cd Day4
nano index.html
Enter fullscreen mode Exit fullscreen mode

Creating a New Workspace

3. Creating My First docker-compose.yml File

Next, I created docker-compose.yml to define both my website (Apache) and database (MySQL 8) services:

services:
  website:
    image: httpd:2.4
    ports:
      - "8080:80"
    volumes:
      - .:/usr/local/apache2/htdocs/

  database:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_DATABASE: mydb
Enter fullscreen mode Exit fullscreen mode
  • What does services: mean? It is the top-level section that lists all the individual containers (website and database) belonging to this application.
  • What does image: do? It tells Docker which base image to pull and run for that service (httpd:2.4 for Apache and mysql:8.0 for MySQL).
  • Why do we use ports:? It maps a port on my host machine (8080) to the listening port inside the container (80) so I can open the site in my browser.
  • What does volumes: do? It mounts my current Day4 folder (.) directly into Apache's document root (/usr/local/apache2/htdocs/) for live file syncing.
  • What are environment variables? They pass startup configuration values into the MySQL container—automatically setting the root password and creating an initial database (mydb) when the container boots.

My First docker-compose.yml

4. Starting the Application (docker compose up)

Predictions Before Running

  • How many containers will Docker create? Two containers: one for website and one for database.
  • Will Docker create anything besides containers? Yes. Docker Compose automatically creates an isolated bridge network (named day4_default) so the two containers can talk to each other.
  • What happens if the required images don't already exist on my machine? Docker automatically downloads them from Docker Hub (mysql:8.0 wasn't cached locally, whereas httpd:2.4 was already cached from yesterday).

Starting the Application

What Actually Happened

Running docker compose up -d pulled mysql:8.0 from Docker Hub, created the day4_default network, and launched both day4-website-1 and day4-database-1 in detached mode:

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

docker compose up

5. Verifying Everything Works

Opening http://localhost:8080 in my browser confirmed Apache was serving my new page.

Exploring

  • Are both containers running? Yes. Both day4-website-1 and day4-database-1 showed status Started.
  • Which ports are being used? website maps host port 8080 to container port 80 (0.0.0.0:8080->80/tcp), while database exposes MySQL's default internal port (3306/tcp, 33060/tcp).
  • Which service is responsible for displaying your webpage? The website service (httpd:2.4).

6. Exploring My Compose Project

To inspect all components belonging to this Compose project, I ran docker compose ps and docker network ls:

docker compose ps
docker network ls
Enter fullscreen mode Exit fullscreen mode
  • Which containers belong to this Compose project? day4-website-1 and day4-database-1.
  • Which images are being used? httpd:2.4 (for the web server) and mysql:8.0 (for the database).
  • Which network was created automatically? day4_default (using the bridge driver).
  • Can you identify the names of your services? Yes. website and database (as listed under the SERVICE column in docker compose ps).

Exploring

7. Investigating the Logs

I inspected the combined logs as well as individual service logs:

docker compose logs
docker compose logs website
docker compose logs database
Enter fullscreen mode Exit fullscreen mode
  • Which service produced the most output? The database (mysql:8.0) service produced far more log output because MySQL initializes system tables, generates SSL certificates, sets up permissions, and creates mydb on first startup. In contrast, Apache only logs a few startup lines.

Investigating

8. Discovering the Hidden Network

  • Does Docker Compose create a network automatically? Yes (day4_default).
  • Can containers communicate with each other without knowing each other's IP addresses? Yes. Compose provides built-in internal DNS resolution on that network.
  • If your website needed to connect to the database, what hostname would it use? It would simply use the service name database as the hostname.
  • Why does this make application development easier? Container IP addresses change every time containers restart, so using stable service names (database) eliminates hardcoded IPs and manual network wiring.

9. Leaving No Zombies (Cleanup)

To stop and clean up everything cleanly, I ran:

docker compose down
docker compose ps -a
Enter fullscreen mode Exit fullscreen mode

Cleanup

  • What is the difference between docker compose stop and docker compose down? docker compose stop only pauses/stops the running containers while keeping the containers and network intact so they can be restarted later. docker compose down stops and completely removes the containers and the automatically created network (day4_default).

Reflection

  • What surprised you most about Docker Compose? How effortless it was to spin up an entire web server and a full MySQL database, complete with automatic database creation and private DNS networking, using just a 14-line YAML file and one command.

Top comments (0)