Docker Compose facilitates to define and run multi-container applications at a single time. Docker Compose make up app services with docker-compose.yml file.
To install docker-compose in your linux ubuntu machine, execute the below command
sudo apt install docker-compose -y
Creating Your First Docker-Compose File
All Docker Compose files are YAML files. Vim editor can be used to create YAML file.
sudo vim docker-compose.yml
version: '3.1' services: mongo: container_name: mongo image: mongo restart: always volumes: - /data/db ports: - 27017:27017 environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: veryeasy1 nginx: container_name: nginx image: 'nginx:latest' ports: - "80:80"
Services and containers are related but both are different things. One or multiple containers can be executed/run by a service. With docker you handle containers and with docker-compose you handle services.
In the above example we have two services mongo and nginx
When we run docker-compose up, then compose will start 2 containers named mongo and nginx
Now let’s try to run our docker-compose file using the following command −
sudo ./docker-compose up
Once executed, all the images will start downloading and the containers will run automatically.
And when we will do docker ps, we will see that the containers are up and running.