Docker Crash Course #11 - Docker Compose
Table of Contents
Introduction
This tutorial will guide you through Docker Compose, a tool that simplifies the process of managing multi-container Docker applications. You'll learn how to define and run multi-container applications using a single YAML file, making it easier to manage dependencies and configurations for your projects.
Step 1: Install Docker and Docker Compose
Before you can use Docker Compose, ensure you have Docker installed on your system. Docker Compose is included in Docker Desktop installations for both Windows and macOS. For Linux, you may need to install Docker Compose separately.
- Download Docker Desktop from Docker Hub.
- For Linux users, follow the official installation guide.
Step 2: Create a Docker Compose File
The Docker Compose file, typically named docker-compose.yml
, defines your application services, networks, and volumes. Here’s how to create one:
- Create a new directory for your project.
- Inside the directory, create a file named
docker-compose.yml
. - Open the file in your preferred text editor.
Step 3: Define Services in the Docker Compose File
In your docker-compose.yml
file, you can specify various services. Here’s a simple example for a Node.js application:
version: '3'
services:
web:
image: node:14
volumes:
- .:/usr/src/app
working_dir: /usr/src/app
command: npm start
ports:
- "3000:3000"
Explanation of the YAML Structure
version
: Specifies the version of the Docker Compose file format.services
: Defines the containers you want to run.web
: The name of the service.image
: The Docker image to use.volumes
: Maps the current directory to the container.working_dir
: Sets the working directory inside the container.command
: The command to run when starting the container.ports
: Maps the host port to the container port.
Step 4: Start the Application
To start your application using Docker Compose, run the following command in your terminal from the directory containing your docker-compose.yml
file:
docker-compose up
- This command will pull the required images, create the containers, and start the services defined in your Compose file.
- Add the
-d
flag to run containers in detached mode:
docker-compose up -d
Step 5: Manage Your Containers
You can manage your containers using a few key commands:
- To stop the services:
docker-compose down
- To view the running containers:
docker-compose ps
- To view logs from your services:
docker-compose logs
Step 6: Scale Services
Docker Compose allows you to scale services easily. For example, if you want to run multiple instances of the web
service, you can use:
docker-compose up --scale web=3
This command will start three instances of the web
service defined in your docker-compose.yml
file.
Conclusion
Docker Compose simplifies the management of multi-container applications, allowing you to define all services in one YAML file and manage them with simple commands. By following this tutorial, you now have the foundational knowledge to create and manage your Docker applications effectively.
For next steps, consider exploring more complex configurations, adding databases, or integrating with other services as your projects grow. Happy coding!