Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 17, 2022 07:06 am GMT

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 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 unittestfrom selenium import webdriverfrom time import sleepclass 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.0ipython==7.0.1

Copy the following inside Dockerfile

FROM python:3WORKDIR /srvADD . /srvRUN apt-get -y updateRUN pip install --upgrade pipRUN apt-get install zip -yRUn apt-get install unzip -yRUN pip install -r requirements.txt# Install chromedriverRUN 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.zipRUN mv -f ~/chromedriver /usr/local/bin/chromedriverRUN chown root:root /usr/local/bin/chromedriverRUN chmod 0755 /usr/local/bin/chromedriver# Install chrome broswerRUN 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.listRUN apt-get -y updateRUN apt-get -y install google-chrome-stableCMD ["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: [email protected]


Original Link: https://dev.to/awscommunity-asean/setup-selenium-docker-image-in-amazon-ecr-jg9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To