0

For hosting a WordPress site on the docker container, I used WordPress and MySQL container images and created a docker-compose.yaml file that made it work perfectly.

Is it possible to create a single image of the running WordPress application (that in the background is using the WordPress and MySQL container images) so that the next time I can move ahead and use that created image and run the image to host the same WordPress site (with WordPress and MySQL database) available inside the same single image instead of using 2 separate images?

1 Answers1

2

It's possible, but it's an anti-pattern.

By jamming multiple unrelated things into a single image, you end up with several problems:

  1. You have to rebuild the image and redeploy your containers every time you want to upgrade either mysql or wordpress. When you have separate images, you can deploy a new version of wordpress without having to restart your database.
  2. You can't take advantage of the upstream-maintained official images (like the official mysql and wordpress images).
  3. You will invariably hide service errors from Docker. With mysql and wordpress running in separate images, the Docker daemon knows when something crashes and can restart it (given an appropriate restart policy), but this isn't possible if you've got something running in the background.
  4. You have dramatically reduced your flexibility: using separate images, you can replace the mysql image with a Galera cluster, or you can run multiple Wordpress containers for load balancing purposes all pointing at the same backend database. If you bundle everything into a single image this becomes less convenient.

That said, your CMD or ENTRYPOINT scripts can do whatever you want: if you want to start something in the background before starting the final foreground process, you can do that! If you want to start up a process supervisor of some sort, you can do that too!

But I think you're better off utilizing the official images as much as possible, and using docker-compose (or another higher-level orchestrator) to compose applications from multiple parts.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Awesome. Would not implement this scenario in the production environment. And as you mentioned that this is possible, can you help me with the steps/process for the testing purpose? – Desh Deepak Dhobi May 18 '22 at 20:17
  • You'll typically want to run the same setup in test environments and production. To make this work, you'd have to build a completely different image, probably not using standard Docker Hub images at all, but building a heavy-weight multi-process environment with an init system with all hand-installed software; it will look nothing like the straightforward Compose setup you show in the question. – David Maze May 18 '22 at 23:05