Introduction :
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to describe your application’s services, networks, and volumes in a docker-compose.yml
file and then deploy and run the entire application stack with a single command. This tutorial will guide you through the basics of Docker Compose with practical examples.
Prerequisites:
- Docker installed on your system. You can download it from here.
Step 1: Create a Docker Compose File
Create a file named docker-compose.yml
in your project directory. This file will define the services, networks, and volumes for your application.
version: '3'
services:
web:
image: nginx:alpine
ports:
- "80:80"
database:
image: postgres:latest
environment:
POSTGRES_PASSWORD: mysecretpassword
In this example, we define two services: web
and database
. The web
service uses the Nginx image and exposes port 80. The database
service uses the latest PostgreSQL image and sets a password for the PostgreSQL server.
Step 2: Run the Docker Compose Application
Open a terminal and navigate to the directory where your docker-compose.yml
file is located. Run the following command to start the application:
docker-compose up
This command pulls the necessary images, creates containers, and starts the defined services. You should see logs from each service in the terminal.
Step 3: Access the Application
Open a web browser and navigate to http://localhost
. You should see the default Nginx welcome page.
Step 4: Stop and Remove the Containers
To stop the running containers and remove them, press Ctrl + C
in the terminal where docker-compose up
is running. Alternatively, you can run:
docker-compose down
This command stops and removes the containers, networks, and volumes defined in your docker-compose.yml
file.
Advanced Features:
1. Environment Variables:
You can use environment variables in your docker-compose.yml
file. Update the web
service to use a custom index page:
services:
web:
image: nginx:alpine
ports:
- "80:80"
environment:
- NGINX_INDEX=custom_index.html
2. Volume Mounting:
Mount a local directory into a container. Update the web
service to include a volume:
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
Create an html
directory in your project directory and place a custom HTML file inside.
3. Networking:
Define a custom network for your services. Update the docker-compose.yml
file:
version: '3'
services:
web:
image: nginx:alpine
ports:
- "80:80"
networks:
- mynetwork
database:
image: postgres:latest
environment:
POSTGRES_PASSWORD: mysecretpassword
networks:
- mynetwork
networks:
mynetwork:
This creates a custom network named mynetwork
and connects both services to it.
Conclusion:
Docker Compose is a powerful tool for defining and managing multi-container Docker applications. This tutorial covered the basics, and you can explore more features and options in the official documentation. As you work with Docker Compose, you’ll find it invaluable for simplifying the deployment and orchestration of your containerized applications.