002 DevOps - Docker Compose
Docker is a powerful tool that helps in working with containers. While designing an application like a distributed micro-services app, we need much simpler tool that can setup multiple containers simultaneously and make their network connections, easily define and run multi-container Docker applications.
Docker Compose comes to save the day here.
It allows you to manage complex application stacks with ease, by using a simple YAML file to define the services, networks, and volumes required for your application.
In this article, we'll explore Docker Compose from installation to the basic usage, providing practical example along the way.
Installation
Earlier, Docker Compose was available as a standalone binary, which needed to be installed separately after installing Docker.
As of 2024, Docker Compose has become part of the main Docker installation along with Docker Engine and Docker CLI.
From July 2023, Compose V1 stopped receiving updates. It’s also no longer available in new releases of Docker Desktop. To install Compose V2, simply install Docker Desktop for your operating system from Get Docker.
Example: Setting Up a Simple Web Application
Let's start with a basic example of using Docker Compose to set up a simple web application consisting of a Flask(python) backend and a Redis instance. The below example is a cut down version of this Docker Compose Example.
Step 1: Define the Docker Compose File
Here we create 4 files for complete example to run an application with database to store values. File names will be app.py, requirements.txt, Dockerfile, compose.yml; paste below content in each file.
This is code for a flask server and a connection to the redis instance.
# app.py
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 'Hello World! I have been seen {} times.\n'.format(count)
Below is the requirements.txt file for python package management.
# requirements.txt
flask
redis
Here comes a Dockerfile which does the heavy lifting to move all code to docker container, also installs necessary binaries.
领英推荐
# Dockerfile
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"]
And this is the docker compose file which connects everything together. It creates two services (containers) web and redis that connect to each other.
# compose.yml
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
Step 2: Build and Run the Application
Run docker compose up in the project directory to build and run the application stack defined in Dockerfile.
docker compose up
Once running you will see logs as below in your terminal. Visit the app on https://172.18.0.3:5000/ to see it working.
redis-1 | oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
web-1 | * Serving Flask app 'app.py'
web-1 | * Debug mode: on
web-1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1 | * Running on all addresses (0.0.0.0)
web-1 | * Running on https://127.0.0.1:5000
web-1 | * Running on https://172.18.0.3:5000
Source code:
Conclusion:
Docker Compose is an essential tool for simplifying the management of multi-container Docker applications. By mastering Docker Compose, you'll be well-equipped to handle complex application architectures with ease.
Stay tuned for an upcoming article about advanced docker compose example.