Docker Compose
What Is Docker Compose?
It will be great to have a simple definition of Docker, before progressing to Docker Compose.
Docker is a containerization tool that assists you in building software solutions in an isolated environments that can function reliably across different infrastructure.
Hence, Docker Compose is a tool that helps define and run multi-container Docker applications. Using a single YAML file (docker-compose.yml
), you can configure services, networks, and volumes for your application, enabling streamlined management and deployment of multiple containers. It simplifies the process of running complex applications by providing a clear structure and commands to manage the containers.
Docker Compose is useful for local development, testing, and staging environments, enabling developers to work with multiple containers seamlessly.
Do you need Docker Compose?
Docker Compose offers several benefits, including:
-
Simplified Configuration Use a single YAML file to define multiple configuration without any external resources, and it will be executed accordingly.
-
Managing Dependencies With Docker Compose you can simplify using and managing services using a single YAML configuration.
Using Docker Compose
- **Install Docker **
Ensure Docker is installed on your system. If you don't have it installed, get it for your respective machine from the official download page. You can verify installation using the command below, which will show the version of both Docker and the Docker Compose.
docker --version && docker-compose --version
- Run a Simple App using Docker Compose This example will demonstrate running a Hello World app in Python using Docker Compose.
Create a folder, called DXHeroes
and inside the folder create all the files below in it.
.
├── app.py
├── requirements.txt
├── Dockerfile
└── docker-compose.yml
App.py
This is your Python application, using Flask to create a simple web server which will return Hello, World!
.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello, World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
Requirements.txt
flask
Docker Compose File
The docker-compose.yml
file defines the configuration for your multi-container Docker application. It specifies how Docker should set up and run your services.
version: '3.8'
services:
web:
build:
context: .
ports:
- "8000:5000"
-
version: '3.8'
Specifies the Docker Compose file format version. Version3.8
is compatible with modern Docker releases. -
services:
Defines the containers to be managed. In this example, there's only one service namedweb
. -
web:
This is the name of the service. It represents the container running your Python app. -
build:
Specifies the build context. Here,context: .
means Docker should look for aDockerfile
in the current directory to build the image for this service. -
ports:
Maps a port on the host machine to a port in the container. The format ishost:container
. For example:"8000:5000"
maps port8000
on your machine (host) to port5000
in the container.
Dockerfile
The Dockerfile
defines how to build the image for the web
service specified in docker-compose.yml
.
# Use Python image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container
COPY . /app
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Run the application
CMD ["python", "app.py"]
-
FROM python:3.9-slim
Specifies the base image for the container. Here, it uses a lightweight Python 3.9 image. -
WORKDIR /app
Sets the working directory inside the container to/app
. All subsequent commands are executed in this directory. -
COPY . /app
Copies the contents of the current directory on the host machine (where the Dockerfile is located) into the/app
directory in the container. -
RUN pip install --no-cache-dir -r requirements.txt
Installs the dependencies listed inrequirements.txt
. The--no-cache-dir
option prevents caching to reduce the image size. -
CMD ["python", "app.py"]
Specifies the command to run when the container starts. Here, it runs the Python scriptapp.py
.
How They Work Together
- The
Dockerfile
is used to create a Docker image that encapsulates your Python application and its dependencies.- The
docker-compose.yml
file creates and manage the service, using the image built from the Dockerfile and mapping the container's port to the host.- The
app.py
runs inside the container, serving the "Hello, World!" response when accessed athttp://localhost:8000
.
- Run Your Docker File
To run your docker file, you need to start the
Docker
app on your machine, then head over to your terminal. Open theDXHeroes
file and use thedocker-compose up
command to start the service. Now, go to your browser and runhttp://localhost:8000/
, you should findHello, World!
staring at you 😊.
Considerations for Docker Compose
-
Resource Management Running multiple containers can consume significant resources. You need to monitor your container performance and adjust resource limits in the
docker-compose.yml
file. -
Production Use Docker Compose is primarily used for development and testing. For production environments, consider tools like Kubernetes (K8s) for advanced orchestration.
Further Resources for Docker Compose
- Docker Compose Documentation – Official documentation for Docker Compose.
- Docker Cheat Sheet for Developers
- Running Puppeteer in a Docker Environment - Running Puppeteer in Docker simplifies the setup process and ensures that it works perfectly in every environment, from a developer's machine to the production server.
Docker Compose simplifies managing multi-container applications by organizing configurations and workflows into a single YAML file. Using Docker Compose, developers can streamline development, ensure consistency, and reduce complexity in multi-service environments.
Want to write for DXKB?
Feel free to contribute. People from DXKB community will be more than happy.
Related articles
ALL ARTICLES
Devops
DevOps is a set of practices that brings development and operations teams together. The collaboration helps to release software much faster.
Read moreRetrospective
A retrospective meeting is an opportunity for the team to inspect itself and create a plan for improvements to be included in the next Sprint.
Read moreContinuous Delivery
Practicing Continuous Delivery means that you adopt practices to be ready to release product changes any time you want. Your product is always ready to deploy to production.
Read moreMeaningful Meetings
Meaningful Meetings are methods that see meetings as obstacles in work. Through practices like setting reasons, creating agendas, tracking outcomes, or meeting polices, you can shorten the meetings and boost your team's performance.
Read moreAPI Authentication
API Authentication is the process of securely verifying and authenticating API requests. This article outlines best practices for setting API credentials and securing APIs.
Read moreALL ARTICLES