Saturday, September 1, 2018

Selenium Docker Configuration With Docker Compose

As per our previous post, we learn how to start Selenium Hub, Nodes and linking container.

Here we will learn how to up Selenium Hub and Nodes with the single command.

Docker Compose is a tool by which we can achieve it. Docker Compose is used to run multiple containers at the same time.

We need to follow the below steps:

Step 1:
 We need to check Docker -compose installed on our machine or not. We can check that using the following commands

                                                       docker-compose version

                                                       docker-compose --version

                                                       docker-compose --v

Step 2:

We need to create a docker compose file, this is a yml file. YML file is an indentation-sensitive file.

This file looks like below:

# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: "3.6"
services:
    selenium-hub:
      image: selenium/hub
      network_mode: bridge
      container_name: hub
      ports:
        - "4446:4444"

    firefox:
      image: selenium/node-firefox-debug
      network_mode: bridge
      container_name: firefox-node
      ports:
        - "5557:5555"
        - "49339:5900"
      environment:
        - NODE_MAX_INSTANCES=5
        - NODE_MAX_SESSION=5
      links:
        - "selenium-hub:hub"

    chrome:
      image: selenium/node-chrome-debug
      network_mode: bridge
      container_name: chrome-node
      ports:
        - "5556:5555"
        - "49338:5900"
      environment:
        - NODE_MAX_INSTANCES=5
        - NODE_MAX_SESSION=5
      links:
        - "selenium-hub:hub"

Step 3:

We can check or verify our yml file before going to deploy using the following command

                                            docker-compose -f <our file path> config


Step 4:

To run the docker compose file we will run the following command:

                                 docker-compose -f  <docker compose file path> up -d


Now run the below command to check that all containers are up and running.

                                                             docker ps -a

We can also check the Grid console from the web browser and it looks like below


Step 5:

We can scale up our containers using the following command:

                docker-compose -f  <docker compose file path> scale firefox=5 chrome=5
Note : If we need to scale up then we need to change the port mapping and remove the container name because it will get conflict issue, so we need to do some changes on our compose file like below:


    firefox:
      image: selenium/node-firefox-debug
      network_mode: bridge
      ports:
        - "5555"
        - "5900"


Step 6:
We can stop and remove all the containers using the following commands

                            docker-compose -f  <docker compose file path> stop
                            docker-compose -f  <docker compose file path> down

Note :

Above steps are related with link on bridge network, we can also compose the docker file for user defined bridge network using the following docker compose file:


# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: "3.6"
services:
  selenium-hub:
    image: selenium/hub:3.14.0-beryllium
    container_name: selenium-hub
    ports:
      - "4446:4444"
    networks:
      - selenium_grid

  firefox:
    image: selenium/node-firefox-debug
    depends_on:
      - selenium-hub
    ports:
      - "5557:5555"
      - "49339:5900"
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444

      - NODE_MAX_INSTANCES=5
      - NODE_MAX_SESSION=5
    networks:
      - selenium_grid

  chrome:
    image: selenium/node-chrome-debug
    depends_on:
      - selenium-hub
    ports:
      - "5556:5555"
      - "49338:5900"
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444

      - NODE_MAX_INSTANCES=5
      - NODE_MAX_SESSION=5
    networks:
      - selenium_grid

networks:
    selenium_grid: