Docker & Mac OS – Setting up a php development container

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:

  1. 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
  2. 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:
  3. Put the following content into your Dockerfile:

    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
  4. In the run.sh file put the following content:
  5. Put some example content into your php files:
    • index.php
    • phpinfo.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:

  1. 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:
  2. Make sure that boot2docker is running.

    If boot2docker is already up, then you should see something like. If you don’t know about boot2docker, google and install it.
  3. Do the build-process to create a new docker-image:

    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
  4. Run the image to get a new docker-container.

    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:
  5. You’re almost ready to test your php web-app, but first you have to find out the ip-adress of boot2docker:

    If boot2docker is up, you should see the ip, for example:
  6. Now you’re finally ready to test your php-files in the browser. Go to your browser and type in the following url:

    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:
  7. 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:
    • Go to your browser, type in the url (or update the page):

      If you see the changes in the browser, you’re all set.

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.

Teilen