Part 3:
'What is Docker Compose?
'
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define a multi-container application in a single file, then spin up your application with a single command (docker compose up).
'Key Concepts
'
Services: Each container started by Docker Compose is a service. Services are defined in the compose.yaml file. Networks: By default, Docker Compose sets up a single network for your application. Each container for a service joins the default network and is discoverable via a hostname identical to the container name. Volumes: Volumes can be used to share files between the host and container or between containers.
'Basic docker compose Commands
'
• ` docker compose up`: Starts up the services defined in the compose.yaml file. • ` docker compose down`: Stops and removes all the containers defined in the compose.yaml file. • ` docker compose ps`: Lists the services and their current state (running/stopped). • `docker compose logs`: Shows the logs from the services.
'Deploying !WordPress with Docker Compose
'
Let's deploy a !WordPress application using two containers: one for !WordPress and another for the MySQL database. Create a compose.yaml file:
version: '3'
services:
# Database Service db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress
# WordPress Service wordpress: depends_on: - db image: wordpress:latest ports: - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: - wordpress_data:/var/www/html
volumes:
db_data: {} wordpress_data: {}
'Start the !WordPress and Database Containers:
'Navigate to the directory containing the `compose.yaml` file and run:
`docker compose up -d`
This command will start the services in detached mode. Once the services are up, you can access the !WordPress site by navigating to `http:<MASTER_IP>:8080` from your browser.
'Stopping the Services:
' To stop the services, navigate to the same directory and run:
'docker compose down
'
Best Practices
* Explicit Service Names: Give your services explicit names to make it clear what each service does.
* Environment Variables: Use environment variables for sensitive information and configurations.
* Service Dependencies: Use the depends_on option to ensure services start in the correct order.