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 runcommands? Running multipledocker runcommands 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.ymlfile? 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) anddatabase(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_PASSWORDorMYSQL_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
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
-
What does
services:mean? It is the top-level section that lists all the individual containers (websiteanddatabase) belonging to this application. -
What does
image:do? It tells Docker which base image to pull and run for that service (httpd:2.4for Apache andmysql:8.0for 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 currentDay4folder (.) directly into Apache's document root (/usr/local/apache2/htdocs/) for live file syncing. -
What are
environmentvariables? They pass startup configuration values into the MySQL container—automatically setting the root password and creating an initial database (mydb) when the container boots.
4. Starting the Application (docker compose up)
Predictions Before Running
-
How many containers will Docker create? Two containers: one for
websiteand one fordatabase. -
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.0wasn't cached locally, whereashttpd:2.4was already cached from yesterday).
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
5. Verifying Everything Works
Opening http://localhost:8080 in my browser confirmed Apache was serving my new page.
-
Are both containers running? Yes. Both
day4-website-1andday4-database-1showed statusStarted. -
Which ports are being used?
websitemaps host port8080to container port80(0.0.0.0:8080->80/tcp), whiledatabaseexposes MySQL's default internal port (3306/tcp,33060/tcp). -
Which service is responsible for displaying your webpage? The
websiteservice (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
-
Which containers belong to this Compose project?
day4-website-1andday4-database-1. -
Which images are being used?
httpd:2.4(for the web server) andmysql:8.0(for the database). -
Which network was created automatically?
day4_default(using thebridgedriver). -
Can you identify the names of your services? Yes.
websiteanddatabase(as listed under theSERVICEcolumn indocker compose ps).
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
-
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 createsmydbon first startup. In contrast, Apache only logs a few startup lines.
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
databaseas 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
-
What is the difference between
docker compose stopanddocker compose down?docker compose stoponly pauses/stops the running containers while keeping the containers and network intact so they can be restarted later.docker compose downstops 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)