docker的官方php镜像php:5.4-apache配置xdebug-gd-mysql-mysqli

Docker-php-apache-xdebug-gd-mysql-mysqli历坑记
可以直接看最后的小结部分,之前的是做个记录…

缘起

PHP & MySQL范例精解,创建、修改、重用一书中,代码用到的mysql_connect在5.5版本已经废弃,故改用php:5.4-apache

添加xdebug

找xdebug的对应版本号

找对应版本号失败经验

  1. ❌网上介绍的到xdebug站点,用phpinfo()的信息贴进去,发现,对5.6,5.4,5.3的都试过,都显示不支持.

  2. pecl search xdebug

    # pecl search xdebug
    Package Stable/(Latest) Local
    xdebug  2.9.4 (stable)        Provides functions for function traces and profiling
    

    尝试2.9.4结果:

    pecl/xdebug requires PHP (version >= 7.1.0), installed version is 5.4.45
    

✅最终,在https://github.com/xdebug/xdebug/tree/xdebug_2_4发现下面这句

Restrict Xdebug 2.4 to PHP >= 5.4 and PHP < 7.1

打出镜像

Dockerfile

FROM php:5.4-apache
COPY Dockerfile /
RUN pecl channel-update pecl.php.net && \
        pecl install xdebug-2.4.1 && \
        docker-php-ext-enable xdebug && \
        mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug
# mkdir Dockerfile.php-5.4-apache-xdebug && cd Dockerfile.php-5.4-apache-xdebug
# docker build -t php-5.4-apache-xdebug .

终于可以打出镜像

初步添加mlocate和gd

查看版本及替换源

查看容器里的debian版本是jessie

# cat /etc/apt/sources.list
deb http://httpredir.debian.org/debian jessie main
deb http://httpredir.debian.org/debian jessie-updates main
deb http://security.debian.org jessie/updates main

替换源为阿里的

mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\

尝试Dockerfile失败:

这个可以跳过不看,作个记录…

FROM php-5.4-apache-xdebug
COPY Dockerfile /
RUN mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug-mlocate-gd && \
mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
apt-get update && apt-get install -y mlocate \
                                     libfreetype6-dev \
                                     libjpeg62-turbo-dev \
                                     libpng-dev && \
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install -j$(nproc) gd


# mkdir Dockerfile.php-5.4-apache-xdebug-mlocate-gd && cd Dockerfile.php-5.4-apache-xdebug-mlocate-gd
# docker build -t php-5.4-apache-xdebug-mlocate-gd .

错误返回:

W: There is no public key available for the following key IDs:
AA8E81B4331F7F50
解决的办法为:
apt-get install debian-keyring debian-archive-keyring
然后
apt-key update

查自 https://www.cnblogs.com/lege/p/4508736.html

又出现报错

configure: WARNING: unrecognized options: --with-freetype, --with-jpeg
error: /usr/src/php/ext/-j6 does not exist

usage: /usr/local/bin/docker-php-ext-install ext-name [ext-name ...]
   ie: /usr/local/bin/docker-php-ext-install gd mysqli
       /usr/local/bin/docker-php-ext-install pdo pdo_mysql

-j$(nproc)去除,

成功打出镜像

Dockerfile

FROM php-5.4-apache-xdebug
COPY Dockerfile /
RUN mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug-mlocate-gd && \
mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
apt-get update && \
apt-get install -y --assume-yes  debian-keyring debian-archive-keyring apt-utils && apt-key update 

RUN apt-get install -y  mlocate \
                                     libfreetype6-dev \
                                     libjpeg62-turbo-dev \
                                     libpng-dev && \
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install gd
# mkdir Dockerfile.php-5.4-apache-xdebug-mlocate-gd && cd Dockerfile.php-5.4-apache-xdebug-mlocate-gd
# docker build -t php-5.4-apache-xdebug-mlocate-gd .

终于成功打出来了

运行

docker run -d \
           -p 80:80 \
           --name virhuiai-php-7302195625-ch1tmp \
           --net=virhuiai_nw \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/public_files/:/var/www/html/ \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/lib/:/var/www/lib \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/templates/:/var/www/templates \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/docker/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
php-5.4-apache-xdebug-mlocate-gd
docker cp virhuiai-php-7302195625-ch1tmp:/etc/apache2/apache2.conf /Volumes/TmpDownload/

使用临时方式,找下xdebug的位置

docker run -d \
           -p 80:80 \
           --rm \
           --name virhuiai-php-7302195625-ch01 \
           --net=virhuiai_nw \
php-5.4-apache-xdebug-mlocate-gd
# locate xdebug
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
# updatedb

# # locate xdebug
/Dockerfile.php-5.4-apache-xdebug
/Dockerfile.php-5.4-apache-xdebug-mlocate-gd
/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
/usr/local/lib/php/.registry/.channel.pecl.php.net/xdebug.reg
/usr/local/lib/php/doc/xdebug
/usr/local/lib/php/doc/xdebug/CREDITS
/usr/local/lib/php/doc/xdebug/LICENSE
/usr/local/lib/php/doc/xdebug/README.rst
/usr/local/lib/php/doc/xdebug/contrib
/usr/local/lib/php/doc/xdebug/contrib/tracefile-analyser.php
/usr/local/lib/php/doc/xdebug/contrib/xt.vim
/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so

找到位置

/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

复制到容器外

docker cp virhuiai-php-7302195625-ch01:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini /Users/jjkkll/Documents/2020-book-read/7302195625/

