Docker Compose is a tool that allows you to run multi-container Docker applications. With Compose, you can create and start all the services from your application with a single command. Compose is great for development, testing, and staging environments, as it allows you to easily build and teardown your app stack.
In this comprehensive guide, I‘ll show you how to install Docker Compose and walk through the basic workflow of using it. We‘ll cover:
- What is Docker Compose and why is it useful
- Installing Compose on Linux, Windows and Mac
- The docker-compose.yml file format
- Basic commands for managing Compose apps
- A sample workflow for building a simple Compose app
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker apps. With Compose, you can create a YAML file to configure the services for your application. Then, with a single command, you can spin everything up or tear it all down.
Some of the major features and benefits of Docker Compose include:
- Configure multiple containers and services in a single file
- Start all services with a single command (docker-compose up)
- Stop all services with a single command (docker-compose down)
- View the status of all services (docker-compose ps)
- Streamline development, testing, and staging environments
- Allows you to easily version and share your Docker app configuration
Overall, Compose makes it very easy to manage multi-container apps. It‘s a powerful tool for development and testing, as well as for running small production apps.
Installing Docker Compose
Docker Compose is included with Docker Desktop installations on Mac and Windows. For Linux users, you‘ll need to install Compose separately.
Installing on Linux
To install Compose on Linux, run the following command:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next, make the file executable:
sudo chmod +x /usr/local/bin/docker-compose
Test the installation:
docker-compose --version
This will print out the version if Compose is installed properly.
Installing on Windows and Mac
Compose is included in Docker Desktop for Windows and Mac. Just install the latest version of Docker Desktop and you will have Docker Compose as well.
To verify Compose is installed, run:
docker-compose --version
The Docker Compose File Format
The main component of Docker Compose is the docker-compose.yml file. This YAML formatted file defines the services, networks, and volumes that make up your app stack.
Here is an example docker-compose.yml file:
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
This Compose file defines two services: web and redis. The web service builds from the Dockerfile in the current directory and exposes port 5000. The redis service uses the latest alpine-based Redis image from Docker Hub.
Some key things to note about the Compose file format:
- The default filename is docker-compose.yml (can be overridden with -f flag)
- The version top-level key specifies the Compose file format version
- Services define the containers/apps to run as part of this app stack
- Other top-level keys include networks and volumes
There are many more options available here for configuring your app services such as build context, command, environment variables, external volumes, health checks, and much more.
The Compose file reference covers all available options in depth.
Basic Compose Commands
Now that we understand the Compose file format, let‘s go over some of the basic commands used to interact with a Compose app.
To start up the app stack defined in the docker-compose.yml file:
docker-compose up
This single command will:
- Build the web service image (if not already built)
- Pull the redis image (if not already pulled)
- Create the network
- Start the web and redis containers
- Attach output logs so you can see app and service activity
To stop the app stack and remove the containers, networks, and volumes:
docker-compose down
This stops everything related to this app stack and cleans everything up.
To start the stack in the background:
docker-compose up -d
To view the status of your stack:
docker-compose ps
This shows what services are currently running and their state.
There are many other commands related to managing Compose apps such as:
- build
- logs
- port
- restart
- stop
- exec
And more. Refer to the Compose CLI reference for details on all available commands.
A Sample Docker Compose Workflow
To see how Docker Compose fits into the development workflow, let‘s look at a simple Python/Flask app stack:
Project structure:
myapp/
docker-compose.yml
web/
Dockerfile
requirements.txt
app.py
docker-compose.yml
version: "3.9"
services:
web:
build: ./web
ports:
- "5000:5000"
web/Dockerfile
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
web/requirements.txt
flask
redis
web/app.py
from flask import Flask
app = Flask(__name__)
@app.route(‘/‘)
def hello():
return ‘Hello, World!‘
With this simple app, we can go through a Dev workflow:
- Make code changes to app.py or requirements.txt
- Run
docker-compose up --build
to rebuild image and restart containers - View app at http://localhost:5000 to test changes
- Repeat steps 1-3 as needed in development process
- Run
docker-compose down
when done testing to stop and remove resources
The Dockerfile handles building the image with our app requirements. The Compose file defines the services that make up the app stack. And the Compose CLI commands handle starting and stopping everything as needed.
This demonstrates how Compose can simplify the local development workflow, enabling fast iterations as you build your application.
Conclusion
Docker Compose provides a powerful, simplified mechanism for managing multi-container Docker apps. With just a single YAML config file and CLI, you can create and manage complex app stacks for development, testing, and small production use cases.
The key benefits of using Docker Compose include:
- Configure app stacks in a declarative fashion with YAML
- Manage the entire lifecycle of the app stack (build, run, tear down)
- Streamline and speed up development and testing workflows
- Isolate app dependencies into containers
- Standardize app stacks across teams using Compose config
Overall, Docker Compose is an essential tool for any Docker workflow. Take time to start integrating Docker Compose into your process today to see the benefits firsthand!