Hello everyone, I am very grateful @michael_k for this post as it has helped me greatly. I also want to share my solution, which may be useful to someone.
1)First of all, we need update docker-compose.yml (we will add chromium service-container) :
chromium:
image: zenika/alpine-chrome
command:
[
chromium-browser,
"--headless",
"--disable-gpu",
"--remote-debugging-address=0.0.0.0",
"--remote-debugging-port=9222",
]
cap_add:
- SYS_ADMIN
volumes:
- tmpfiles:/tmp
ports:
- "9222:9222"
networks:
sail:
ipv4_address: 172.22.0.100
2)The chromium service-container use a static IP, so we need to update our sail network:
networks:
sail:
ipam:
driver: default
config:
- subnet: "172.22.0.1/24"
3)In the next step, you should update volumes:
volumes:
tmpfiles:
driver: local
4)Don't forget add this volume to your service-container with laravel:
services:
laravel.test: # Here will be name of your service-container
...
volumes:
- 'tmpfiles:/tmp' # This line
- '.:/var/www/html'
...
depends_on:
- chromium # This line too
Item 5: Rebuild docker
# Step 1: Stop the containers
./vendor/bin/sail down
# Step 2: Rebuild the containers without cache
./vendor/bin/sail build --no-cache
# Step 3: Start the containers
./vendor/bin/sail up
Thats all, now you can use browsershot like this
$bs = Browsershot::html($html)
->format('A4')
->showBackground()
->margins(10, 10, 10, 10);
if (config('browser-shot.HOST')) {
$bs->setRemoteInstance(config('browser-shot.HOST'), '9222')
->waitUntilNetworkIdle();
}
return $bs->pdf();
for convenience, i have created browser-shot.php in config directory
<?php
return [
'PATH_CHROME' => env('PATH_CHROME'),
'PATH_NODE_MODULES' => env('PATH_NODE_MODULES'),
'HOST' => env('BROWSERSHOT_CHROMIUM_HOST', '127.0.0.1'),
'NODE' => env('NODE', '/usr/bin/node'),
'NPM' => env('NPM', '/usr/bin/npm'),
];
Don't forget add this line to your env file
BROWSERSHOT_CHROMIUM_HOST=172.22.0.100
Have a good day!