Understanding Docker Compose
In this lecture weāre going to have a closer look at Docker-Compose, the orchestration for Docker containers.
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
- In this case called
- It will run this as a container named
[folder-name]_[service_name]_[index #]
- So:
step-1_phpapp_1
- So:
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
Last updated on