It has cost me quite a while to set this up. So I just want to share a simple Tutorial to set up a php development environment with docker on Mac OS.
Goals:
What we want to achieve is, that we have an environment, in which we can develop any php-app and – more important – edit it on the fly with our IDE. For instance, I use PhpStorm, where I have a project folder. And whenever I edit and save something in the folder, I want that theses changes appear in the browser directly.
For me, It was quite a hassle to set this up, so here I want to share my learnings.
Prerequisites:
- First, you should have understood the basic concepts of docker and you should also have gone through the Getting Started. For example, you have to know:
- what is an image and a container,
- what the Dockerfile is good for and
- what are the basic commants of docker (docker ps, docker-machine, etc.)
- Secondly, you should have installed boot2docker
Basic setup of development environment:
So you want to set up a php-development-environment in docker? Do the following steps:
- Create a directory on your Mac: Your workspace, where you you want to edit your web app. I work with PhpStorm, so I will create a new project, which creates a new folder with the path
123/Users/tobi/Documents/Docker/my-project-folder - Create files and folder:
- The Dockerfile
- A Shell script run.sh
- Create a src-folder. Within that src-folder create two test-files: index.php and phpinfo.php.So finally your directory structure looks like:
12345678my-project-folder|- Dockerfile|- run.sh|- src|- index.php|- phpinfo.php
- Put the following content into your Dockerfile:
123456789101112131415161718192021222324252627282930313233343536FROM ubuntu:trustyMAINTAINER Fernando Mayo <fernando@tutum.co># Install base packagesRUN apt-get update && \DEBIAN_FRONTEND=noninteractive apt-get -yq install \curl \apache2 \libapache2-mod-php5 \php5-mysql \php5-mcrypt \php5-gd \php5-curl \php-pear \php-apc && \rm -rf /var/lib/apt/lists/* && \curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composerRUN /usr/sbin/php5enmod mcryptRUN echo "ServerName localhost" >> /etc/apache2/apache2.conf && \sed -i "s/variables_order.*/variables_order = \"EGPCS\"/g" /etc/php5/apache2/php.iniENV ALLOW_OVERRIDE **False**# Add image configuration and scriptsADD run.sh /run.shRUN chmod 755 /*.sh# Configure /app folder with sample appRUN mkdir -p /app && rm -fr /var/www/html && ln -s /app /var/www/htmlADD src/ /appEXPOSE 80WORKDIR /appCMD ["/run.sh"]
I won’t go into detail, but the code is mainly coming form Github (thanks!). In the following the most important concepts:- Name your main image-source (ubuntu): FROM ubuntu:trusty
- Starting apache and php: RUN apt-get update && ...
- And last but not least, you should add the project source into your working directory in the container. This is done via: ADD src/ /app
- In the run.sh file put the following content:
123456789101112131415#!/bin/bashchown www-data:www-data /app -Rif [ "$ALLOW_OVERRIDE" = "**False**" ]; thenunset ALLOW_OVERRIDEelsesed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.confa2enmod rewritefisource /etc/apache2/envvarstail -F /var/log/apache2/* &exec apache2 -D FOREGROUND - Put some example content into your php files:
- index.php
1234<?phpecho ("Hello World! Thank god it's friday!"); - phpinfo.php
1234<?phpphpinfo();
- index.php
That’s it. Your development environment is set. Now you just have to do the build and run process and map your src-folder with the app folder in your container. That way changes are seen directly on the fly.
Build and run your php-app with docker:
- You have created the folder structure as explained above, so now open your terminal and go to the project folder. Im my case that is:
123cd /Users/tobi/Documents/Docker/my-project-folder - Make sure that boot2docker is running.
123boot2docker up
If boot2docker is already up, then you should see something like. If you don’t know about boot2docker, google and install it.
1234...Your environment variables are already set correctly. - Do the build-process to create a new docker-image:
123docker build -t my-user-name/my-image-name .
Explanation:- docker build says, that you want to build a new image
- With -t you’re stating, that you want to define the image name. After that state any user name and an image name that you like.
- The Dot in the end is telling docker, that you want to build the image from the directory that your’re currently in. So that’s why you have to be in your project folder
- Run the image to get a new docker-container.
123docker run -d -p 80:80 -v /Users/tobi/Documents/Docker/my-project-foler/src:/app --name mycontainer my-user-name/my-image-name
This is the most tricky part, because you have to be aware of the different options:- docker run is the opening command, of course
- -d run in the background
- -p 80:80 is the port-mapping: Here the 80 port of your browser (on the mac os) is mapped to the port 80 of the container. 80 is the standard apache port, that’s why.
- -v /Users/tobi/Documents/Docker/my-project-foler/src:/app Similar to the ADD-Statement in the Dockerfile, the v-option tells docker, to mount the project folder (on the Mac) to the app-folder (in the container). This way, when your container is running, changes in your project folder are directly passed to the container.
- --name mycontainer gives a name to your created container
- The last thing is to name the image:
my-user-name/my-image-nameAfter that command, you can test, if your container is running. With using
docker ps you should see something like:
1234CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES065832c6b466 my-user-name/my-image-name "/run.sh" 50 sec 50 sec 0.0.0.0:80->80/tcp my-container-name
- You’re almost ready to test your php web-app, but first you have to find out the ip-adress of boot2docker:
123boot2docker ip
If boot2docker is up, you should see the ip, for example:
123192.168.59.103 - Now you’re finally ready to test your php-files in the browser. Go to your browser and type in the following url:
123http://192.168.59.103/
If you see the content of your index-file (“Hello World! Thank god it’s friday!” as stated in the first part) you can be sure, that the build process was successful.
You can also test your php-version with:
123http://192.168.59.103/phpinfo.php - Next, you have to test, if the mapping of your project folder to your container-folder is working correctly (in docker that is called data-volumes, and is explained in detailed here)
- Edit the index.php file in your project folder. For example put in the following content:
1234<?phpecho ("Hello World! Thank god it's saturday!"); - Go to your browser, type in the url (or update the page):
123http://192.168.59.103/
If you see the changes in the browser, you’re all set.
- Edit the index.php file in your project folder. For example put in the following content:
Conclusion
That’s it. By following that guide you should have been able to set up your dev-environment. Hopefully you could follow the guide and it helped you.
[…] http://www.codesolutions.de/docker-mac-os-setting-up-a-php-development-container/ […]