Running Browsershot with Laravel Sail on M1 Apple silicon #711
Replies: 11 comments 16 replies
-
Works for me (using the same approach on Windows with WSL)! Thanks for sharing Johannes. |
Beta Was this translation helpful? Give feedback.
-
oh my goshhh . 100 points for this |
Beta Was this translation helpful? Give feedback.
-
A little contribution to this great post. I needed to use
Dont know if is possible change the dir where the temp view file stores though. That would be interesting, since we could create custom volume and not necessarily /temp dir. Yet this works for me. Again thnaks @Johannes-Werbrouck for this post |
Beta Was this translation helpful? Give feedback.
-
in my situation when I print google thank you for your help! |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Just come accross this thread and given it a try on my setup but get an error relating to the IP address.
Here is my docker compose.
Does anyone have any ideas about what I might need to do differently? |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I am very grateful @Johannes-Werbrouck for this post as it has helped me greatly. I also want to share my solution, which may be useful to someone. 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! |
Beta Was this translation helpful? Give feedback.
-
Great solution, and some good writeups. Is there a reason that the chromium container must use a static IP? I'd like to get this working over a bridge network and use the built-in name resolution instead - but it seems when I do this, something fails and browsershot is falling back to trying to run chrome in the app container.
|
Beta Was this translation helpful? Give feedback.
-
Would this work similar on amazon graviton instance t4g ? |
Beta Was this translation helpful? Give feedback.
-
Trying to get this up and running, also trying to pass a view to browsershot, not using the url method. I'm getting the error:
Presumably I've misconfigured the volumes or something. I noticed one person on this thread had a solution involving adding a volume for 'tmpfiles:/tmp', and another person added I'm not sure how to get this working. Any ideas? I guess I'm not understanding why it's looking for a file. My
UPDATE: I added this line and now I get UPDATE 2: In case this helps anyone else, I ran: Which allowed it to work. It generated the PDF, however, it has no styling :( but that seems like another issue. |
Beta Was this translation helpful? Give feedback.
-
It's work for me. Thank you @Johannes-Werbrouck |
Beta Was this translation helpful? Give feedback.
-
This is a guide for getting Browsershot to work on Apple silicon macs. As many have experienced, getting Browsershot fully functional in a Docker environment is not easy peasy lemon squeezy on macs with Apple silicon. The main issue is this: One of the requirements is the installation of puppeteer, which will install chromium automatically. But, on M1 (or M2) macs, there is no chromium package available for the arm64 architecture. Now, you could be tricked into setting the platform tot amd64 in your docker compose file, but that will open a can of worms you really want to keep closed. In short, it'll string you along by making you think it's technically possible, but the virtualization will ultimately prevent you from getting the chromium package to work.
So, what is the solution then?
It is possible to install Puppeteer and Browsershot in your laravel container, and point Puppeteer to a different container that has chromium installed. Cue the Alpine-chromium image from Zenika: https://github.com/Zenika/alpine-chrome . This is a small image that just runs chromium headless, exactly what we need!
We'll need to adjust some settings, so here's what I added in my docker-compose.yml:
Notice the networks section: The chromium container gets a static IP. To enable this, we need to change our sail network a bit, it will look like this:
And finally, here's how I use Browsershot:
The chromium container doesn't have these assets on its filesystem, so I'm using url() instead of html() so all the assets (css, js, images) are served correctly from the Laravel container. The showBackground() is to insure that all Tailwind classes are loaded properly.
I hope this helps, and don't hesitate to give feedback!
Beta Was this translation helpful? Give feedback.
All reactions