Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Using Compose is basically a three-step process:
Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere.Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment.Run
docker-compose up
and Compose starts and runs your entire app.
Install Docker compose file
- DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
- mkdir -p $DOCKER_CONFIG/cli-plugins
- curl -SL https://github.com/docker/compose/releases/download/v2.19.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
- chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
- docker compose version
Example
version: '3.3'
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
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: {}
docker-compose up -d
docker-compose down
Example -2
version: '3.3'
services:
db:
image: ramansharma95/mysql
webapp:
image: ramansharma95/webapp
ports:
- "84:80"
Run the following command in mysql container (docker exec -it <<containerid>> bash)
mysql -uroot -pwhizlabs
create database company;
use company;
create table employee ( name varchar(30), mobile varchar(30));
select * from employee;
Docker Compose Python Example
Docker Compose multi Docker file Example
Docker Compose with Custom Network
Docker Compose with Predefined network
Docker Compose Examples