Setting up Selenium in Docker Container and Pushing Image in Amazon ECR
Tags: selenium, docker, ecr, automation
Published: 2022-02-17 07:06:03

**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
``` shell
── /MyFolder/               # root folder
  ├── /main_test.py         # source code main test
  ├── /requirements.txt     # dependencies
  └── /Dockerfile           # docker commands
```

## Code


Copy the following inside `main_test.py`
``` shell
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`
``` shell
selenium==3.12.0
ipython==7.0.1
```

Copy the following inside `Dockerfile`
``` shell
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"]
```
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
``` shell
$ docker build -t myapp .
```

![builddocker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38vp2yipffg4axs56pwi.png)

Check created image
```
$ docker images
```

![dockerimage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzac2cjbctln713im2sw.png)

Run image
```
$ docker run myapp
```

Output should be like this:

![testoutput](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjoioacbio8nbaet8cbs.png)


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

``` shell
$ 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`
``` shell
$ docker tag image_id aws_account_id.dkr.ecr.region.amazonaws.com/myapp
```

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

![ecr new repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmxzo3oieh5kszeddqcr.png)

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
```

[Jaira Encio Linkedin] (https://www.linkedin.com/in/jaira-mae-encio-929a68176/)