Skip to content

Apache PHP 7 Docker Environment for developers

In this tutorial, I will show how you can setup PHP + Apache docker container for development purposes. Here I am not using MySQL container because MySql is already installed on my machine, but you can create a separate container for MySql and then connect that from the container.

The first thing is to create a new Dockerfile in the project. Here is the screenshot of the project file structure:

dockerfile setting for php apache
docker file setting

I have stored this project in E:\Project\Phpapp folder, we will need this path later, so that we can load our local project in the container.

Now add following in the Dockerfile:

FROM php:7.2-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
COPY . /var/www/html/
EXPOSE 80

Here we are using the official PHP image from Docker. After that we have RUN command, where we are installing mysqli, you will need this only if your project is going to use mysqli.

After that, there is COPY command, which is basically copying all files from our project to /var/www/html location in the container. And in the last, we are exposing the port 80 of the container.

Now, We need to build the image using this Dockerfile. So open terminal/command line, and run below command:

$ docker build -t <image-name-given-by-you> .

On executing this command, it will pull the image, run the command and expose the port, basically, it will perform all steps written in DockerFile. When it’s completed, You can run a container using this image by this command:

$ docker run -p 8080:80 --name <container-name-given-by-you> -v /e/Project/Phpapp:/var/www/html <image-name-given-by-you-in-previous-step>

Here I have mapped the container’s 80 port to the host system’s 8080 port. -v flag is used to map our host system folder to containers /var/www/html folder. This way we can use our project file for development and any changes in the project files will not require to build the image again. This is called sharing the volume. You can check how to mount the host system shared volume in Docker on Windows.

Now you can visit the <docker-machine-ip>:8080 url.

Be First to Comment

Leave a Reply

Your email address will not be published.