How can I run Symfony Panther in a Docker container?

Running Symfony Panther in a Docker container involves setting up a Docker environment where you can run a headless browser along with your PHP/Symfony application. To achieve this, you can use the official Docker images for PHP and a headless browser, such as Chrome or Firefox, with respective drivers like ChromeDriver or GeckoDriver.

Here's a step-by-step guide to setting up Symfony Panther to run in a Docker container:

Step 1: Create a Dockerfile for Your PHP Application

Create a Dockerfile in your Symfony project root directory to set up the PHP environment.

# Use the official PHP image with the version that matches your Symfony project
FROM php:7.4-fpm

# Install system dependencies (you might need more depending on your project)
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libpng-dev \
    libonig-dev \
    libxml2-dev

# Clear out the local repository of retrieved package files
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions (you might need more depending on your project)
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Set the working directory
WORKDIR /var/www

# Copy the application code to the container
COPY . /var/www

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Install dependencies with Composer
RUN composer install --no-interaction

# Set user permissions
RUN chown -R www-data:www-data /var/www

# Expose the port the app runs on
EXPOSE 9000

Step 2: Set Up Selenium/ChromeDriver in Docker

Create a docker-compose.yml file to define the services needed to run your application and the headless browser.

version: '3.7'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www
    depends_on:
      - chrome

  chrome:
    image: selenium/standalone-chrome:latest
    ports:
      - "4444:4444"

This docker-compose.yml file defines two services:

  1. app: This is your PHP application built using the Dockerfile.
  2. chrome: This is the Selenium server with Chrome and ChromeDriver pre-installed.

Step 3: Configure Symfony Panther

Configure Symfony Panther to use the standalone Chrome service for its tests by setting environment variables in your .env or .env.test file:

PANTHER_NO_SANDBOX=1
PANTHER_WEB_SERVER_PORT=9000 # Ensure this port matches the one exposed in the Dockerfile
PANTHER_CHROME_ARGUMENTS='--disable-dev-shm-usage'
PANTHER_EXTERNAL_BASE_URI=http://localhost:9000

Step 4: Running the Containers

Start the containers using docker-compose:

docker-compose up -d

Step 5: Running Symfony Panther Tests

After the containers are up and running, you can execute your Symfony Panther tests using the following command:

docker-compose exec app ./vendor/bin/phpunit

This command will run PHPUnit tests inside the app container, and Symfony Panther will connect to the Chrome instance running in the chrome container.

Additional Configuration

Depending on your application's needs, you might have to adjust the Dockerfile, docker-compose.yml, and PHP/Symfony configurations. The above example provides a basic setup that should be tailored to fit the specifics of your application.

Remember that the Docker setup can vary based on your application's requirements and the versions of PHP and Symfony you are using. Always refer to the official documentation for the most accurate and up-to-date instructions.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon