21 January 2019

what is docker-compose

When you wish to run services together and want to run them as single unit then docker-compose is the tool for you, which allows you to run multiple services as kind of microservice by defining them in a single configuration file.
  • docker-compose is a docker tool for defining and running multi containers docker applications.
  • docker-compose allows us to define all the services in a configuration file and with one command it will spin up all the containers that we need.
  • it uses yaml files to configure application services (docker-compose.yml)
  • it uses a single command to start and stop all the services (docker-compose up & docker-compose down)
  • it can scale up services whenever required.
by default, this tool is automatically installed when you are on windows or mac with docker v1.12+

but if you are on Linux try this command given at GitHub for docker-compose


$ curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose \
-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose


alternatively you can find the latest version available here at github

docker-compose.yml prototype will look like:

version:
services:
  image:
network:
volume:


version: first thing first define version of the docker-compose that we are using, there is no restrictions of not to use latest version of compose so I have used '3' here

version: '3'

services: service definition contains configuration which will be applied to each container started for that service, much like passing a command-line parameter to docker run

---
version: ‘3’
services:
webserver:
image: punitporwal07/apache
ports:
- “9090:80” database:
image: mysql
ports: - “4041:3306” environment:
- MYSQL_ROOT_PASSWORD=password - MYSQL_USER=user - MYSQL_PASSWORD=password - MYSQL_DATABASE=demodb
...

so instead of defining items in the docker run command, now we can define it more easily in the configuration file here but with a little bit of syntax

now launch the service using a simple command docker-compose up and it will spin up MySQL and apache in fractions of minutes for you.

--

No comments:

Post a Comment