Login

Log in with one your accounts

Contribution

We are happy you want to contribute to DXKB. Please choose your preferred way

All Articles
Dec 05, 20245 min read

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.

Ready to put this knowledge into action?

Visit our website and learn how DX Heroes can help your business succeed.

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

  1. **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
  1. 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. Version 3.8 is compatible with modern Docker releases.

  • services: Defines the containers to be managed. In this example, there's only one service named web.

  • 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 a Dockerfile 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 is host:container. For example: "8000:5000" maps port 8000 on your machine (host) to port 5000 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 in requirements.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 script app.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 at http://localhost:8000.
  1. 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 the DXHeroes file and use the docker-compose up command to start the service. Now, go to your browser and run http://localhost:8000/, you should find Hello, World! staring at you 😊.

Complete code on GitHub

Considerations for Docker Compose

  1. 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.

  2. 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 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.

Was the article helpful?

Want to write for DXKB?

Feel free to contribute. People from DXKB community will be more than happy.

Contribution

We are happy you want to contribute to DXKB. Please choose your preferred way