DEV Community

Jaira Encio for AWS Community ASEAN

Posted on • Updated on

Setting up Selenium in Docker Container and Pushing Image in Amazon ECR

Overview: Running headless mode selenium in Docker Container then pushing image in Amazon ECR.

What is Amazon ECR?
Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that makes it easy to store, share, and deploy container images.

What is Docker container?
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

File Structure

── /MyFolder/               # root folder
  ├── /main_test.py         # source code main test
  ├── /requirements.txt     # dependencies
  └── /Dockerfile           # docker commands
Enter fullscreen mode Exit fullscreen mode

Code

Copy the following inside main_test.py

import unittest
from selenium import webdriver
from time import sleep


class app_test_case(unittest.TestCase):


    def setUp(self):
        chromeOptions = webdriver.ChromeOptions()
        driver_path = '/usr/local/bin/chromedriver'
        chromeOptions.add_argument('--headless')
        chromeOptions.add_argument('--disable-gpu')
        chromeOptions.add_argument('--no-sandbox')


        self.driver = webdriver.Chrome(driver_path, chrome_options=chromeOptions)
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        path = 'https://www.google.com/'
        self.base_url = path


    def test_i_d_e_script1(self):
        driver = self.driver
        driver.get(self.base_url)

        get_title = driver.title
        print(get_title, "  ", len(get_title))


    def tearDown(self):
        sleep(5)
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()
Enter fullscreen mode Exit fullscreen mode

Copy the following inside requirements.txt

selenium==3.12.0
ipython==7.0.1
Enter fullscreen mode Exit fullscreen mode

Copy the following inside Dockerfile

FROM python:3


WORKDIR /srv


ADD . /srv
RUN apt-get -y update
RUN pip install --upgrade pip
RUN apt-get install zip -y
RUn apt-get install unzip -y


RUN pip install -r requirements.txt


# Install chromedriver
RUN wget -N https://chromedriver.storage.googleapis.com/72.0.3626.69/chromedriver_linux64.zip -P ~/
RUN unzip ~/chromedriver_linux64.zip -d ~/
RUN rm ~/chromedriver_linux64.zip
RUN mv -f ~/chromedriver /usr/local/bin/chromedriver
RUN chown root:root /usr/local/bin/chromedriver
RUN chmod 0755 /usr/local/bin/chromedriver


# Install chrome broswer
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
RUN apt-get -y update
RUN apt-get -y install google-chrome-stable


CMD ["python", "main_test.py"]
Enter fullscreen mode Exit fullscreen mode

Dockerfile is the one that builds a Docker image. The image contains all the dependencies our application needs, including Python itself.

Build and Run New Docker Image

Once Dockerfile and all required config files have been created, we can now build a new docker image

$ docker build -t myapp .
Enter fullscreen mode Exit fullscreen mode

builddocker

Check created image

$ docker images
Enter fullscreen mode Exit fullscreen mode

dockerimage

Run image

$ docker run myapp
Enter fullscreen mode Exit fullscreen mode

Output should be like this:

testoutput

Pushing Image in Amazon ECR

Step 1. Authenticate your Docker client to the Amazon ECR registry to which you intend to push your image

$ aws ecr get-login --region region --no-include-email
Enter fullscreen mode Exit fullscreen mode

Step 2. Copy and paste the docker login command into a terminal to authenticate your Docker CLI to the registry.

Step 3. Tag your image with the Amazon ECR registry, repository, and optional image tag name combination to use. The registry format is: docker tag image_id aws_account_id.dkr.ecr.region.amazonaws.com/myapp

$ docker tag image_id aws_account_id.dkr.ecr.region.amazonaws.com/myapp
Enter fullscreen mode Exit fullscreen mode

(Note: You must first create a repository in ECR)

ecr new repo

Step 4. Push the image using the docker push command: The registry format is: docker tag image_id aws_account_id.dkr.ecr.region.amazonaws.com/myapp

$ docker push aws_account_id.dkr.ecr.region.amazonaws.com/myapp
Enter fullscreen mode Exit fullscreen mode

Jaira Encio Linkedin

Latest comments (0)