In this article, we will get through Docker Compose and some real-word applications that use Docker Compose.
1. Overview of Docker and related tools
In recent years, Docker, which is a virtualization software, gains a lot of attractions, popularizes from software development community. Docker introduces a way to “package an application with all its dependencies into a standardized unit for software development” that we can call container. This container, when is delivered to any environment, is guaranteed to be run the same. This makes the software be portable, helps eliminate the risks that the software can not run on some environments because of lacking of prerequisite software, difference of versions….For more detail, you can take a look at the Docker official website.
Docker comes with some supported technologies/tools such as:
- Swarm
A native clustering system for Docker.
- Machine
A tool to automatically sets up Docker on any computer or cloud providers , data centers. If we are familiar with the DevOps term, Docker Machine is a kind of Docker provision tool.
- Kitematic
A simple application with GUI for managing Docker containers on Mac, Linux and Windows
In this article, I’d like to show another Docker supported tool called Docker Compose. This tool will facilitate our operations on Docker containers, Docker images…
2. Introduction to Docker Compose
If you’re familiar with Docker, you can see that it’s easy to pull a docker image from Docker registry and run on your local machine. And it’s easy too to build a Docker image and publish to Docker hub for other people, for your team to use. That could be a simple application. However, what will happen if your application is a little bit more complex day by day, and you want to make it a little bit different with the way are used to? Assume that you have an web application, and you want to use a PostgreSQL docker image from Docker hub and your application image. How can you define all those components, Docker images and configurations for your application? How to link those dockers together? How to easily share some data, information between those docker images in your application?… It’s time for Docker Compose to come into play.
Let’s see a definition from the Docker website:
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration
3. Install Docker Compose
There are several ways to install Docker Compose, below is a simple one:
1 2 |
curl -L https://github.com/docker/compose/releases/download/1.7.1/docker-compose-\ `uname -s`-`uname -m` > /usr/local/bin/docker-compose |
1 |
chmod +x /usr/local/bin/docker-compose |
We can check the version of Docker Compose by:
1 |
docker-compose version |
Here is an example on my PC.
1 2 3 4 5 |
root@geeksgn:~# docker-compose version docker-compose version 1.7.1, build 0a9ab35 docker-py version: 1.8.1 CPython version: 2.7.9 OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013 |
4. Basic steps to use Docker Compose
- Define the app’s environment with a Dockerfile.
- Define all related Docker containers, volumes, ports,…in the docker-compose.yml.
- Use the command: docker-compose up to start entire of the application.
You can get started by follow this example on the Docker website.
5. Some real-world applications used Docker Compose
There are a lot of real-world applications made use of Docker Compose published on internet. In this article, I’d like to introduce a basic one I have used recently. That is Apache Kafka Docker. Her is some information about it you can find.
This is an Apache Kafka docker created by Wurstmeister. It is used to deploy a cluster of Apache Kafka brokers and independent Apache Zookeeper as coordinator for those brokers.
Here is the content of the docker-compose.yml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
zookeeper: image: wurstmeister/zookeeper ports: - "2181" kafka: build: . ports: - "9092" links: - zookeeper:zk environment: KAFKA_ADVERTISED_HOST_NAME: 192.168.59.103 volumes: - /var/run/docker.sock:/var/run/docker.sock |
The above file will create a cluster of Apache Kafka as below image:
Let’s see more detail of the file.
The first part is used to pull the image wurstmeister/zookeeper, create a container and publish the port 2181 to the Docker host machine.
1 2 3 4 |
zookeeper: image: wurstmeister/zookeeper ports: - "2181" |
The second part is used for the Apache Kafka broker.
1 2 3 4 5 6 7 8 9 10 |
kafka: build: . ports: - "9092" links: - zookeeper:zk environment: KAFKA_ADVERTISED_HOST_NAME: 192.168.59.103 volumes: - /var/run/docker.sock:/var/run/docker.sock |
This part:
- Firstly build the current Docker image by looking for the Dockerfile in the current directory.
- Start the container
- Publish the port 9092 to the host machine
- Link the wurstmeister/zookeeper container recently created above
- Create a volume at /var/run/docker.sock in the container and bind with the /var/run/docker.sock on the host machine.
6. Conclusion
Hopefully this article give you an overview about Docker Compose. You can find more useful information in below links if you want to get started.
Below are other articles related to Docker. If you’re interested in, you can refer to the following links:
Copy Files, Folders from Host to Docker Container and Vice Versa
How to Get IP Address of a Docker Container
How To Pull A Docker Image And Run A Container
Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step
Vagrant Docker Provider Tutorial