docker官方鏡像為安裝php擴展封裝了函數,為開發者提供了很大的便利,以下以Dockerfile的形式演示安裝gd擴展的方法,安裝gd擴展需要安裝幾個依賴包,安裝依賴包使用系統命令,安裝命令根據基礎鏡像的不同有所不同,以下演示兩種使用較多的alpine和debian系統的Dockerfile,Dockerfile中同時包含更換國內開源鏡像源的內容。
1、基礎鏡像 php:7.2-fpm-alpine
Dockerfile如下:
FROM php:7.2-fpm-alpine RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && apk update \ && apk add libpng-dev freetype-dev libjpeg-turbo-dev \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd EXPOSE 9000 CMD ["php-fpm"]
2、基礎鏡像 php:7.2-fpm-stretch (debian 9)
Dockerfile如下:
FROM php:7.2-fpm-stretch RUN echo "deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib" > /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/debian-security stretch/updates main" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/debian-security stretch/updates main" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && rm -f /etc/apt/sources.list.d/* \ && apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include \ && docker-php-ext-install -j$(nproc) gd EXPOSE 9000 CMD ["php-fpm"]