2.4. Orchestration

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

Docker Compose file

Previously we ran (you don’t need to run them again):

docker volume create volume-mariadb
docker run --name mariadb-container-with-existing-external-volume -v volume-mariadb:/var/lib/mysql -e MARIADB_ROOT_PASSWORD=my-secret-pw -d mariadb

and:

export ip=$(docker inspect mariadb-container-with-existing-external-volume -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}')
docker run -d --name frontend -e username=peter -e password=venkman -e servername=$ip grafgabriel/container-lab-frontend

We now create a file called compose.yml:

version: '3.8'

volumes:
  volume-mariadb:
    # remove external: true if you want to create a new volume
    external: true

services:
  mariadb:
    image: mariadb:11.5
    container_name: mariadb-container-with-existing-external-volume
    environment:
      MARIADB_ROOT_PASSWORD: my-secret-pw
    volumes:
      - volume-mariadb:/var/lib/mysql

  frontend:
    image: grafgabriel/container-lab-frontend
    container_name: frontend
    environment:
      username: peter
      password: venkman
      servername: mariadb-container-with-existing-external-volume
    ports:
      - "5000:5000"

If you explore this compose file you will see, that we could start both containers, create the volume and put them togheter in a network all in one file. The various options are described in the Compose file reference .

Having this file, you can run the app with a simple command, but don’t forget to stop your currently running containers first:

docker stop frontend
docker rm frontend
docker stop mariadb-container-with-existing-external-volume
docker rm mariadb-container-with-existing-external-volume
docker-compose up -d

Try connecting to the server using the container’s Docker-assigned IP address:

docker inspect frontend -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}'

This will show only the IP of the container as output:

172.17.0.4

Since we don’t have a browser in the web shell, use curl http://172.17.0.4:5000 to view the page in your terminal. On a local installation, you could simply open http://172.17.0.4:5000 in your browser.

curl http://172.17.0.4:5000

To stop the containers, use the following command:

docker-compose down

This will stop and remove the services.