Selenium Script Dockerization: A Step-by-Step Guide
What is a Docker:
Docker is an open-source platform that utilizes containerization to package applications and their dependencies into portable, standardized units called containers. These containers encapsulate everything needed to run an application, including code, runtime, system tools, libraries, and settings. This encapsulation ensures consistency across different environments, from development to testing and production, eliminating the infamous “it works on my machine” issue.
Critical Concepts of Docker:
Docker Container: Containers are lightweight, isolated, and portable environments that run applications. They operate within the host operating system, sharing its kernel but remaining independent of each other. This isolation provides security and efficiently enables multiple containers to run on a single host.
Images: Docker images are the blueprints for containers. They contain the application code, libraries, dependencies, and configurations required to create a container. Images are stored in repositories and can be shared across teams or individuals.
Dockerfile: A Dockerfile is a text file that contains instructions on how to build a Docker image. It defines the steps needed to create a specific environment for an application. Developers can version control Dockerfiles to maintain consistency and reproduce the same environment across different stages of development.
Docker Compose: Docker Compose files are the final part of the ecosystem you’ll need to familiarize yourself with when using Docker.
These YAML files with the .yml extension contain the specific instructions needed to create the environment to run the Docker Image.
These will download the image if you don’t have it locally, so it can be shared or used to run on another machine quickly. Most importantly, it will run the same way every time in a predictable, efficient way. The Docker Compose file uses a text editor like Notepad++ in Windows or Nano/Vim in Linux.
Benefits of Docker
- Portability and Consistency: Docker containers ensure consistent behaviour across different environments, enabling developers to build once and run anywhere. This portability streamlines development workflows and minimizes compatibility issues.
- Resource Efficiency: Containers share the host system’s kernel, reducing overhead and enabling more efficient resource utilization than traditional virtual machines (VMs). They start quickly and consume fewer resources, facilitating scalability and faster deployment.
- Isolation and Security: Containers provide isolation, enhancing security by isolating applications from each other and the host system. This isolation mitigates risks associated with software vulnerabilities.
- DevOps Enablement: Docker plays a pivotal role in DevOps practices, fostering collaboration between development and operations teams. It enables continuous integration, continuous deployment (CI/CD), and automated workflows, resulting in faster delivery of software updates.
Streamlining Selenium Setup with Docker:
Setting up Selenium can be challenging due to its reliance on various factors, including the specific machine setup, preferred browser, browser version, and operating system, among others. I’ve personally faced hurdles whenever I attempted to set up Selenium, often spending significant time on configuration rather than focusing on actual code development.
However, my experience took a turn for the better when I adopted Docker. This alternative method involves launching a Chrome browser within a Docker container, allowing our Selenium code to interface with the container seamlessly. As a result, this approach ensures consistent performance across different devices and operating systems. Moreover, Docker simplifies the setup process with minimal installation requirements and streamlined code implementation.
I need to give a little understanding of “base image”.
What is a Base Image?
In the context of Docker, a base image refers to the starting point for building a Docker container. It serves as the foundation upon which additional container layers are created. Base images typically contain a minimal operating system environment and may include pre-installed software or configurations specific to a particular use case.
You can read more about Images in detail in this Medium article.
Prerequisites:
There is one prerequisite: to have Docker installed on your system.
To test Docker on your system, run this command.
$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
Here are docker commands for dockerizing your Selenium-based script.
FROM python:3.10
WORKDIR /builders
COPY . /builders
# Install Python dependencies
RUN pip install --upgrade pip
RUN pip install --trusted-host pypi.python.org -r requirements.txt
RUN apt-get update && apt-get install -y wget unzip && \
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
apt install -y ./google-chrome-stable_current_amd64.deb && \
rm google-chrome-stable_current_amd64.deb && \
apt-get clean
CMD ["python", "main.py"]
Lets break down each command and explain them.
- FROM python 3.10: Specifies the base image to use for the Docker image. In this case, it's the official Python image for version 3.10
- WORKDIR /builders: Sets the working directory inside the Docker container to /builders. This is the directory where subsequent commands will be executed.
3. COPY. /builders: Copies the current directory (the directory containing the Dockerfile and application code) into the /builders directory in the Docker container.
4. RUN pip install — upgrade pip: Upgrades the pip package manager to the latest version.
5. RUN pip install — trusted-host pypi.python.org -r requirements.txt: Installs Python dependencies listed in the requirements.txt file. The — trusted-host flag indicates that the specified host is trusted when downloading packages.
6. RUN apt-get update && apt-get install -y wget unzip && \: Updates the package index and installs wget and unzip packages using the apt-get package manager.
7. wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \: Downloads the Google Chrome browser Debian package from the specified URL using wget.
8.apt install -y ./google-chrome-stable_current_amd64.deb && \: Installs the downloaded Google Chrome Debian package using apt.
9. rm google-chrome-stable_current_amd64.deb && \: Removes the downloaded Debian package to save disk space.
10. apt-get clean: Cleans the package cache and removes unnecessary files to reduce the size of the Docker image.
11. CMD [“python”, “main.py”]: Specifies the default command to run when the Docker container starts. It runs the main.py Python script using the Python interpreter in this case.
That’s it. It was that simple to dockerize your Selenium-based script.
If you have any questions or want to share a better way to dockerize Python scripts or projects, don’t hesitate to contact me on my LinkedIn.