DOCKER TUTORIAL
VM vs Containers:
Definitions:
Docker Container can be created by docker image.
Docker Hub:
Docker in Automation:
Docker Terminology:
Docker Commands:
Docker run -it image_name – to create container from image
Docker system prune -f – Delete all inactive containers
Docker kill container_name – Kill a containers
Docker run example:
Project Structure Example
cpp
CopyEdit
my-selenium-project/
├── tests/
│ └── test_example.py
├── requirements.txt
├── Dockerfile
└── pytest.ini (optional)
📄 requirements.txt
Include all dependencies:
txt
CopyEdit
selenium
pytest
You may also want to add:
txt
CopyEdit
pytest-html
webdriver-manager
🐳 Dockerfile
This sets up Python with Selenium and runs Pytest:
Dockerfile
CopyEdit
# Use official Python image
FROM python:3.11-slim
# Install dependencies for Selenium (e.g., Chrome & driver)
RUN apt-get update && apt-get install -y \
  wget \
  curl \
  unzip \
  chromium-driver \
  chromium \
  && apt-get clean
# Set environment variables to use Chromium
ENV CHROME_BIN=/usr/bin/chromium
ENV PATH="$PATH:/usr/bin/chromium"
# Set workdir
WORKDIR /app
# Copy project files
COPY . .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Default command to run tests
CMD ["pytest", "--maxfail=1", "--disable-warnings", "-v"]
🧪 Example Test: tests/test_example.py
python
CopyEdit
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def test_google_search():
  options = Options()
  options.add_argument('--headless')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')
  driver = webdriver.Chrome(options=options)
  driver.get("https://www.google.com")
  assert "Google" in driver.title
  driver.quit()
Build and Run the Container
bash
CopyEdit
# Build the image
docker build -t selenium-tests .
# Run the container
docker run --rm selenium-tests
📝 Notes
      This runs tests in headless Chromium.
      Add pytest-html if you want an HTML report.
      For Firefox instead of Chrome, change the Dockerfile to install firefox
       and geckodriver.
How to run locally and with selenium grid:
1. docker-compose.yml for Local Testing (Headless Chrome)
This setup:
Uses a single container.
Runs Selenium + Pytest with Chromium in headless mode.
🔧 Project Files
cpp
Copy
Edit
my-selenium-project/
├── tests/
│ └── test_example.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
└── pytest.ini (optional)
📄 Dockerfile
(Use the same one from earlier, see above)
📄 docker-compose.yml
yaml
Copy
Edit
version: '3.8'
services:
 test-runner:
  build: .
  container_name: selenium_pytest
  volumes:
      - .:/app
  working_dir: /app
▶️Run the Tests
bash
Copy
Edit
docker-compose up --build
✅ 2. Selenium Grid Setup with Chrome/Firefox Nodes
This is a more advanced setup for full browser support, useful for parallel
tests and debugging.
🔧 Project Files
css
Copy
Edit
my-selenium-grid-project/
├── tests/
│ └── test_example.py
├── requirements.txt
├── docker-compose.yml
└── Dockerfile
📄 Dockerfile (Pytest + Selenium only)
Dockerfile
Copy
Edit
FROM python:3.11-slim
WORKDIR /tests
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY tests/ ./tests/
CMD ["pytest", "--maxfail=1", "--disable-warnings", "-v"]
📄 docker-compose.yml
yaml
Copy
Edit
version: '3.8'
services:
 selenium-hub:
  image: selenium/hub:4.18.1
  container_name: selenium-hub
  ports:
    - "4444:4444"
 chrome:
  image: selenium/node-chrome:4.18.1
  depends_on:
    - selenium-hub
  environment:
    - SE_EVENT_BUS_HOST=selenium-hub
    - SE_EVENT_BUS_PUBLISH_PORT=4442
    - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
 firefox:
  image: selenium/node-firefox:4.18.1
  depends_on:
    - selenium-hub
  environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
 test-runner:
  build: .
  depends_on:
      - selenium-hub
  volumes:
      - ./tests:/tests
🧪 Example: test_example.py
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
def test_google_grid():
  options = Options()
  options.add_argument("--headless")
  driver = webdriver.Remote(
       command_executor='http://selenium-hub:4444/wd/hub',
       options=options
  driver.get("https://www.google.com")
  assert "Google" in driver.title
  driver.quit()
▶️Run All with:
bash
Copy
Edit
docker-compose up –build
What is the Difference Between Docker stop and Docker
kill container? Docker stop gracefully stops a container
by sending a SIGTERM signal followed by a SIGKILL
signal after a grace period. Docker kill immediately
stops the container by sending a SIGKILL signal, without
waiting for it to shut down gracefully.