Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

i want to make separate container in docker swarm, that will run headless chromium…

  1. User

    i want to make separate container in docker swarm, that will run headless chromium browser, that i could use from other containters - symfony php apps, through local network or something. could you show me example docker-compose.yml and php usage example

  2. Assistant

    Certainly! To create a setup where you have a headless Chromium browser running in one Docker container and communicate with it from another container (for example, a Symfony PHP application), you can follow these steps.

    Docker Compose Setup

    You'll use docker-compose.yml to define multiple services, one for the Chromium browser and another for your Symfony PHP application.

    Example docker-compose.yml

    version: '3.8'
    
    services:
      # Headless Chromium Browser
      chrome:
        image: browserless/chrome
        ports:
          - "3000:3000"
        environment:
          ENABLE_DEBUGGER: "true"
        networks:
          - my_network
    
      # Symfony PHP Application
      symfony-app:
        image: php:8.1-apache
        volumes:
          - ./symfony-app:/var/www/html
        depends_on:
          - chrome
        networks:
          - my_network
    
    networks:
      my_network:
        driver: overlay
    

    Explanation:

    1. Headless Chromium (browserless/chrome):

      • This service uses the browserless/chrome image, which is a popular choice for running headless Chrome.
      • Ports are exposed, and environment variables are set to enable debugging or other features.
      • The service is part of the my_network network, allowing communication with other services on the same network.
    2. Symfony PHP Application:

      • This service uses a PHP Apache image to run your Symfony application.
      • It mounts the local directory (./symfony-app) to container's web directory (/var/www/html).
      • The depends_on setting ensures the Symfony container waits for the Chrome service to be ready.
      • It is also part of the same my_network.

    PHP Usage Example

    In your Symfony PHP application, you can use a library like puppeteer (via Node.js) or guzzlehttp/guzzle along with chrome-php/chrome to interact with the headless Chrome instance.

    Here's an example using chrome-php/chrome:

    1. Install the library using Composer:

      composer require chrome-php/chrome
      
    2. PHP Usage:

    <?php
    
    require 'vendor/autoload.php';
    
    use HeadlessChromium\BrowserFactory;
    
    try {
        // Connect to the headless Chrome instance running in the separate container
        $browserFactory = new BrowserFactory('http://chrome:3000');
        $browser = $browserFactory->createBrowser();
    
        // Create a new tab and navigate to a webpage
        $page = $browser->createPage();
        $page->navigate('https://www.example.com')->waitForNavigation();
    
        // Get the page's title
        $pageTitle = $page->evaluate('document.title')->getReturnValue();
        echo "Page title is: $pageTitle\n";
    
        // Close the browser
        $browser->close();
    } catch (\Exception $e) {
        echo "An error occurred: " . $e->getMessage();
    }
    

    Notes:

    • Ensure your Symfony app can reach the chrome service via its hostname (chrome in this case, due to Docker's internal DNS resolution).
    • Adjust ports or configurations as needed for your specific network and security settings.
    • The above example assumes you understand how to run Docker Swarm and deploy this setup. You might need to adapt paths and command-line tools if you are using Docker in different environments (e.g., Docker Desktop, production swarm clusters).