修改

/Users/jjkkll/Documents/2020-book-read/7302195625/docker-php-ext-xdebug-4php54apache.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host = host.docker.internal 
xdebug.remote_port = 9001
xdebug.remote_log = /var/log/php/xdebug.log

平时运行

可以将各个文件夹映射加上

docker run -d \
           -p 80:80 \
           --rm \
           --name virhuiai-php-7302195625-ch01 \
           --net=virhuiai_nw \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/public_files/:/var/www/html/ \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/lib/:/var/www/lib \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/templates/:/var/www/templates \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/docker/docker-php-ext-xdebug-4php54apache.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
-v /Users/jjkkll/Documents/2020-book-read/7302195625/docker/apache2-4php54apache.conf:/etc/apache2/apache2.conf \
php-5.4-apache-xdebug-mlocate-gd

添加mysqli

Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli
php-5.4-apache-xdebug-mlocate-gd-mysqli

Dockerfile

FROM php-5.4-apache-xdebug-mlocate-gd
COPY Dockerfile /
RUN mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli && \
# apt-get install php5-mysql 
docker-php-ext-install mysql mysqli
# mkdir Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli && cd Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli
# docker build -t php-5.4-apache-xdebug-mlocate-gd-mysqli .

打出镜像

docker run -d \
           -p 80:80 \
           --rm \
           --name virhuiai-php-7302195625-ch01 \
           --net=virhuiai_nw \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/public_files/:/var/www/html/ \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/lib/:/var/www/lib \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/9780470192429_All_Code/ch_01/templates/:/var/www/templates \
           -v /Users/jjkkll/Documents/2020-book-read/7302195625/docker/docker-php-ext-xdebug-4php54apache.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
-v /Users/jjkkll/Documents/2020-book-read/7302195625/docker/apache2-4php54apache.conf:/etc/apache2/apache2.conf \
php-5.4-apache-xdebug-mlocate-gd-mysqli

gd坑

运行网上一个gd例子报错:

Call to undefined function imagettftext()

查看gd_info():

var_dump(gd_info());
array (size=11)
  'GD Version' => string 'bundled (2.1.0 compatible)' (length=26)
  'FreeType Support' => boolean false
  'T1Lib Support' => boolean false
  'GIF Read Support' => boolean true
  'GIF Create Support' => boolean true
  'JPEG Support' => boolean false
  'PNG Support' => boolean true
  'WBMP Support' => boolean true
  'XPM Support' => boolean false
  'XBM Support' => boolean true
  'JIS-mapped Japanese Font Support' => boolean false

发现

  'FreeType Support' => boolean false

添加Gd时,Dockerfile的内容有部分要修改:

docker-php-ext-configure gd --with-freetype-dir=/usr/include/freetype2 --with-jpeg-dir=/usr/include/ --with-freetype --with-jpeg --enable-gd-native-ttf && docker-php-ext-install gd

总结

加了xdebug镜像的Dockerfile

FROM php:5.4-apache
COPY Dockerfile /
RUN pecl channel-update pecl.php.net && \
        pecl install xdebug-2.4.1 && \
        docker-php-ext-enable xdebug && \
        mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug
# mkdir Dockerfile.php-5.4-apache-xdebug && cd Dockerfile.php-5.4-apache-xdebug
# docker build -t php-5.4-apache-xdebug .

加了gd的镜像的Dockerfile

FROM php-5.4-apache-xdebug
COPY Dockerfile /
RUN mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug-mlocate-gd && \
mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
echo "deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list &&\
apt-get update && \
apt-get install -y --assume-yes  debian-keyring debian-archive-keyring apt-utils && apt-key update 

RUN apt-get install -y  mlocate \
                                     libfreetype6-dev \
                                     libjpeg62-turbo-dev \
                                     libpng-dev && \
docker-php-ext-configure gd --with-freetype --with-freetype-dir=/usr/include/freetype2 --with-jpeg --with-jpeg-dir=/usr/include/ --enable-gd-native-ttf && docker-php-ext-install gd
#docker-php-ext-install -j$(nproc) gd
# mkdir Dockerfile.php-5.4-apache-xdebug-mlocate-gd && cd Dockerfile.php-5.4-apache-xdebug-mlocate-gd
# docker build -t php-5.4-apache-xdebug-mlocate-gd .

加了mysql,mysqli的镜像的Dockerfile

FROM php-5.4-apache-xdebug-mlocate-gd
COPY Dockerfile /
RUN mv /Dockerfile /Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli && \
# apt-get install php5-mysql 
docker-php-ext-install mysql mysqli
# mkdir Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli && cd Dockerfile.php-5.4-apache-xdebug-mlocate-gd-mysqli
# docker build -t php-5.4-apache-xdebug-mlocate-gd-mysqli .

运行时:

docker run -d \
           -p 80:80 \
           --name virhuiai-php-fzyz54 \
           --net=virhuiai_nw \
           -v /..省略长目录./fzyz/webapps/:/var/www/html/ \
           -v /..省略长目录./fzyz/sitedata/:/var/www/sitedata/ \
           -v /..省略长目录./fzyz/miniphp/:/var/www/miniphp/ \
           -v /..省略长目录./docker-php-ext-xdebug-4php54apache.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
           -v /..省略长目录./apache2-4php54apache.conf:/etc/apache2/apache2.conf \
php-5.4-apache-xdebug-mlocate-gd-mysqli

你可能感兴趣的:(开发,docker,php)