Selenium Grid Setup with Docker
Tags: automation, docker, selenium, testing
Published: 2022-04-15 05:53:25

Hello readers,

from past few days I've been thinking about reducing automation test execution time by creating an environment where I could perform cross-browser and/or parallel execution.

I came-across once solution where we could leverage docker to setup selenium grid on our local machine.

---

### Perquisite(software)
Docker

### Commands to pull required images

> pulling hub image
`docker pull selenium/hub:latest`

> pulling node-chrome
`docker pull selenium/node-chrome:latest`

> pulling node-firefox
`docker pull selenium/node-firefox:latest`

> pulling node-edge
`docker pull selenium/node-edge:latest`

### Commands to setup grid

> creating custom network
`docker network create grid`

> running hub image
`docker run -d --rm -p 4442–4444:4442–4444 --net grid --name selenium-hub selenium/hub:latest`

> running node-chrome image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-chrome -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:latest`

> running node-edge image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-edge -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-edge:latest`

> running node-firefox image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-firefox -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:latest`

### Check container status

`docker ps`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lhsof9ekt08a97o6hf6.png)

all your containers should have **Status** as **Up**

### Grid UI:
navigate to `http://localhost:4444/ui/index.html` on your machine to open grid UI


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11o45vrsjae5j21degvi.png)

---

### Increasing instances for parallel execution on same browser

> creating custom network
`docker network create grid`

> running hub image
`docker run -d --rm -p 4442–4444:4442–4444 --net grid --name selenium-hub selenium/hub:latest`

> running node-chrome image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-chrome --shm-size="2g"  -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:latest`

> running node-edge image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-edge --shm-size="2g" -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-edge:latest`

> running node-firefox image
`docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-firefox --shm-size="2g" -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:latest`

### Grid UI:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4fdsb8401dt4nvr2uq2.png)

---

### Stopping containers & removing network
Once you are done with you testing you can remove container with the help of below command, where you run docker stop command by passing container names to stop them.

> stopping container
`docker stop node-chrome node-edge node-firefox selenium-hub`

> removing docker network
`docker network rm grid`