Lecture 10 – DOCKER COMPOSE
DOCKER COMPOSE
What Is Docker Compose?
Docker Compose is a tool provided by Docker that allows you to define and manage multi-container Docker applications using a simple YAML file. It is used to orchestrate and coordinate multiple Docker containers to work together as a single application stack. With Docker Compose, you can define the services, networks, and volumes required for your application in a declarative manner.
To use Docker Compose, you need to have Docker installed on your system. Docker Compose is usually bundled with Docker for most platforms, but you can also install it separately if needed. It’s an excellent tool for managing complex applications that consist of multiple interconnected containers and services.
How To Install Docker-Compose
Pre-requisites:
Docker up and running
Step 01: To download docker compose.
COPY & RUN –>$ the below command
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Step 02: Apply executable permissions to the binary.
COPY & RUN –>$ the below command
sudo chmod +x /usr/local/bin/docker-compose
Step 03: To test the installation.
COPY & RUN –>$ the below command
sudo docker-compose --version
Step 04: Create a directory wordpress and navigate into wordpress dir
COPY & RUN –>$ the below commands
mkdir wordpress
cd wordpress
Step 05: Create a docker-compose text document, copy and paste the below yaml code.
COPY & RUN –>$ the below command
nano docker-compose.yml
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
Press ctrl O and press ENTER to save, Press ctrl X to come out of text document
Step 06: To build the docker-compose code.
COPY & RUN –>$ the below command
sudo docker-compose up -d
To access your website, copy your server ip and add port # 8000
Step 07: To shotdown the containers.
COPY & RUN –>$ the below command
sudo docker-compose down
