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

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 imag…


This content originally appeared on DEV Community and was authored by Jaira Encio

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

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()

Copy the following inside requirements.txt

selenium==3.12.0
ipython==7.0.1

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"]

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 .

Check created image

$ docker images

Run image

$ docker run myapp

Output should be like this:
docker output

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

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

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

ecr repo

Step 4. Create a repository in AWS ECS
Step 5. 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

Linkedin
Email: jairaencio@gmail.com


This content originally appeared on DEV Community and was authored by Jaira Encio


Print Share Comment Cite Upload Translate Updates
APA

Jaira Encio | Sciencx (2022-02-17T07:06:03+00:00) Setting up Selenium in Docker Container and Pushing Image in Amazon ECR. Retrieved from https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/

MLA
" » Setting up Selenium in Docker Container and Pushing Image in Amazon ECR." Jaira Encio | Sciencx - Thursday February 17, 2022, https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/
HARVARD
Jaira Encio | Sciencx Thursday February 17, 2022 » Setting up Selenium in Docker Container and Pushing Image in Amazon ECR., viewed ,<https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/>
VANCOUVER
Jaira Encio | Sciencx - » Setting up Selenium in Docker Container and Pushing Image in Amazon ECR. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/
CHICAGO
" » Setting up Selenium in Docker Container and Pushing Image in Amazon ECR." Jaira Encio | Sciencx - Accessed . https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/
IEEE
" » Setting up Selenium in Docker Container and Pushing Image in Amazon ECR." Jaira Encio | Sciencx [Online]. Available: https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/. [Accessed: ]
rf:citation
» Setting up Selenium in Docker Container and Pushing Image in Amazon ECR | Jaira Encio | Sciencx | https://www.scien.cx/2022/02/17/setting-up-selenium-in-docker-container-and-pushing-image-in-amazon-ecr/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.