Docker compose is an useful tool that used to start multiple services (a.k.a. containers) simultaneously and place them in the same environment and network configurations.
Specifically, we need to first define the services and their configs in a YAML file (docker-compose.yml). After that we can start the services all at once with a single command docker-compose up.
For example, if you want to launch a Dockerized Node.js application that accesses Redis in another container. You can write a docker-compose.yml file like below:
We define containers under the services section. In this example, we define two containers: redis-server and node-app.
The redis-server uses a standard container built from the redisimage from Docker Hub. To use an image directly from Docker Hub or a local machine, we use the image keyword.
The node-app is a Node.js application with redis and express installed. It simply connects to the redis-server to retrieve, store, and display the number of visitors. Since node-app’s Dockerfile is in the same directory as the docker-compose.yml, we can use the build keyword to build the image from the Dockerfile. Finally, we set the port mapping using the ports keyword.