c2c — Docker Compose to ContainerShip Converter

 

When you are just starting out with Docker, you will spend some time reading through the documentation, getting familiar with the steps needed to containerize your app.

Step one is creating a Dockerfile to build each of your codebases into a Docker image. Once that is working, you will likely end up creating a docker-compose.yml file that describes a multi-tiered application, and how those tiers interact to create a functioning system. An example might be a 2-tiered application made up of wordpress and a mysql database.

At this point you are able to run the “stack” locally via the docker toolbox or boot2docker to test it out and play with it.

The next hurdle is taking your docker-compose file, and using it to deploy to a multi-server setup running on your cloud hosting provider of choice. This is where many users get tripped up and require assistance.

You’ll need to understand more advanced features like container links, cluster schedulers, service discovery, overlay networks, and many things that the average developer has little understanding of. The documentation quickly gets complicated, and those without a system administration background are likely to get lost.

Luckily with the help of ContainerShip, you can avoid having to setup all of those complex pieces by hand, and can focus on what matters: deploying and scaling your application. ContainerShip Cloud lets you add credentials for your cloud provider, choose a few options, and moments later have a running cluster where you can deploy applications.

Our new tool c2c will take a docker-compose.yml file and convert it into the JSON format expected by the ContainerShip API.

Example

To get started, install the c2c utility:

npm install c2c -g

Now lets look at an example docker-compose.yml file. This file is designed to setup a monitoring stack made up of Google cAdvisor for metrics collection, InfluxDB for metrics storage, and Grafana for data visualization.

InfluxSrv:
image: “tutum/influxdb:0.8.8”
ports:
— “8083:8083”
— “8086:8086”
expose:
— “8090”
— “8099”
environment:
— PRE_CREATE_DB=cadvisor
cadvisor:
image: “google/cadvisor”
volumes:
— “/:/rootfs:ro”
— “/var/run:/var/run:rw”
— “/sys:/sys:ro”
— “/var/lib/docker/:/var/lib/docker:ro”
links:
— “InfluxSrv:influxsrv”
ports:
— “8080:8080”
command: “-storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_host=influxsrv:8086”
grafana:
image: “grafana/grafana:2.0.2”
ports:
— “3000:3000”
environment:
— INFLUXDB_HOST=localhost
— INFLUXDB_PORT=8086
— INFLUXDB_NAME=cadvisor
— INFLUXDB_USER=root
— INFLUXDB_PASS=root
links:
— “InfluxSrv:influxsrv”

Now to convert this into ContainerShip format, just run c2c in the same directory as this docker-compose.yml file. You will get ContainerShip JSON as output.

{
“InfluxSrv”: {
“id”: “InfluxSrv”,
“image”: “tutum/influxdb:0.8.8”,
“env_vars”: {
“PRE_CREATE_DB”: “cadvisor”
},
“tags”: {},
“command”: “”,
“cpus”: 0.1,
“memory”: 256,
“respawn”: true,
“network_mode”: “host”,
“containers”: [],
“volumes”: []
},
“cadvisor”: {
“id”: “cadvisor”,
“image”: “google/cadvisor”,
“env_vars”: {
“influxsrv_PORT_8083_TCP_ADDR”: “$CS_ADDRESS_INFLUXSRV”,
“influxsrv_PORT_8083_TCP_PORT”: “8083”,
“influxsrv_PORT_8086_TCP_ADDR”: “$CS_ADDRESS_INFLUXSRV”,
“influxsrv_PORT_8086_TCP_PORT”: “8086”
},
“tags”: {},
“command”: “-storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_host=influxsrv:8086”,
“cpus”: 0.1,
“memory”: 256,
“respawn”: true,
“network_mode”: “bridge”,
“containers”: [],
“container_port”: “8080”,
“volumes”: [
{
“host”: “/”,
“container”: “/rootfs”
},
{
“host”: “/var/run”,
“container”: “/var/run”
},
{
“host”: “/sys”,
“container”: “/sys”
},
{
“host”: “/var/lib/docker/”,
“container”: “/var/lib/docker”
}
]
},
“grafana”: {
“id”: “grafana”,
“image”: “grafana/grafana:2.0.2”,
“env_vars”: {
“INFLUXDB_HOST”: “localhost”,
“INFLUXDB_PORT”: “8086”,
“INFLUXDB_NAME”: “cadvisor”,
“INFLUXDB_USER”: “root”,
“INFLUXDB_PASS”: “root”,
“influxsrv_PORT_8083_TCP_ADDR”: “$CS_ADDRESS_INFLUXSRV”,
“influxsrv_PORT_8083_TCP_PORT”: “8083”,
“influxsrv_PORT_8086_TCP_ADDR”: “$CS_ADDRESS_INFLUXSRV”,
“influxsrv_PORT_8086_TCP_PORT”: “8086”
},
“tags”: {},
“command”: “”,
“cpus”: 0.1,
“memory”: 256,
“respawn”: true,
“network_mode”: “bridge”,
“containers”: [],
“container_port”: “3000”,
“volumes”: []
}
}

If you’d like to save the output to a specific file, you can use the -o flag.

c2c -o monitoring-stack.json

Now you can do a POST to the ContainerShip API to deploy this stack to one of your ContainerShip clusters.

curl -X POST http://leader-host-ip-address:8080/v1/applications -d@monitoring-stack.json -H "Content-Type: application/json"

Using directly from the containership CLI tool

c2c is built into the ContainerShip command line interface via the create-from-file command.

For example, assuming you have your docker-compose.yml file in the current working directory, you can run the following command to deploy to the current cluster your CLI is configured to use.

cs app create-from-file

Extending docker-compose.yml with ContainerShip specific settings

Sometimes the vanilla docker-compose is not powerful enough for production application deployments. By creating a containership-compose.yml, you can specify additional settings such as constraints, tags, and the number of containers that should run.

Here is an example containership-compose.yml file that overrides some settings from our monitoring stack, setting the number of containers to run, and forcing our InfluxDB to run on every follower host in the cluster.

Grafana:
containers: 1
InfluxSrv:
tags:
constraints:
per_host: 1

All you need to do is specify both files when using create-from-file

cs app create-from-file docker-compose.yml containership-compose.yml
Show Comments

Get the latest posts delivered right to your inbox.