Andre0512 / lidl-plus

Fetch receipts and more from Lidl Plus
MIT License
75 stars 17 forks source link

Suggestion: Dockerization #15

Open tomek7667 opened 6 months ago

tomek7667 commented 6 months ago

I would be most appreciative if an example in docs or in practical implementation would be shown on how to run code from the library inside a docker container (preferably with docker-compose).

When running it on a desktop computer seems no problem at all, I have spent an entire day on trying to dockerize the lib but I still didn't manage to run it on a ubuntu server.

tomek7667 commented 6 months ago

If anyone struggle with similar issue, I have managed to setup the containerization properly and this is my Dockerfile:

FROM python:3.11

USER root

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
    apt-get install -y --no-install-recommends wget gnupg2 curl libssl-dev openssl \
    && rm -rf /var/lib/apt/lists/
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get update
RUN apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

COPY main.py /app/main.py
COPY lidlplus /app/lidlplus
COPY requirements.txt /app/requirements.txt
WORKDIR /app

# RUN pip3 install selenium webdriver_manager getuseragent oic selenium-wire pocketbase
RUN pip3 install -r requirements.txt --no-cache-dir
# print "Hello world"
RUN bash -c 'echo "Hello world"'

CMD ["python3", "main.py"]

I had to also change the lidl-plus source code a little bit. The api.py file, method _init_chrome:

    def _init_chrome(self, headless=True):
        user_agent = UserAgent(self._OS.lower()).Random()
        logging.getLogger("WDM").setLevel(logging.CRITICAL)
        options = webdriver.ChromeOptions()
        options.add_argument("--headless")
        options.add_argument("--no-sandbox")
        options.add_experimental_option("mobileEmulation", {"userAgent": user_agent})
        service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())
        chrm = webdriver.Chrome(service=service, options=options)
        return chrm