Multiple services (Wordpress/Node) best practice
To preface - I am very much a Docker noob, I have expert full stack development technical knowledge but I am just getting my feet wet with CI/CD and docker.
I essentially have a project with two applications. Wordpress is powering the backend API and an Express server is serving a React application. I'm using Docker compose for all of the Wordpress stuff, my docker-compose.yml initiates a mysql and wordpress/apache container. My question is what's best practice regarding adding Node to my setup. Do I just run an init script on the Wordpress container to install Node/NPM and mount a volume with my Node project? Or do I add another service to my docker compose file which adds a Node image?
The end goal here is to use Drone for CI/CD.
TLDR: Node/Wordpress project - Multiple services in docker-compose file or add init script to Wordpress container to install Node?
Edit: For anyone interested what I ended up doing is mounting a init.sh volume and running a script to provision all of the Node stuff and build my project. I have an assumption this is not best practice as this feels a bit like I'm duct taping Node onto a Wordpress/Apache image.
Posted Nov 17, 2017, 3:05 PM UTC · edited
Best practice is to have decoupled containers. So, you should have a container per service. You ideally would not be running a node server on your PHP container. You should run a Node container as a separate service.
This makes sense. However, I'm using Node/Webpack to compile a Wordpress theme for post previewing. At which point does Node belong in its own container for the front end client and in the PHP container for Wordpress?
Node should always be a separate container, in my opinion. If you need to compile the theme what you should be doing is compiling it in the Node container and sharing a volume from your host machine to both the Node container and the PHP container.
You should probably use a multi-stage build on your Wordpress container. What this will allow you to do is start your container build with a node install and do your compilation. Then, you can start a second stage build with your PHP container stuff and simply pass the compiled code from the first stage into the PHP stage.
Docker then discards everything from that first stage except for the newly compiled codebase.
Then you would still have a separate container that contains your Node server and react code.
https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds