docker-compose.yml
FileYou can create a Docker Compose file with the default name docker-compose.yml
or choose a custom name. If you use a different file name, specify it with the -f
option in Docker Compose commands. For example, if you name your file my-compose-file.yml
, you would run:
docker-compose -f my-compose-file.yml up
Custom Name: Use the -f
option to specify a different file name.
This file defines your application’s services, their configurations, volumes, and networks.
The docker-compose.yml file contains :
Example:
##### version: '3.8' Specify the version of Docker Compose syntax to use
services:
web:
##### image: ubuntu Use the official Ubuntu image
##### command: sleep infinity Keep the container running by using a command
networks:
##### - my_network Connect the web service to the custom network
db:
##### image: mysql:5.7 Use the official MySQL image version 5.7
environment:
##### MYSQL_ROOT_PASSWORD: example Set the root password for MySQL
volumes:
##### - db_data:/var/lib/mysql Persist database data using a named volume
networks:
##### - my_network Connect the db service to the custom network
networks:
my_network:
##### driver: bridge Use the default bridge network driver
volumes:
##### db_data: Declare the named volume used by the db service
This example defines two services: web
(an Ubuntu ) and db
(a MySQL database). It also defines a volume db_data
to persist the database data.
docker-compose up .
This command builds (if necessary) and starts all containers, creating the necessary networks and volumes.
To start all the services defined in the docker-compose.yml
file and scale a specific service, use the following command:
docker-compose up --scale web=3
This command will run three instances of the web
service. The --scale
option allows you to specify the number of instances (or replicas) you want to run for a particular service.
To stop and remove the containers, networks, and volumes defined in the docker-compose.yml
file, use:
docker-compose down
This command stops all running containers and removes the resources created by docker-compose up
.
You can view the combined logs from all services with: docker-compose logs [service_name]