Home Explore Blog CI



docker

1st chunk of `content/manuals/compose/gettingstarted.md`
62047aa3a6fd701bafa5937895663d00a6bcd7b281bf04e10000000100001001
---
description: Check out this tutorial on how to use Docker Compose from defining application
  dependencies to experimenting with commands.
keywords: docker compose example, docker compose tutorial, how to use docker compose,
  running docker compose, how to run docker compose, docker compose build image, docker
  compose command example, run docker compose file, how to create a docker compose
  file, run a docker compose file
title: Docker Compose Quickstart
linkTitle: Quickstart
weight: 30
---

This tutorial aims to introduce fundamental concepts of Docker Compose by guiding you through the development of a basic Python web application. 

Using the Flask framework, the application features a hit counter in Redis, providing a practical example of how Docker Compose can be applied in web development scenarios. 

The concepts demonstrated here should be understandable even if you're not familiar with Python. 

This is a non-normative example that demonstrates core Compose functionality. 

## Prerequisites

Make sure you have:

- [Installed the latest version of Docker Compose](/manuals/compose/install/_index.md)
- A basic understanding of Docker concepts and how Docker works

## Step 1: Set up

1. Create a directory for the project:

   ```console
   $ mkdir composetest
   $ cd composetest
   ```

2. Create a file called `app.py` in your project directory and paste the following code in:

   ```python
   import time

   import redis
   from flask import Flask

   app = Flask(__name__)
   cache = redis.Redis(host='redis', port=6379)

   def get_hit_count():
       retries = 5
       while True:
           try:
               return cache.incr('hits')
           except redis.exceptions.ConnectionError as exc:
               if retries == 0:
                   raise exc
               retries -= 1
               time.sleep(0.5)

   @app.route('/')
   def hello():
       count = get_hit_count()
       return f'Hello World! I have been seen {count} times.\n'
    ```

   In this example, `redis` is the hostname of the redis container on the
   application's network and the default port, `6379` is used.

   > [!NOTE]
   >
   > Note the way the `get_hit_count` function is written. This basic retry
   > loop attempts the request multiple times if the Redis service is
   > not available. This is useful at startup while the application comes
   > online, but also makes the application more resilient if the Redis
   > service needs to be restarted anytime during the app's lifetime. In a
   > cluster, this also helps handling momentary connection drops between
   > nodes.

3. Create another file called `requirements.txt` in your project directory and
   paste the following code in:

   ```text
   flask
   redis
   ```

4. Create a `Dockerfile` and paste the following code in:

   ```dockerfile
   # syntax=docker/dockerfile:1
   FROM python:3.10-alpine
   WORKDIR /code
   ENV FLASK_APP=app.py
   ENV FLASK_RUN_HOST=0.0.0.0
   RUN apk add --no-cache gcc musl-dev linux-headers
   COPY requirements.txt requirements.txt
   RUN pip install -r requirements.txt
   EXPOSE 5000
   COPY . .
   CMD ["flask", "run", "--debug"]
   ```

   {{< accordion title="Understand the Dockerfile" >}}

   This tells Docker to:

   * Build an image starting with the Python 3.10 image.
   * Set the working directory to `/code`.
   * Set environment variables used by the `flask` command.
   * Install gcc and other dependencies
   * Copy `requirements.txt` and install the Python dependencies.
   * Add metadata to the image to describe that the container is listening on port 5000
   * Copy the current directory `.` in the project to the workdir `.` in the image.
   * Set the default command for the container to `flask run --debug`.

   {{< /accordion >}}

   > [!IMPORTANT]
   >
   >Check that the `Dockerfile` has no file extension like `.txt`. Some editors may append this file extension automatically which results in an error when you run the application.

   For more information on how to write Dockerfiles, see the [Dockerfile reference](/reference/dockerfile/).

Title: Docker Compose Quickstart: Setting Up a Basic Python Web App
Summary
This section guides you through setting up a basic Python web application using Flask and Redis. It involves creating the project directory, `app.py`, `requirements.txt`, and a `Dockerfile`. The `Dockerfile` sets up the Python environment, installs dependencies, and configures the application to run using Flask.