Docker Compose Fundamentals
This section focuses on the fundamental aspects of Docker Compose, outlining the structure of a docker-compose.yml
file, the creation and management of multi-container applications, and how to leverage Docker Compose for local development and testing.
Defining Multi-container Applications
Docker Compose facilitates the definition of multi-container applications through a YAML file named docker-compose.yml
. This file acts as a blueprint, specifying the services, networks, and volumes that comprise your application.
Example:
version: '3.8' # Specify the Compose file version
services:
web:
build: . # Build the web service from the current directory
ports:
- "80:80" # Expose port 80 on the host to port 80 in the container
depends_on:
- db # Ensure the database service is started before the web service
db:
image: mysql:latest # Use the latest MySQL image
environment:
MYSQL_ROOT_PASSWORD: password # Set the MySQL root password
Explanation:
version
: Specifies the Docker Compose file version.services
: Defines the individual services that constitute the application.build
: Instructs Compose to build an image from the specified directory.ports
: Maps ports between the host and the container.depends_on
: Ensures that the specified service is started before the current service.image
: Defines the image to use for the service.environment
: Sets environment variables for the service.
Defining Services, Networks, and Volumes
Services:
A service represents a container within your multi-container application. Each service is defined within the services
section of the docker-compose.yml
file.
Example:
services:
web:
image: nginx:latest
db:
image: postgres:latest
Networks:
Networks in Docker Compose provide a means for services to communicate with each other.
Example:
networks:
app-net: # Define a network named 'app-net'
driver: bridge # Use the 'bridge' network driver
Volumes:
Volumes in Docker Compose allow for persistent data storage, decoupling data from the container’s life cycle.
Example:
services:
web:
volumes:
- data:/var/www/html # Mount a volume named 'data' to the container's /var/www/html directory
volumes:
data:
Docker Compose Lifecycle
Docker Compose provides commands for managing the application’s lifecycle, including up
, down
, and restart
.
docker-compose up
: Starts and builds services defined in the docker-compose.yml
file.
docker-compose down
: Stops and removes containers, networks, and volumes created by docker-compose up
.
docker-compose restart
: Restarts the services defined in the docker-compose.yml
file.
Using Docker Compose for Local Development and Testing
Docker Compose is a valuable tool for local development and testing, simplifying the process of managing and deploying multi-container applications.
Benefits of Docker Compose:
- Simplified deployment: Compose streamlines the process of running and managing multi-container applications.
- Consistent environment: Ensures a consistent development environment across different machines.
- Improved workflow: Enables faster development cycles by automating common tasks.
Example:
docker-compose up -d # Start the application in detached mode
docker-compose exec web bash # Access the web service's container shell
docker-compose down # Stop and remove the application
Sources: