Docker 06: What is docker-compose and how to create it?

Docker 06: What is docker-compose and how to create it?

What is Docker Compose:

It is a service or feature of Docker, which helps us to launch and run multiple services in multiple containers on a single host simultaneously.

docker-compose is a .yaml or.yml file, that contains different service details that docker should launch in the docker containers. Each service contains details of image/build, ports, network, scale etc.

To start the docker-compose below command is used.

docker-compose up

To stop the docker-compose, i.e to stop all the containers below command is used.

docker-compose stop

To destroy or delete all the containers of docker-compose below command is used.

docker-compose down

All the above commands must be executed in a terminal or PowerShell in the location where docker-compose.yml is present.

The docker-compose.yml has been evolving with new enhancements, there are different versions of it. Let's look into them.

Docker Compose Versions:

  • In version 1, we do not have a way to specify a network to run the containers in a different network other than the default bridge network.

  • Also, there is no start-up container order to specify in version 1.

  • In version 2 of docker-compose, the above two problems are addressed. A separate network will be created for all the containers in the docker-compose, and they interact with each other within the network.

  • In version 2, the depends-on property is introduced to maintain the start-up containers' order.

  • The latest docker-compose version as of today is version 3. The main feature added in version 3 compared to version 2 is, it supports docker-swarm setup.

A sample docker-compose.yml:

Check the below yaml file, which can be found here docker selenium grid yaml. Create a docker-compose.yml and the details version and services below as per your requirement.

  1. The version of the yaml should be specified in the first line of the file.

  2. Next, the services. You can see in the below file, under services, there are multiple services defined chrome, edge, firefox and selenium-hub.

  3. Each service has its configuration details like image, name, port, network etc.

     # To execute this docker-compose yml file use `docker-compose -f docker-compose-v3-beta-channel.yml up`
     # Add the `-d` flag at the end for detached execution
     # To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3-beta-channel.yml down`
     version: "3"
     services:
       chrome:
         image: selenium/node-chrome:beta
         shm_size: 2gb
         depends_on:
           - selenium-hub
         environment:
           - SE_EVENT_BUS_HOST=selenium-hub
           - SE_EVENT_BUS_PUBLISH_PORT=4442
           - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    
       edge:
         image: selenium/node-edge:beta
         shm_size: 2gb
         depends_on:
           - selenium-hub
         environment:
           - SE_EVENT_BUS_HOST=selenium-hub
           - SE_EVENT_BUS_PUBLISH_PORT=4442
           - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    
       firefox:
         image: selenium/node-firefox:beta
         shm_size: 2gb
         depends_on:
           - selenium-hub
         environment:
           - SE_EVENT_BUS_HOST=selenium-hub
           - SE_EVENT_BUS_PUBLISH_PORT=4442
           - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    
       selenium-hub:
         image: selenium/hub:latest
         container_name: selenium-hub
         ports:
           - "4442:4442"
           - "4443:4443"
           - "4444:4444"
    

This is how a sample docker-compose.yml file looks. Thank you for reading, keep learning. #docker #devops #dockerwithbrahma #brahmakothapalli

Did you find this article valuable?

Support Brahma Rao Kothapalli by becoming a sponsor. Any amount is appreciated!