Docker-Compose Explanation

Getting started with docker-compose

Introduction

Docker-compose is a tool that combines and runs multiple containers of interrelated services with a single command. It is a tool to define all the application dependencies in one place and let the Docker take care of running all of those services in just one simple command docker-compose up.

If you are following this course on your local machine remember to install the Docker Compose from the official documentation website.

Clone the git repo using git clone https://github.com/venky8283/Docker.git . Type git checkout 4de325cf1da2428e757be4f2bcc53f35c384c598 to get to the code used in this lesson.

Below is an example of docker-compose file that combines a MySQL database service with a Python flask app that implements a simple login and logout web application.

Press + to interact
version: '3'
services:
web:
# Path to dockerfile.
# '.' represents the current directory in which
# docker-compose.yml is present.
build: .
# Mapping of container port to host
ports:
- "5000:5000"
# Mount volume
volumes:
- "/usercode/:/code"
# Link database container to app container
# for reachability.
links:
- "database:backenddb"
database:
# image to fetch from docker hub
image: mysql/mysql-server:5.7
# Environment variables for startup script
# container will use these variables
# to start the container with these define variables.
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "MYSQL_USER=testuser"
- "MYSQL_PASSWORD=admin123"
- "MYSQL_DATABASE=backend"
# Mount init.sql file to automatically run
# and create tables for us.
# everything in docker-entrypoint-initdb.d folder
# is executed as soon as container is up nd running.
volumes:
- "/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"

Docker Compose file explanation

The Compose file is in YAML format. It’s a very ...