Php: Re-enable mysql in php5.4

Created on 15 Apr 2015  路  16Comments  路  Source: docker-library/php

Hi guys, I love docker and I'm transitioning our company to using docker for everyone's dev environment.

A year ago, all I had to do was pull the mysql:latest and php5.4-apache images, and DONE. our site was up and running on a local developer's machine.

As of Nov 10, it's broken. I understand that all extensions were removed, but please consider that the standard php5.4 lamp stack has always included mysql. We need it for a legacy app we maintain.

Now it seems we can't just pull an off the shelf image, like we used to pull an off the shelf LAMP stack, to run. IMHO mysql mysqli and mysql pdo should be included always, especially in php5.4 where that was THE standard at the time, so anyone still using 5.4 is probably doing it because of mysql.

Anyways, I don't think individuals should have to maintain and build dockerfiles just to add extensions that everybody always had in php5.4. In 5.6+ I fully support it because nowadays it's not a given that you're using a mysql database.

So I just wanted to let you know that update broke our company's nice docker off-the-shelf solution. But if you all still agree that even in php5.4 mysql should not be included, please provide us the new way, as I was not able to find any documentation of specifically enabling mysql, mysqli, and pdo with the new ext-install script.

So before it broke, this is what our company did to go from a folder with the website in it to a full running website:

sudo docker run --name kcity-mysql -e MYSQL_ROOT_PASSWORD=password123 -e MYSQL_USER=knowledgecity -e MYSQL_PASSWORD=password123 -e MYSQL_DATABASE=test -d mysql

sudo docker run -it --link kcity-mysql:mysql -v "$(pwd)":/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p < /tmp/import/sample_data.sql'

sudo docker run -d -p 80:80 --name kcity-web -v "$(pwd)":/var/www/html --link kcity-mysql:mysql  php:5.4-apache

We just hired some new developers and they're like "why make things complicated by using docker?" since we weren't able to get their dev environment set up with 3 commands like we did before the November change.

Thanks for your help

Most helpful comment

@booyaa Thanks!! I did that, and also included regular mysql and pdo, and also added a RUN for enabling mod_rewrite on apache, which we were previously doing by an exec command.

For anyone who comes upon this issue, be it known that the following Dockerfile works as expected:

FROM php:5.4-apache
RUN docker-php-ext-install mysql mysqli pdo pdo_mysql
RUN cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

I'll defer to ya'all's judgement in removing all the extensions, and I thank you very much for the magic install extensions script.

I have 2 questions to finish up this issue and have our company's dev workflow back on track:

First, should I have 2 separate RUN commands like that or should I do that && thing?

Second, would it make more sense to include the Dockerfile in our code repository, and so have devs clone the repository, then build the Dockerfile, then run the containers, or does it make more sense to store the new image on docker hub and have them download it by running the containers?

All 16 comments

Speaking in an unofficial capacity I kinda understand why Docker don't want to put anything extra into an image, because there will be another person who wants postgres and doesn't need the overhead of a mysql bindings.

I haven't had a chance to test, but this should be easily remedied by creating a custom php build (basing this off the wordpress image which builds off php).

Dockerfile

FROM php:5.4-apache
RUN docker-php-ext-install mysqli

Then you can do docker build -t php-mysql:5.4-apache . and issue docker run -d -p 80:80 --name kcity-web -v "$(pwd)":/var/www/html --link kcity-mysql:mysql php-mysql:5.4-apache

The bonus being you could even include a COPY of your website into the container, to simplify that docker run.

As a side note, have you looked at using docker-compose? It's really handy, once you go beyond a single docker run and in turn each command has a dependency on the previous.

@booyaa :+1:

@booyaa Thanks!! I did that, and also included regular mysql and pdo, and also added a RUN for enabling mod_rewrite on apache, which we were previously doing by an exec command.

For anyone who comes upon this issue, be it known that the following Dockerfile works as expected:

FROM php:5.4-apache
RUN docker-php-ext-install mysql mysqli pdo pdo_mysql
RUN cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

I'll defer to ya'all's judgement in removing all the extensions, and I thank you very much for the magic install extensions script.

I have 2 questions to finish up this issue and have our company's dev workflow back on track:

First, should I have 2 separate RUN commands like that or should I do that && thing?

Second, would it make more sense to include the Dockerfile in our code repository, and so have devs clone the repository, then build the Dockerfile, then run the containers, or does it make more sense to store the new image on docker hub and have them download it by running the containers?

@AwokeKnowing I'd recommend RUN a2enmod rewrite instead. Those files under mods-enabled are normally symlinks, not copies.

As for the complicated && stuff in the RUN statements, I believe that's mostly to keep the number of layers in the base images small. Since your image will likely not be further extended, I see no problem with separate RUN statements.

@md5 Ok, yeah I'll try a2enmod. can't remember why we had copy. The "few layers" makes sense. I guess the next things to do are put the image in docker hub and figure out how "compose" works to make it all seemless.

I would suggest putting the Dockerfile in your git repo and have devs build it themselves. It helps them understand the dependencies that get installed (and they can easily submit changes to add new deps). You do have to teach them to pull the base image every once in a while and rebuild so that security fixes get propagated. My opinion is that there are only a few times you want to pass an image around: base images (like official images provide), and when going from QA to production (ie QA servers build the image and test it, then that image can be deployed to production). It would require adding a COPY to your Dockerfile (but still bind mount with -v for developers for live code changes).

FROM php:5.4-apache
RUN docker-php-ext-install mysql mysqli pdo pdo_mysql
RUN cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
COPY . /var/www/html

You do have to teach them to pull the base image every once in a while

Or use docker build --pull ., which should perform a pull before building

@thaJeztah great tip! I hadn't seen that before.

It was added at some point but not much exposure :)

@yosifkit

It helps them understand the dependencies that get installed

Thank you for this insight. I agree.

Regarding:

adding a COPY to your Dockerfile (but still bind mount with -v for developers for live code changes).

How does it work to copy those files into your web document root and also mount your local folder into the same location?

How does it work to copy those files into your web document root and also mount your local folder into the same location?

A bind-mounted volume is mounted "over" the files in the image; basically, they "mask" / "hide" the underlying files in the image. The container then uses the files that are in the volume, in stead of the container itself, which allows you to make changes to those files without going "inside" the container.

Once the changes are complete, rebuild the image (which will ADD/COPY the most recent files into the image) and ship it.

It allows you to use the same Dockerfile for QA and production, skip the -v and all the files are ready to go.

Thanks everyone for your help and insight!

@yosifkit right on with that setup with PDO!!! Thanks a lot! It saved my life tonight!

RUN docker-php-ext-install mysql mysqli pdo pdo_mysql

Spent some time trying to install the mysql PDO extention from the base image yesterday. Wish I had stumbled on that sooner.

Btw, yet another way to setup rewrite:

RUN ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

Was this page helpful?
0 / 5 - 0 ratings

Related issues

2Kable picture 2Kable  路  3Comments

igodorogea picture igodorogea  路  3Comments

cordoval picture cordoval  路  3Comments

yosifkit picture yosifkit  路  3Comments

dhoeric picture dhoeric  路  4Comments