Popular topics: Multilogin X, Multilogin 6,
Running Multilogin in a Docker container
Table of contents
In this article, we've covered the process of creating and launching Multilogin X inside a Docker container. Docker allows you to package and deploy your application in a consistent and portable manner, making it easier to manage across different environments.
By following the steps outlined here, you can use Docker to run Multilogin X on your local machine, in a cloud environment, or on any system that supports Docker, providing flexibility and scalability for your web automation needs.
The instructions in this article apply only to agent versions 1.15.0 and earlier.
Make sure you’re launching Mimic profiles under a standard user account, not with the root user. Chromium browsers, including Mimic, do not function properly when executed with root privileges.
Before you start
- Install Docker by following the official installation guide for your operating system:
- Download, install, and connect Multilogin X agent
- Install Python and libraries for basic automation
Running the script
To run our example script, you will need a Dockerfile, an entrypoint file and an automation script.
Dockerfile
A Dockerfile is a set of instructions for Docker to build an image, much like a recipe for crafting a ready-to-use environment for an application. It outlines what software to include, how to configure it, and which files to add.
- Install the necessary dependencies
Script example
FROM ubuntu:22.04 AS base
# Install basic dependencies
RUN apt-get update && \
apt-get install -y \
xvfb \
ca-certificates \
fonts-liberation \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libatspi2.0-0 \
libc6 \
libcairo2 \
libcups2 \
libcurl4 \
libdbus-1-3 \
libdrm2 \
libexpat1 \
libgbm1 \
libglib2.0-0 \
libgtk-4-1 \
libnspr4 \
libnss3 \
libpango-1.0-0 \
libu2f-udev \
libvulkan1 \
libx11-6 \
libxcb1 \
libxcomposite1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxkbcommon0 \
libxrandr2 \
wget \
xdg-utils \
openjdk-18-jre-headless \
jq \
curl \
unzip \
openssh-client \
python3 \
python3-pip \
libayatana-appindicator3-dev && \
pip install requests selenium && \
curl --location --fail --output mlxdeb.deb "https://mlxdists.s3.eu-west-3.amazonaws.com/mlx/1.15.0/multiloginx-amd64.deb" && \
dpkg -i mlxdeb.deb && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
Make sure that the base image is compatible and all necessary dependencies are properly installed. For instance, Ubuntu 20.04 and Linux distributions built on top of it have an outdated libc6 package, which means they won't function properly with our application.
- Set up a virtual display
Many applications need a graphical interface to interact with. This also applies to browsers and Multilogin. By default, containers don’t have a GUI, so we need to set one for headless automation. You don't need to install a conventional desktop environment like GNOME or KDE – a virtual one will do the trick.
For this, you can use the Xvfb (X Virtual Frame Buffer) package. Xvfb emulates a display that doesn't rely on a physical screen or windowing system, which makes it lightweight and ideal for headless setups.
Run the commands below to set up a virtual display:
ENV DISPLAY=:99
RUN mkdir /tmp/.X11-unix
RUN chown root:root /tmp/.X11-unix && \
chmod 1777 /tmp/.X11-unix
- Create a user
RUN useradd -m mlx-user && \
chown -R mlx-user:mlx-user .
Before running the entrypoint file, switch to this user by adding the line USER mlx-user
.
Make sure you’re launching Mimic profiles under a standard user account, not with the root user.
- Copy the complementary files and set up permissions for them
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY ./env.py /app/mlx-app/
COPY ./main.py /app/mlx-app/
COPY ./mlx_functions.py /app/mlx-app/
- Define the entrypoint.sh file to be run once the container is started
ENTRYPOINT ["/entrypoint.sh"]
Entrypoint file
An entrypoint file is a script or executable that tells Docker how to start the application inside the container. They work together to ensure the application runs correctly and consistently in the container.
- Start virtual display in the background
- Start Multilogin X launcher in the background by using the shortcut “mlx“
- Wait for a few seconds for the launcher to update
- Change the directory to where the automation script is located
- Start the automation script
#!/bin/bash
# Start virtual display
Xvfb :99 -screen 0 1024x768x24 &
# Start Multilogin X launcher
mlx &
# Wait for a while so the launcher is properly installed
sleep 20
# Run Python automation script
cd /app/mlx-app && python3 main.py
Automation script
An automation script conducts some basic scraping tasks with Multilogin X headless browser profiles. Written in Python, it employs the Selenium WebDriver for task automation.
The script performs the following actions:
- Go to a specific website
- Print the website title
- Print the number of articles present on the page
- Print the name of each article
- Conclude the automation process and terminate the driver session
Script example
def automation(driver): # Automation function
try:
driver.get("https://www.scrapethissite.com/pages/") # Go to this website
print(f'The title of the webpage is: "{driver.title}"\n') # Print the title of the website
articles = driver.find_elements(By.CLASS_NAME, "page")
print(f'There are {len(articles)} different articles on this page:\n') # Print how many articles are there
for i, article in enumerate(articles): # For each article, show us what is the name of the article
article_text = article.find_element(By.TAG_NAME, "a").text
print(f"{i+1}. {article_text}")
print("\nBasic automation finished.\n") # Print that the automation is finished
except Exception as e: # Catch expection if any
print(f"Something happened: {e}") # If any exception, print its details
finally:
driver.quit() # Quit driver session
mlx.stop_profile(quick_profile_id) # Close browser profile
Running a Docker container
To build a Docker container, ensure your Docker daemon is running, and then execute the following command in the directory containing your Dockerfile:
docker build -t mlx-image .
Start the container with the following command:
docker run -it --name my_mlx_container mlx-image