12.1 Docker Compose

Instead of managing the containers with the docker command, you may use Docker Compose to handle them.

Docker Compose file

Previously we ran:

docker run --name mariadb-container-with-existing-external-volume -v$(pwd)/datastore-mysql:/var/lib/mysql -it -e MARIADB_ROOT_PASSWORD=my-secret-pw -d mariadb

and:

docker run -itd --name php-app -p8080:80 --link mariadb-container-with-existing-external-volume php-app

We now create a file called docker-compose.yml:

version: '3'

services:

  php-app:
    image: php-app
    ports:
      - '8080:80'
    networks:
      - container-basics-training

  mariadb-container-with-existing-external-volume:
    image: mariadb
    environment:
      - MARIADB_ROOT_PASSWORD=my-secret-pw
    volumes:
      - 'volume-mariadb:/var/lib/mysql'
    networks:
      - container-basics-training

networks:
  container-basics-training:

volumes:
  volume-mariadb:

For each of the docker run commands, you add an entry under services, containing the appropriate options. The various options are described in the Compose file reference .

Having this file, you can run both containers with a simple command:

docker-compose up

Then again, check http://LOCALHOST:8080/db.php in a browser (or curl in another terminal in the webshell).

To stop the containers, hit CTRL+c followed by

docker-compose down

This will stop and remove the services.