PHP Platform on Docker
A PHP-Platform Docker tutorial to make PHP runnable in Docker
First of all: I LOVE PHP! I am PHP-Developer for more then 12 years now and still love it like on the first day ;)
But a new “Darling” shown up 4 years ago. DOCKER! I don’t want to loos my PHP but I was also interested using Docker for my App-Servers and my Dev-Machine.
Because of this I decided to use PHP and Docker at the same time and what can I say: IT IS JUST AWESOME!
In this Tutorial i will show you the Setup on my Dev-Machine to develop PHP-Apps with PHP-FPM and NGINX.
#Prerequirements You have Docker installed already and know how Dockerfiles work.
WARNING: This setup is just for your Dev-Machine! Do not use it 1 to 1 on your production servers! I will post a second tutorial how to build this on production!
#Lets go!
Basically, we need two Docker-Images:
- PHP-FPM
- Nginx
I build my own PHP-FPM5 and 7 images based on Alpine. You should create two directories phpfpm5 and phpfpm7 and create the Dockerfiles in it.
Configfiles for php-fpm.
php-fpm.conf
php.ini
Dockerfile PHP-FPM5
Dockerfile PHP-FPM7
Based on these Dockerfiles run a docker build command to build your images. Navigate into your directories and run
After this you should have two Docker images on your machine. You can see the new generated images by typing
Your output should look like this:
Before we run the PHP-Containers we should setup the NGINX-Webserver and link it to the PHP-FPM Container.
Build your NGINX Container
nginx configuration file
Dockerfile nginx
You can build your Docker Image in the same way you built your PHP-FPM Images. Navigate into the directory and run docker build. Don’t forget to label your build ;).
Run your Containers
Make sure you have at least 2 images. NGINX and PHP-FPM.
Now we can run our containers. Very important at this point is the fact we need to link the containers against each other and allow them to talk.
First, start your PHP-FPM container. In my case PHP-FPM7.
The –name is important to link our containers. I case this is completely new to you read the docs.
There was or still is a CLI-command –link. This i depracted and should not be used anymore. Therefore you should create a User-defined bridged network for your containers.
[Docker User-defined networks] (https://docs.docker.com/engine/userguide/networking/#user-defined-networks)
I did this by typing:
Now you should have a User-defined bridged dev network (Isn’t that a perfect word for hangman: “userdefinedbridgeddevnetwork” :D)
List your networks by typing docker network ls.
Now, finally!, let’s start our containers.
Important: Start your PHP-FPM first.
Now, start your NGINX webserver
Let’s have a look if it works:
Type docker ps to list your running container instances. You should have two containers up & running.
Awesome! That looks good. Now lets see what webapp we are running, currently.
To change the PHP-FPM for example just kill and remove the running PHP-FPM container instance and run the other PHP-FPM container.
How to do this?
simply run docker ps to list your running containers (As we did it a lately to check our running containers). Each running container has an unique container-id. To kill and remove the container you can run
What’s next
- A PHP and Docker production Tutorial i a new Post.
- Use Pear and Pecl with PHP-FPM on alpine-linux Base-Images
If you have any questions or commands feel free to leave a comment.