image source medium |
How to use Docker compose.
If you are using docker for your application and you are using more than one container for your app, then you have to use docker compose for app and it’s too time consuming, example: web server with database will run from same file (WordPress,apache OR mysql). Docker compose allow you to use YAML file to run deploy multi container apps. We can configure multiple container we want. In the docker file it describe how app will build, connect and where data stored.
To run docker compose, You have to install docker and docker compose in your PC.
To install docker click here.
- Install docker compose.
Download latest version of docker compose.
$ curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- Give the permission to the binary.
$ chmod +x /usr/local/bin/docker-compose
- Now we will install wordpress site along with mysql using docker compose. let’s create docker compose file.
$ vim docker-compose.yaml
version: '3'
services:
database:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql1
restart: always
environment:
MYSQL_ROOT_PASSWORD: yourRootpassword
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: yourpassword
wordpress:
depends_on:
- database
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: yourpassword
volumes:
db_data:
Save the file and run the docker compose from same directory.This file will build database and WordPress containers. it will run docker with -d flag with detached mode.
- Run docker compose.
$ docker-compose up -d
- You can check the docker status of the file.
$ docker ps
Download latest version of docker compose.
$ curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
version: '3'
services:
database:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql1
restart: always
environment:
MYSQL_ROOT_PASSWORD: yourRootpassword
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: yourpassword
wordpress:
depends_on:
- database
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: yourpassword
volumes:
db_data:
$ docker-compose up -d
$ docker ps
- To remove the docker composer.
$ docker-compose down
Check from browser with localhost and configure with your settings.
Linuxguru