Try Docker Compose (2024)

This tutorial is designed to introduce the key concepts of Docker Compose whilst building a simple Python web application. The application uses the Flask framework and maintains a hit counter inRedis.

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

You need to have Docker Engine and Docker Compose on your machine. You can either:

You don't need to install Python or Redis, as both are provided by Docker images.

Step 1: Define the application dependencies

  1. Create a directory for the project:

    $ mkdir composetest$ cd composetest
  2. Create a file called app.py in your project directory and paste the following code in:

    import timeimport redisfrom flask import Flaskapp = 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 'Hello World! I have been seen {} times.\n'.format(count)

    In this example, redis is the hostname of the redis container on theapplication's network. We use the default port for Redis, 6379.

    Handling transient errors

    Note the way the get_hit_count function is written. This basic retryloop lets us attempt our request multiple times if the redis service isnot available. This is useful at startup while the application comesonline, but also makes the application more resilient if the Redisservice needs to be restarted anytime during the app's lifetime. In acluster, this also helps handling momentary connection drops betweennodes.

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

    flaskredis

The Dockerfile is used to build a Docker image. The imagecontains all the dependencies the Python application requires, including Pythonitself.

In your project directory, create a file named Dockerfile and paste the following code in:

# syntax=docker/dockerfile:1FROM python:3.10-alpineWORKDIR /codeENV FLASK_APP=app.pyENV FLASK_RUN_HOST=0.0.0.0RUN apk add --no-cache gcc musl-dev linux-headersCOPY requirements.txt requirements.txtRUN pip install -r requirements.txtEXPOSE 5000COPY . .CMD ["flask", "run"]

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.

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 theDocker user guideand theDockerfile reference.

Step 3: Define services in a Compose file

Create a file called compose.yaml in your project directory and pastethe following:

services: web: build: . ports: - "8000:5000" redis: image: "redis:alpine"

This Compose file defines two services: web and redis.

The web service uses an image that's built from the Dockerfile in the current directory.It then binds the container and the host machine to the exposed port, 8000. This example service uses the default port for the Flask web server, 5000.

The redis service uses a publicRedisimage pulled from the Docker Hub registry.

  1. From your project directory, start up your application by running docker compose up.

    $ docker compose upCreating network "composetest_default" with the default driverCreating composetest_web_1 ...Creating composetest_redis_1 ...Creating composetest_web_1Creating composetest_redis_1 ... doneAttaching to composetest_web_1, composetest_redis_1web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Ooredis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just startedredis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.confweb_1 | * Restarting with statredis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.web_1 | * Debugger is active!redis_1 | 1:M 17 Aug 22:11:10.483 # Server initializedredis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.web_1 | * Debugger PIN: 330-787-903redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections

    Compose pulls a Redis image, builds an image for your code, and starts theservices you defined. In this case, the code is statically copied into the image at build time.

  2. Enter http://localhost:8000/ in a browser to see the application running.

    If this doesn't resolve, you can also try http://127.0.0.1:8000.

    You should see a message in your browser saying:

    Hello World! I have been seen 1 times.

    Try Docker Compose (1)

  3. Refresh the page.

    The number should increment.

    Hello World! I have been seen 2 times.

    Try Docker Compose (3)

  4. Switch to another terminal window, and type docker image ls to list local images.

    Listing images at this point should return redis and web.

    $ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEcomposetest_web latest e2c21aa48cc1 4 minutes ago 93.8MBpython 3.4-alpine 84e6077c7ab6 7 days ago 82.5MBredis alpine 9d8fa9aa0e5b 3 weeks ago 27.5MB

    You can inspect images with docker inspect <tag or id>.

  5. Stop the application, either by running docker compose downfrom within your project directory in the second terminal, or byhitting CTRL+C in the original terminal where you started the app.

Step 5: Edit the Compose file to add a bind mount

Edit the compose.yaml file in your project directory to add abind mount for the web service:

services: web: build: . ports: - "8000:5000" volumes: - .:/code environment: FLASK_DEBUG: "true" redis: image: "redis:alpine"

The new volumes key mounts the project directory (current directory) on thehost to /code inside the container, allowing you to modify the code on thefly, without having to rebuild the image. The environment key sets theFLASK_DEBUG environment variable, which tells flask run to run in developmentmode and reload the code on change. This mode should only be used in development.

From your project directory, type docker compose up to build the app with the updated Compose file, and run it.

$ docker compose upCreating network "composetest_default" with the default driverCreating composetest_web_1 ...Creating composetest_redis_1 ...Creating composetest_web_1Creating composetest_redis_1 ... doneAttaching to composetest_web_1, composetest_redis_1web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)...

Check the Hello World message in a web browser again, and refresh to see thecount increment.

Shared folders, volumes, and bind mounts

  • If your project is outside of the Users directory (cd ~), then youneed to share the drive or location of the Dockerfile and volume you are using.If you get runtime errors indicating an application file is not found, a volumemount is denied, or a service cannot start, try enabling file or drive sharing.Volume mounting requires shared drives for projects that live outside ofC:\Users (Windows) or /Users (Mac), and is required for any project onDocker Desktop for Mac and Docker Desktop for Windows that usesLinux containers.For more information, seeFile sharing on Docker for Mac,File sharing on Docker for Windows,File sharing on Docker for Linux.and the general examples on how toManage data in containers.

  • If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in thisVB troubleticket. Newer Windows systems meet therequirements forDocker Desktop for Windows and do notneed VirtualBox.

Step 7: Update the application

As the application code is now mounted into the container using a volume,you can make changes to its code and see the changes instantly, without havingto rebuild the image.

Change the greeting in app.py and save it. For example, change the Hello World!message to Hello from Docker!:

return 'Hello from Docker! I have been seen {} times.\n'.format(count)

Refresh the app in your browser. The greeting should be updated, and thecounter should still be incrementing.

Try Docker Compose (5)

If you want to run your services in the background, you can pass the -d flag(for "detached" mode) to docker compose up and use docker compose ps tosee what is currently running:

$ docker compose up -dStarting composetest_redis_1...Starting composetest_web_1...$ docker compose ps Name Command State Ports -------------------------------------------------------------------------------------composetest_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp composetest_web_1 flask run Up 0.0.0.0:8000->5000/tcp

The docker compose run command allows you to run one-off commands for yourservices. For example, to see what environment variables are available to theweb service:

$ docker compose run web env

See docker compose --help to see other available commands.

If you started Compose with docker compose up -d, stopyour services once you've finished with them:

$ docker compose stop

You can bring everything down, removing the containers entirely, with the downcommand. Pass --volumes to also remove the data volume used by the Rediscontainer:

$ docker compose down --volumes

Where to go next

Try Docker Compose (2024)
Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 5977

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.