Skip to content

Understanding Docker Compose

In this lecture we're going to have a closer look at Docker-Compose, the orchestration for Docker containers.

View the Full Course Now

The docker-compose.yml File Explained Line by Line for Composer Beginners

Let's re-use this Dockerfile:

FROM php:7.2-apache
COPY index.php /var/www/html

And this index.php file in the same directory:

<?php

echo "hello world \n\n";

But this time we will add a docker-compose.yml file in the same directory as well:

version: '3'

services:
  phpapp:
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8080:80"

Then we run

docker-compose up

What happens here?

  • It will read the docker-compose.yml file in the current directory
  • It will build a new image with this pattern: [folder-name]_[service-name] based on the Dockerfile
    • In this case called step-1_phpapp
  • It will run this as a container named [folder-name]_[service_name]_[index #]
    • So: step-1_phpapp_1

Now we can access http://localhost:8080 and it should show us the "hello world" again from our index.php.

If we change the index.php, still nothing happens.

  • Change the "hello world" to "hello docker".
  • Save it.
  • Reload the browser, you still see "hello world" instead of "hello docker"
  • Do you know why? ... yep! Volume Mounting, or image-rebuilding...

Stop the running containers with ctrl-c

Then start the containers again:

docker-compose up

Open http://localhost:8080 should still bring up "hello world". Because our image is still the same. We need to rebuild it!

docker-compose down

docker-compose up --build
  • This rebuilds the image before spinning it up
  • Now http://localhost:8080 container "hello docker"
docker-compose down
docker-compose rm
  • Cleanup, remove the containers