Docker: PHP, Apache and MySQL in the same container / the same Dockerfile

Viewed 2840

I need an image with all the 3 elements combined in one container. But however I Google'd or searched over Docker Hub there I found no images to hold PHP, Apache and MySQL in the same container / Dockerfile.

Can anybody suggest the way to create the desired container?

I would also appreciate the explanation for the rationale of why this kind of images/containers is not available?

As a side note I myself cannot imagine a point making a separation of the MySQL server in the different container feasible due to the fact that the DB itself is not saved with the container and there is only MySQL server in it. So even MySQL being in the same container still perfectly decoupled from the DB itself.

3 Answers

Good Morning bob-12345

The Point of docker containers is to get a more flexible architecture.

I will make a Example from my work life:

You have an application thats run on php7.0 you now want to test if it still works with php 7.3 so you just stops the php 7.0 container and starts the 7.3 one and checks your application. Or you want to check if it still works with ngnx and php-fpm and so on...

So it is not recommended to put your webserver your coding layer and your persisent layer in one container due versioning issues.

If you still want to have it all in one. I would suggest you start with a plain debian/ubuntu/whatEverDistroYouPrefer and start building like you would do it on a regular webserver.

For Example:

FROM debian:latest
RUN apt-get update \ 
&& apt-get install -y --no-install-recommends \ 
&& http \
&& php \ 
...

I hope Icould Help you :)

PHP and Apache together in the same container is a good idea - in a practical sense, PHP is only really useful when combined with a webserver (generally speaking). The same cannot be said for MySQL. There are real world problems around scalability, redundancy and availability that cannot be solved if the database server and volume is bundled up with the application code.

Lets say you've deployed your "LAMP Image" into production and your website becomes really popular - cool! You're receiving a lot of traffic and you need multiple instances of your application running. You also need a read-replica of your database.

How are you going to do that if everything is bundled into a single image? You're forced to run the same number of MySQL instances as the number of applications.

The later me to answer the question of the earlier me.

The need to have PHP and MySQL in the same container comes from a wish to make the portable package of these.

As later me sees it, the solution is not in combining both in the same container but instead under the same Docker Compose docker-compose.yml that easily connects 3 containers (Apache, PHP, MySQL).

Docker Compose is Docker containers orchestrator. It can start, stop, remove, recreate a package of containers. So it makes the package portable.

The quick Google search for docker-compose php apache mysql reveals a number of useful examples of docker-compose.yml. This is just one of them.

This is so much easier and fun with Docker / Compose as soon as you get used to it.

Related