Skip to Content
CoursesLearn DockerYour first Docker-Compose.yml file

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
docker-compose down docker-compose rm
  • Cleanup, remove the containers
Last updated on