Tutorial 29 - Selenium4Grid: Create docker container and setup chrome browser node
Before we proceed with creating chrome/firefox node on docker container, let us first see how to create a simple docker container.
What you will Learn:
Create docker container in foreground (Ubuntu machine)
Start docker container in background (Ubuntu machine)
Create docker container and setup Chrome Browser node
Execute test on docker
Prune docker images and containers
Create docker container in foreground (Ubuntu machine)
Container is simply a running instance of any image.
The command docker container ls -a shows all the containers (whether running or not) and right now we don’t have any container as shown below
Before we go ahead and create our first docker container, just go to https://hub.docker.com/_/ubuntu
As seen below, we can see official ubuntu image on docker hub
Let us see how to create a docker container with ubuntu image running.
The syntax would be:
docker container run <image name> <command you want to run once container is created>
So, our image name would be ‘ubuntu’ and let us try to execute a linux command cat /etc/os-release on this ubuntu OS image once the container gets created, see below
Notice 2 things in the output above.
The first thing is that, the ubuntu image was not present locally and hence was downloaded from the docker hub. The second portion shows the output of linux command cat /etc/os-release
So our first docker container got created within few minutes with an ubuntu application running!!
To list all containers (whether running or not), execute the command: docker container ls -a
Notice above that the container status is shown as ‘Exited’ (because the container stops as soon as it finishes executing the command cat /etc/os-release).
Start docker container in background (Ubuntu machine)
Let us run the container as shown below. The ‘sleep’ command will freeze the current terminal session for 30 seconds
Now, our requirement is, we want to run the command in background so that we can continue working on the same terminal without any disruption.
We can do this by adding -d option (‘d’ option stands for detach)
Notice above that the terminal did not freeze this time. The container got created in background and the container id is shown in the output.
Create docker container and setup Chrome Browser node
Let us now come back to our original selenium 4 grid topic and setup chrome browser node on docker. Ensure you have docker running on your windows OS
Next go to official docker selenium page https://github.com/SeleniumHQ/docker-selenium and scroll down a bit, you would find execution modes section
These are the commands we can use to run docker container in standalone mode.
Copy the docker container command
Paste it in any editor
Remove $ that you see in the beginning
Next select the entire command and copy it. Paste it and execute, you might see an error as shown below
We get this error if docker engine is not running.
Simply double click the ‘Docker Desktop’ to start the engine
You will see below prompt after a minute or so
Try the same command, notice now that we get a different error. It simply means that the docker engine is in the process of starting
Wait for the docker image to get stabilized on your notifications tray
Execute same command, now we don’t get any error, see below
As you can see above, the image is not present locally and hence it is getting downloaded from docker repository. It might take few minutes for the pull to complete. At the end you would see the message ‘Downloaded newer image…’
Execute ‘docker container ls’ command and notice below that a container is running with selenium/standalone-chrome image. The port 4444 is being used
So, let us launch http://localhost:4444
Notice above that selenium grid (hub) is up and running. We also have a node (chrome browser) connected to this hub.
So we are successful is setting up a standalone selenium grid hub and node in a docker container.
Execute test on docker
Let us now execute the same standalone script that we had created in our earlier tutorials
Notice below that 1 session is running
Notice that the test got executed in the background
If you see any errors during script execution, you can stop and restart the container as shown below. For stopping/starting the container, you can type the first 3 letters of the container id
Make sure that hub is up and running
Now execute the same standalone script. You should not see any errors now.
You can see the docker desktop dashboard by clicking the docker icon from your notifications tray
Notice below the docker container running the selenium/standalone-chrome image
Click ‘Images’ on the left hand side, you can see the images running
Once you are done with test execution, make sure to stop the docker container
If you refresh the session, you can see that it is down (since none of the containers is running)
Even dashboard shows container status as EXITED
Prune docker images and containers
Next ‘docker images’ command would list the images that we have pulled down
Now how we get rid of the images, because notice below that we still have ‘Exited’ containers that hold these images?
We execute below command to prune all the stopped containers and all the images
Type ‘y’ and hit Enter
There are no images left
No containers running
Notice below that none of these commands return anything
So this is how we work with Docker. It is highly useful when you want to execute your tests in parallel.
Thank you for reading!