Deploying Containers using Docker Compose

Docker Compose is a tool that allows you to deploy an application composed by multiple containers. For example, imagine that you want to deploy Joomla. It requires two containers, the web application (which it also runs the web server apache) and the database.

Deploying using the docker CLI

First, let's deploy Joomla using the Docker command line interface.

1 - Run the database container. We do it first because the database is a dependency of the application.

docker run --name db -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

2 - Run the application container, link it to the database and map the port 80

docker run --name my-joomla --link db:mysql -d -p 80:80 joomla

Executing containers in the background

The previous commands allows you to run the containers and all the logs will be shown in the terminal. If you want to run the containers in the background, you need to add the parameter -d

docker run -d --name  db -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Stopping containers

If you want to stop and delete a running container, allowing you to start another container with the same name, you just need to execute the following command. The last parameter is the container name.

docker rm -f db

NOTE: you can get the name of all running containers executing the command docker ps )

Installing Docker

You can install Docker by executing the following command:

curl -sSL | sh

Deploying using Docker Compose

Docker compose makes things more simple. It allows you to create a simple YAML file that will contain everything required to orchestrate both containers. Let's run the previous example using Docker Compose.

Create a file called compose-joomla.yml with the following content:

  image: joomla
    - db:mysql
    - 80:80
  image: mysql

and run docker-compose up

docker-compose up

Installing Docker Compose

If you do not have Docker Compose installed yet, you can do it by executing the following commands:

curl -sL`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Deploying Containers in the Cloud using Manageacloud

Now that we know how to run our application, we can use Manageacloud to deploy the containers in cloud environments, such as Amazon Web Services, Google Compute Engine, DigitalOcean, Rackspace and more.

First, in our account, we create a server configuration, called docker_compose_joomla, using shell, for Ubuntu 14.04 and using the following content:

set -x # enable debug

# install docker curl -sSL | sh
# install docker compose curl -sL`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
# add the configuration for joomla using Docker Compose mkdir ~/compose-joomla cat > ~/compose-joomla/docker-compose.yml << EOL joomla:   image: joomla   links:     - db:mysql   ports:     - 80:80 db:   image: mysql   environment:     MYSQL_ROOT_PASSWORD: example EOL
# execute both containers cd ~/compose-joomla/ && /usr/local/bin/docker-compose up -d

Running applications in the cloud

A server configuration is everything that is required to run applications in the cloud using Manageacloud.

Deploying from the web interface

There are two ways to deploy a server from the web interface:
- By clicking "Quick Deployment" from the server configuration view
- By clicking "Production" or "Testing" from the advanced deployment page

Deploying using the mac cli

You can also deploy from the command line interface:

mac instance create -c docker_compose_joomla

Installing mac Command Line Interface

You can install mac cli by executing the following command:

curl -sSL | bash

Deploying using Manageacloud triggers

The mac cli and the web interface requires credentials and some other minor tweaks to run. However, if you use triggers , you will isolate all that complexity. You can deploy just by executing a POST into an URL.
1 - First, create a new trigger at you account
2 - Comment out the lines that contains the credentials:

export MAC_USER=[...]
export MAC_APIKEY[...]

3 - Add the following line to the trigger

mac instance create -c docker_compose_joomla

Now you can run the trigger, deploying the application, just executing a POST to an URL. Example:

curl -X POST

Triggers and Webhooks

Triggers are bash scripts that isolates all the credentials and complexity. Triggers are specially ideal to run in Webhooks as part of the Continuous Integration or Continuous Deployment pipeline.

Deploying using macfiles

Sometimes is just not good enough to deploy single servers as you need to use infrastructure resources such as load balancers, autoscaling groups and more.

macfile is a technology agnostic framework that allows you to integrate any technology that uses bash . Access to the quickstart guide for more information.

Deploying using API

If you want to integrate the deployment of Joomla with an application, you should use the API to create the server. For example:

$ curl -X POST -i -H "Content-Type: application/json" \
-H "Authorization: ApiKey username:myhashedpass" -d \
'{"hardware": "512mb", "cookbook_tag": "docker_compose_joomla", "location": "sfo1", }' \

Deploying using Manageacloud scripts

Sometimes you need to deploy server configurations in existing servers. In this case, you can deploy using the Manageacloud script, accessible from the deployment page. For example:

curl -sSL | bash


When should I use Manageacloud scripts ?

You can use Manageacloud script in many different cases, for example:
- In the Dockerfile to create the configuration of your container
- If you create development servers using Virtual Box
- and more


Docker Compose is a fantastic tool to deploy containers and microservices in the cloud. Using it along ManageaCloud offer us the flexibility required to deploy applications in the cloud, covering many different use cases and deployment scenarios.

Written by Ruben Rubio Rey on Monday January 11, 2016
Permalink - Category: devops - Tags: devops, docker, docker-compose, orchestration, deployment

« Manageacloud at LinuxConf 2016 - Blue green Deployments using Containers »