I'm trying to launch a browser using Selenium and Chrome on Docker but I get "unknown error: DevToolsActivePort file doesn't exist". It works fine on my local machine, but I get this error in Docker.
Here is the Docker configuration and code fragment I am using
Docker File Structure
/my_project
├── Dockerfile
├── my_script.py
├── chromedriver
└── params.yaml
Dockerfile
FROM python:3.10
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["bash"]
my_script.py
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from fake_useragent import UserAgent
ua = UserAgent()
userAgent = ua.chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--window-size=1920x1080")
service = Service('/usr/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=chrome_options)
Since the problem occurs within Docker, I think that my Docker configuration or Dockerfile might be problematic. I would like to ask for help from anyone who has experienced this issue or has a solution. What steps can I take to solve the problem?
Additional Information:
Python version: Python 3.10
ChromeDriver version: 115.0.5790.170
Google Chrome version: 115.0.5790.170
Operating System: Ubuntu 20.04
"I tried to launch a Chrome browser using Selenium within a Docker container. I expected the Chrome browser to start successfully and run automated tasks as it does on my local machine. However, when I run the code inside the Docker container, I encounter the 'unknown error: DevToolsActivePort file doesn't exist' error. This error suggests that Chrome is failing to start within the Docker environment. Despite trying different ChromeDriver versions, adjusting Docker settings, and reviewing my Dockerfile configuration, I couldn't resolve the issue. I expected the Chrome browser to launch and function properly within the Docker container, just as it does on my local machine."