docker-composeによるPython + Selenium環境

docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
version: '3.8'
services:
  app:
    build: ./app/
    entrypoint: [ "wait-for-it", "selenium:4444", "--", "python3", "/code/main.py" ]
    volumes:
      - ./work:/work
    environment:
      SELENIUM_URL: http://selenium:4444/wd/hub
    depends_on:
      - selenium
  selenium:
    image: selenium/standalone-chrome
    volumes:
      - /dev/shm:/dev/shm

app/Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM python:3

WORKDIR /work

RUN apt update && apt install -y \
  wait-for-it

ADD requirements.txt /tmp/
RUN pip3 install -r /tmp/requirements.txt

ADD code/ /code

app/requirements.txt

1
requests >= 2.24.0

app/code/main.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

selenium_url = os.environ['SELENIUM_URL']
website_url: str = None

driver = webdriver.Remote(
    command_executor=selenium_url,
    desired_capabilities=DesiredCapabilities.CHROME,
)

driver.get(website_url)