Laravel Node Asset Compilation in Docker

Although I don’t actually spend a lot of my time developing now, I do still have a few projects to work on in my own time, and will always find the time to tinker and learn about something new.

Something I’ve always done whilst developing with Laravel; using Mix to compile my assets, is to have another terminal window open with npm run watch and just leave it there running whilst I work on it.

What tends to happen is that I either minimise the terminal and forget its running – just to find it sat there a week later doing nothing but chewing up thinking power or I restart/close it and go back to work and it’ll take me a moment to figure out why my views are not updating.

Considering that I almost entirely develop in Docker containers running each project through compose – why I still do this hasn’t really crossed my mind until recently – and by adding a very simple container to my compose yaml file, I’ve now got asset compilation running automatically alongside the other project containers:

When you shut the rest of your development environment with docker-compose down the watch container will also stop.

Note: The volume map and workdir are set for some reason, cant fully remember why – but realistically it doesn’t matter where you put them if you set both the volume path and workdir to the same path.