Docker Compose :
docker-compose help us to create a configuration file in yaml format(docker-compose.yml) and put together the services and the options specific to run them.
docker-compose up command will bring up the entire application stack.
This is very easy to implement, run and maintain since all the configuration are at one place.
It is only applicable to running application on a single docker host machine.
Sample application - voting machine
To understand the docker-compose concepts lets go through the Sample application developed by Docker - voting application
This Sample application has build upon various development platform such as docker, nodejs, .NET and different services such as redis, PostgreSQL.
Architecture :
# Using docker run :
docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 voting-app
docker run -d --name=result -p 5001:80 result-app
docker run -d --name=worker worker
All the Above services/container runs successfully however UI will not be accessible because we haven't link them together. How the container running independentely will know the process running outside?.
To overcome this issue we can use link command-line options. It help us to link the two container together.
We will get Depercation Warning when we use link. To learn the concept we have used here and later in this section we will see the alternative of this.
# Using docker run :
docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
docker run -d --name=result -p 5001:80 --link db:db result-app
docker run -d --name=worker --link redis:redis --link db:db worker
Create docker-compose.yml
We can create docker-compose.yml file out of the above command.
# docker-compose.yml :
redis:
image: redis
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
links:
- redis
result:
image: result-app
ports:
- 5001:80
links:
- db
worker:
image: worker
links:
- db
- redis
Since the voting-app, result and worker are the application code which reside in host machine. we can replace the image to build command in docker-compose.yml file.
# Run the docker-compose.yml file:
docker-compose up
Since the voting-app, result and worker are the application code which reside in host machine. we can replace the image to build command in docker-compose.yml file.
# docker-compose.yml :
redis:
image: redis
db:
image: postgres:9.4
vote:
build: ./vote
ports:
- 5000:80
links:
- redis
result:
build: ./result
ports:
- 5001:80
links:
- db
worker:
build: ./worker
links:
- db
- redis
docker-compose - Versions
The abover created docker-compose.yml file is origional version or version: 1 of docker-compose.
Limitations with Version: 1 of docker-compose.yml
- We can not deploy the container on other network, There is no option to specify that.
- We can not define the dependencies within the container. In the above example if we want to bring up the database first and then worker. We won't be able to specify that.
From Version 2 and above the format of the docker-compose file is changed. We need specify all the stack information inside of services section.
From Version 2 and above we must have to provide the version of docker-compose.
From Version 2 and above we do not have to use links as it automatically create a dedicated network for this application and attaches all these container to that network and hence all the container will be able to communicate to each other using service name.
From Version 2 and above we have depends_on property to define the dependencies.
# docker-compose.yml Version 2:
version: 2
services:
redis:
image: redis
networks:
back-end:
db:
image: postgres:9.4
vote:
build: ./vote
ports:
- 5000:80
depends_on:
- redis
networks:
-front-end
-back-end
result:
build: ./result
ports:
- 5001:80
networks:
-front-end
-back-end
worker:
build: ./worker
networks:
front-end:
back-end:
Version 3 supports docker docker swarm.
# docker-compose.yml Version 3 :
version: 3
services:
redis:
image: redis
db:
image: postgres:9.4
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
vote:
build: ./vote
ports:
- 5000:80
depends_on:
- redis
result:
build: ./result
ports:
- 5001:80
worker:
build: ./worker