Docker Compose is a tool for characterizing and running multi-container Docker applications. With Compose, you utilize a YAML record to configure your application’s services. At that point, with only one command, you make and begin every one of the services from your configuration.
Want to read more about Docker Hub?
Compose works in all situations: creation, arranging, advancement, testing, just as CI workflows.
Utilizing Compose is essentially a three-advance procedure:
- Characterize your application’s condition with a Dockerfile so it very well may be replicated anyplace.
- Characterize the services that make up your application in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-make up and Compose begins and runs your whole application.
docker-compose.yml looks like this:
Docker Compose has commands for dealing with the entire lifecycle of your application:
- Begin, stop, and revamp services
- View the status of the running services
- Stream the log yield of running services
- Run a one-off command on a service
Features of Docker Compose
The Features of Compose that make it powerful are:
- Different confined environments on a solitary host
- Save volume information when containers are made
- Just reproduce containers that have changed
- Factors and moving a composition between environments
Different confined environments on a solitary host
Compose utilizes a project name to confine environments from one another. You can utilize this project name in a few unique contexts:
- on a dev host, to make different duplicates of a solitary environment, for example, when you need to run a steady duplicate for each component part of a project
- on a CI server, to shield works from meddling with one another, you can set the project name to a unique build number
- on a common host or dev host, to counteract various tasks, which may utilize similar service names, from meddling with one another
Save volume information when containers are made
Compose safeguards all volumes utilized by your services. At the point when docker-compose up runs, on the off chance that it finds any containers from past runs, it duplicates the volumes from the old container to the new one. This procedure guarantees that any information you’ve made in volumes isn’t lost.
In the event that you use docker-compose on a Windows machine, see Environment variables and alter the important environment variables for your particular needs.
Just reproduce containers that have changed
Compose reserves the setup used to make a container. When you restart a service that has not changed, Compose re-utilizes the current containers. Re-utilizing containers imply that you can make changes to your environment all-around rapidly.
Factors and moving a composition between environments
Compose underpins variables in the Compose document. You can utilize these variables to modify your composition for various environments or various users. See Variable substitution for more subtleties.
You can expand a Compose document utilizing the broadens field or by making numerous Compose files. See extends out for more subtleties.
Docker Compose ─ Installation
The accompanying advances should be pursued to get Docker Compose going.
Step 1 − Download the important files from github using the following command −
curl -L "https://github.com/docker/compose/releases/download/1.10.0-rc2/dockercompose -$(uname -s) -$(uname -m)" -o /home/demo/docker-compose
The above command will download the most recent version of Docker Compose which at the season of composing this article is 1.10.0-rc2. It will at that point store it in the directory /home/demo/.
Stage 2 − Next, we have to give execute privileges to the downloaded Docker Compose file, utilizing the following command –
chmod +x /home/demo/docker-compose
Then we can use the following command to see the compose version.
- version− This command is used to specify that we want the details of the version of Docker Compose.
The version details of the Docker Compose will be displayed.
The following example shows how to get docker-compose version.
sudo ./docker-compose -version
Then you will get the following output −
Creating Your First Docker-Compose File
Presently how about we feel free to make our first Docker Compose record. All Docker Compose files are YAML files. You can make one utilizing the vim editor. So execute the accompanying command to make the compose file –
sudo vim docker-compose.yml
We should investigate the different subtleties of this file −
- The database and web keyword are utilized to characterize two separate services. One will run our mysql database and the other will be our nginx web server.
- The image keyword is utilized to determine the image from dockerhub for our mysql and nginx containers
- For the database, we are utilizing the ports keyword to make reference to the ports that should be uncovered for mysql.
- And after that, we likewise determine environment variables for mysql which are required to run mysql.
Presently how about we run our Docker Compose file utilizing the following command –
sudo ./docker-compose up
The above command will take the docker-compose.yml file in your local directory and start building the containers.
Once executed, all the images will start downloading and containers will start automatically.
And when you do a docker ps, you will be able to see that the containers are indeed up and running.
Want to read more about Docker Documentation?