Build Applications with Docker Compose Examples

Docker Compose is an orchestration tool for Docker that allows you to define a set of containers and their interdependencies in the form of a YAML file. You can then use Docker Compose to bring up part or the whole of your application stack, as well as track application output, etc. Build applications with docker compose and use it on your continues delivery.

If you have started working with Docker and are building container images for your application services, you most likely have noticed that after a while you may end up writing long `docker run` commands. These commands while very intuitive can become cumbersome to write, especially if you are developing a multi-container application and spinning up containers quickly.

docker compose example

Docker Compose also allows you to manage your application as a single entity rather than dealing with individual containers.

Install Docker Compose

You can install Docker Compose on macOS, Windows and 64-bit Linux OS.

Uset this command to install on your linux OS, download Docker Compose, replacing $dockerComposeVersion with the specific versions.

# curl -L https://github.com/docker/compose/releases/download/$dockerComposeVersion/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Example to download Docker Compose in linux with docker 1.13

# curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker-compose --version
docker-compose version 1.13.0, build 1719ceb

 

Install using pip

If you install using pip, use this command but make sure python system packages that conflict with docker-compose dependencies.

# pip install docker-compose

Install inside a container

Simple bash script wrapper will install it.

# curl -L --fail https://github.com/docker/compose/releases/download/1.13.0/run.sh > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

Docker compose sample deployment

After you installed docker compose successfully, create a YAML file which will contain the docker image and environment details.

Create a docker-compose.yml file with your WordPress blog and MySQL container with persistence data using volume mount.

version: '2'

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_pa33

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_pa33
volumes:
db_data:

 

That’s it. Now, run this command from your project directory.

# docker-compose up -d

Once pull all images and start containers, wait for few seconds to initialize the database, etc and open http://MACHINE_IP:8000 in a browser.

Docker Compose example commands

You can start the containers with the up command in daemon mode (by adding -d as a param) or by using the start command:

# docker-compose start

Stopping containers

# docker-compose stop

Remove containers

To stop and remove all the containers use the down command

# docker-compose down

or the rm command if the containers are stopped already.

# docker-compose rm --all