What is Docker Compose?

According to the official documentation, Docker Compose is used to define and run multi-container Docker applications. With Docker Compose, we can create a file (docker-compose.yml) to define an application’s services and use a single command to create and start the services.

Docker Compose on Educative

To use Docker Compose on Educative, follow the given steps. We are using a modified Flask-PHP example from here. The e-commerce website (made using PHP) will use an API on the products service (made using Flask) to request information about the products.

Step 1: Get your course enabled

At the moment, Docker Compose cannot run on the platform by default. It can be enabled for individual authors who need it for their course.

Please email authors@educative.io with your course’s link to request this special functionality.

Step 2: Create Dockerfile(s)

Create Dockerfile with the following Docker Compose installation commands:

Press + to interact
FROM gcr.io/educative-exec-env/educative-ubuntu-microvm:latest
RUN apt-get update && apt-get install -y curl wget
RUN wget -qO- https://get.docker.com/ | sh
RUN curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Explanation

  • Line 1: using gcr.io/educative-exec-env/educative-ubuntu-microvm:latest as the base image. This will be mandatory for Docker Compose setups.
  • Line 2: installing curl and wget.
  • Line 3: installing Docker.
  • Line 4: installing Docker Compose.

💡 We can have multiple RUN commands in our Dockerfile. However, RUN commands increase the size of a Docker image, and as a result, your image will take longer to build. Hence, a good practice is to use a single RUN and chain all your commands using &&.

Create the Dockerfile for the products service:

Press + to interact
FROM python:3
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . /usr/src/app
CMD cd /usr/src/app && python api.py

Explanation

  • Line 1: using python:3 as the base image.
  • Line 2: copying requirements.txt.
  • Line 3: installing the requirements.
  • Line 4: copying the current directory’s contents to /usr/src/app.
  • Line 5: specifying the commands to execute when we build the image.

Here’s what requirements.txt looks like:

Flask==0.12
flask-restful==0.3.5

Step 3: Create the docker-compose.yml file

Press + to interact
version: '3'
services:
product-service:
build: ./product
volumes:
- ./product:/usr/src/app
ports:
- 5001:80
website:
image: php:apache
volumes:
- ./website:/var/www/html
ports:
- 5000:80
depends_on:
- product-service

Explanation

  • Line 1: specifying the version of the docker-compose file format.
  • Line 3: we use it to specify our services. In the file above, we have two services: product-service and website.

The products-service service:

  • Line 5: specifying the path to the build context which in our case, is usercode/products.
  • Lines 6-7: mounting a volume. This is a great read on volumes.
  • Lines 8-9: mapping ports in the host:container format.

The website service:

  • Line 12: telling Docker Compose to use the php:apache image available at Docker Hub.
  • Lines 13-14: mounting a volume.
  • Lines 15-16: mapping ports in the host:container format.
  • Lines 17-18: expressing a dependency between website and product-service. You are encourage to read up on depends_on.

At Educative, the path of the volume you are trying to mount should be a subdirectory of /usercode/.

Step 4: Upload to the platform

Next, create a tarball out of all of your files and upload them to the platform as described in this lesson of the guide.

You can download the files for your tarball here:

FROM gcr.io/educative-exec-env/educative-ubuntu-microvm:latest
RUN apt-get update && apt-get install -y docker.io
RUN apt-get install -y curl && apt-get install -y wget
RUN curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Step 5: Create a Docker job

Let’s discuss each field in detail.

  • Job Name: A name for your job. It can be anything.
  • Input File Name: This field is not applicable when working with LiveVMs.
  • Run Script: This script will run every time the RUN button is clicked. After the first run, every subsequent run will execute the commands written in this field. Since it cannot be empty, we are adding echo "hello" here.
  • Run in Live Container: Checking this option will result in an app that stays live for 15 minutes.
  • Application Port: This is the port that your app is live on.
  • Start Script: This script runs the first time you click the RUN button.

Example

To run the Flask-PHP example, please follow this link.