I know, I know. It is hard to resist some times on treating Docker images like VMs. I totally get it, but I prefer to not do that. You see, there is no sense to put everything in a custom image, every server and every product you develop, with all their necessary libraries. And I will explain myself.
From a size point, you will end up with a huge image, probably more than a GB of size on your system, which will reserve a lot of space. And if this is the only setup, that’s somehow OK. But what will happen if you need to replicate that just to experiment with a new library or a new version of a crucial component? You end up with more setups and a lot of space reserved.
From a security point, huge images tend to have obsolete or unused code and libraries that no one updates because of the fear to break something. It is hard to experiment on updates, hard to rollback and maintain. Even the dockerfiles of these images tend to have hundreds of lines.
From a flexibility point of view, it is difficult to reuse the same images on multiple projects. As a result different projects have different images, strongly tied with them, containing different libraries and scripts. Also, it is hard to change one crucial component, i.e. PHP 5 to PHP 7, in a fast and easy way, without building the whole image from the scratch.
The solution of ‘course to all the above, and this monolithic image, is to use single service containers, based on images that do only one thing, and do it the best way they can.
So, if you are a PHP developer and most of your projects use PHP, Nginx, MariaDB, Redis, Elastic Search, Kibana, the next time you want to create a setup on docker prefer to use different containers for each of the above servers. Separate, i.e. PHP from Nginx and put the containers on the same network. This will give you the necessary flexibility to change, if you need, PHP or Nginx versions really easy, just by changing the image of the specific containers. All the other containers will be untouched, continue to work just fine.
And if you have multiple projects, that use the same technology, use exactly the same images on all of them. And in case you have to maintain those images, I am sure that even the dockerfiles will have much less code, and will be much easier to maintain, than the previous monolithic dockerfile.