Try out various PHP execution environments! 2016 will be decided by Docker coordination!

Hello.
I'm Mandai, in charge of Wild on the development team.

If you want to create a stylish development environment with Docker, why not try creating your own Docker image with Docker Build?
Having a Docker image that is easy to use will give you more flexibility in how you arrange it!

This is one article in the Docker introductory series.
If you would like to read the previous Docker introductory series, please follow the link below.

Please note that this is not a copy-and-paste series, as it cannot be done by copy and paste

About the PHP execution environment

The Docker images on Docker Hub are quite good and there is no problem using them as they are

  • For example, you might want to be more particular about the directory where you place your source files
  • I want to encode an audio file using ffmpeg from PHP
  • I want to use PHP with lots of extension modules

In order to meet such requirements, I think the only option is to rebuild the Docker image ourselves

In many cases, this can be resolved by making changes to an existing Docker image and rebuilding it, but it is also possible to include the module when compiling PHP. Docker builds are done using a Dockerfile, which is the procedure manual, and it is immediately clear what is being done

In other words, the Docker image you are currently using also has a Dockerfile somewhere, and if you make a few modifications to that Dockerfile and build it, you can get the optimal Docker image you want

Furthermore, Dockerfiles are actually uploaded to GitHub, and many of them are open (this tendency is stronger for official Docker images), so it's a great place to look at various Dockerfiles and steal their methods

This time, I would like to create a Docker image that contains multibyte-compatible PHP, using the official PHP Docker image as a reference

First, find the base docker image

By looking at the Docker images listed on Docker Hub, it's easy to find one that's close to what you want to create. This time, I the official PHP Docker image as a base and making some modifications. Looking at the page, there's a link to the Dockerfile managed on GitHub.

By following the link (7.0/apache/Dockerfile), I found a Dockerfile for creating a Docker image for PHP 7.0 series + Apache.
I'll use this as a starting point. I'll git clone this repository on a VM and make some modifications.

cd ~ vagrant@vagrant:~$ git clone https://github.com/docker-library/php.git vagrant@vagrant:~$ cd /home/vagrant/php/7.0/apache

Add "--enable-mbstring" to the ./configure options

Also, create a test PHP file and copy it to the Docker image

vi Dockerfile # Dockerfile, around line 59 && ./configure --enable-mbstring # ← Add --with-config-file-path="$PHP_INI_DIR" # Add the following COPY command to the appropriate location in the Dockerfile # I added it before WORKDIR /var/www/html. COPY mbstring.php /var/www/html/

Compiling PHP with docker build

Once you have made the corrections, create mbstring.php

echo "<?php echo mb_convert_kana('1234567890', 'N');" > mbstring.php 

Once created, try running docker build

vagrant@vagrant:~/php/7.0/apache$ docker build -t myphp7 .

the last build, this build will take quite a while because apt-get updates and other things will be running frequently.
Once the build is complete, try creating a Docker container from the Docker image.

vagrant@vagrant:~/php/7.0/apache$ docker run -it myphp7 /bin/bash root@66c8bbc720a9:/var/www/html# ls mbstring.php root@66c8bbc720a9:/var/www/html# service apache2 start [....] Starting web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message . ok root@66c8bbc720a9:/var/www/html# curl http://localhost/mbstring.php 1234567890root@66c8bbc720a9:/var/www/html#

It executes a command to convert half-width numbers to full-width numbers and then finishes, and the conversion was successful without any problems

You don't need to start from scratch, but if you find a relatively plain Dockerfile and use it as a starting point, you should be able to quickly build it. The official php Docker image doesn't even include vim, so if you want to modify the configuration inside, installing vim with apt-get should be no problem

When creating a virtual environment using vagrant, file size is a concern, but when creating a Docker image, it is reduced to 1/4 the size (of course, this is only the PHP execution environment), so it is easy to share. In the first place, if you only share the Dockerfile and the files to be included in the Docker image, it is also easy to manage it in a git repository

Being able to compile the entire process as a command has many advantages when building an environment in the early stages of development, so it would be great to be able to provide the optimal execution environment by adding the optimal compilation options without relying solely on apt-get or yum

That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
592
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.