Build a PHP Docker File with .htaccess and Mod-Rewrite

Hi Guys,

today a write about Docker to share a PHP Project with Mod-Rewrite and no Public Application Code and i try to write it in english. I must learn it. 😀 This How-to is written for Puschel from Cranktube.tv. Maybe he will understand the Docker Commands now.

The PHP Project Folder are

/myProject

/appcode

/Model
/View
/Control

/html

/css
/js
/img
index.php

.htaccess
.gitignore
Dockerfile
httpd.conf

The First Step is to open a File names Dockerfile and write the Build Requirements in it.

Here the full Dockerfile Content.

FROM centos:latest
MAINTAINER Manuel ‚Onko‘ Müller <mueller.m.h@gmail.com>

RUN yum update -y
RUN yum install git curl php -y
RUN yum install httpd -y
RUN yum install php-pdo -y
RUN yum install php-pdo_mysql -y

COPY . /var/www
COPY httpd.conf /etc/httpd/conf/httpd.conf

EXPOSE 80

CMD httpd && tail -f /var/log/lastlog

We need a OS so i choose CentOS in latest version. This is the First Line

FROM centos:latest

The MAINTAINER is only for Information, who has written this holy shit.

The next RUN Commands update the CentOS and install Git, Curl, PHP, httpd, php-pdo, php-pdo-mysql

PDO is the Database Conector and php_pdo_mysql the Connector for MySQL.

The Copy Command take the Content of the actually Folder in the Docker Container. The Target Folder is /var/www. This is the httpd default Webroot.

Then i  copy my own httpd.conf file in the Container to Allow Override and some other Stuff. This File is in the same Folder too.

EXPOSE 80 Open the http Port for the Container so it is possible to connect the container.

CMD httpd && tail -f /var/log/lastlog this command start the httpd service and open a logfile without the tail logfile the httpd will startet and the docker container shutdown if all done.

Now we have a full running PHP Project in this Container.

In my Case i will run it local for development. So i run boot2docker on my Windows Maschine and shared my Development Folder with the Virtual Maschine. So i have access to it with mount Point /c/Users

So i Switch the workdir to my development Project Folder

cd /c/Users/myPHPCode

In this folder are my PHP Code, public code like CSS Images and public PHP Files in subfolder html, my httpd Config and the Dockerfile

So i can build a Container now.

docker build -t myProject .

The dot as the end is the acctually folder i can use follow command too.

docker build -t myProject /c/Users/myPHPCode

Next Command we need is to start the Container.

docker run -p 80:80  -d -i -t myProject

With -p i mapped the container port 80 to the Virtual Maschine Port 80.

Now i have running a PHP Project with the latest code of my Project. But i want to change code and try it out. so i can mount my real local code to the container.

With -v i can mount /var/www with the virtual maschine folder /c/Users/myPHPCode

docker run -p 80:80 -v /c/Users/myPHPCode/var/www -d -i -t myProject

I hope my english is clear enough to understand what i try to explain. 🙂

mysql_query ( ) und mysql_* co hat ausgedient

Da ich mich privat wiedermal PHP gewidmet habe ist mir jetzt erst bekannt geworden das die wohl meist verbreitete Art der MySQL Datenbank Nutzung im PHP Bereich längst ausgedient hat und bereits auf der Abschuss Liste steht. Oder besser gesagt in den neuesten Versionen bereits nicht mehr vorhanden sind.

Als Alternative kommt nun nur der Connector PDO zum Tragen. Auch dieser ist nicht neu und seit Jahren ein erpropter Connector.

Natürlich war das die richtige Entscheidung PHP weiter Richtung OOP zu bringen und die alt gedienten Funktionen zu entfernen.

Wer seine Datenbank Handler mit prepare bind_param und execute ausgestattet und verwendet hat, ist die umstellung kein spektakulärer Eingriff. In den besten Fällen kann zum Beispiel der Inhalt der eigenen Datenbank Klasse gelöscht und stattdessen ein extend PDO genutzt werden.

Die Info ist für alle die wie ich überhaupt nicht auf dem laufenden sind. 🙂

Viel Spass beim programmieren.