一、CentOS 7.x 安装Docker-ce社区版本
https://www.osyunwei.com/archives/11592.html
二、准备安装软件包
cd /usr/local/src #建议先把需要安装的软件包下载到本地目录
1、下载php
http://mirrors.sohu.com/php/php-7.2.34.tar.gz
2、下载openssl
https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
3、下载libmcrypt(php扩展)
https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
4、下载yasm(php扩展)
http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
5、t1lib(php扩展)
http://download.freenas.org/distfiles/t1lib-5.1.2.tar.gz
6、适用于php 5.5.x及其以上版本
https://github.com/libgd/libgd/releases/download/gd-2.3.1/libgd-2.3.1.tar.gz
7、libvpx(gd库需要)
https://github.com/webmproject/libvpx/archive/v1.10.0/libvpx-1.10.0.tar.gz
8、tiff(gd库需要)
http://download.osgeo.org/libtiff/tiff-4.0.7.tar.gz
9、libpng(gd库需要)
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.37.tar.gz
10、freetype(gd库需要)
https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz
11、jpegsrc(gd库需要)
http://distfiles.macports.org/jpeg/jpegsrc.v9d.tar.gz -O jpeg-9d.tar.gz
12、libzip(编译php需要)
https://libzip.org/download/libzip-1.7.3.tar.gz
13、oniguruma(编译安装php7.4.x及其以上版本需要)
https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz -O oniguruma-6.9.7.1.tar.gz
14、curl库(编译php需要)
https://curl.se/download/curl-7.77.0.tar.gz
15、下载cmake(编译工具)
https://cmake.org/files/v3.21/cmake-3.21.1.tar.gz
三、构建php-fpm容器镜像
1、从docker hub拉取官方基础镜像
#我们用ubuntu系统进行构建
https://hub.docker.com/_/ubuntu?tab=tags&page=1&ordering=last_updated
选择ubuntu:20.04版本
docker pull ubuntu:20.04 #拉取系统镜像
docker image ls #查看docker镜像
docker run -itd --name ubuntu ubuntu:20.04 #运行容器
docker ps #查看容器
[root@master01 src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe79b0184c95 ubuntu:20.04 "bash" 17 seconds ago Up 15 seconds ubuntu
docker exec -it fe79b0184c95 /bin/bash #进入容器
root@fe79b0184c95:/# cat /etc/issue #查看系统版本
Ubuntu 20.04.2 LTS \n \l
exit #退出容器
2、编写Dockerfile构建文件
cd /usr/local/src #进入目录
vi /usr/local/src/dockerfile #dockerfile文件必须和上一步下载的软件包在同一目录
#基于这个镜像进行操作
FROM ubuntu:20.04
#作者和邮箱
MAINTAINER osyunwei osyunwei@osyunwei.com
#指定容器里面的路径,为后面的RUN、CMD或者ENTERPOINT操作指定目录
WORKDIR /usr/local/src
#更新ubuntu 20.04系统的163源
RUN mv /etc/apt/sources.list /etc/apt/sources.list-bak
RUN echo "deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse" > /etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list
#安装依赖包
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& set -xe \
&& buildDeps=" \
debian-keyring \
debian-archive-keyring \
build-essential \
gcc \
g++ make \
libtool \
automake \
autoconf \
libmcrypt-dev \
libxml2-dev \
re2c \
wget \
cron \
bzip2 \
libzip-dev \
libc6-dev \
bison \
file \
flex \
m4 \
gawk \
less \
cpp \
binutils \
diffutils \
unzip \
tar \
libbz2-dev \
libncurses5 \
libncurses5-dev \
libevent-dev \
openssl \
libssl-dev \
zlibc \
libsasl2-dev \
libltdl3-dev \
libltdl-dev \
zlib1g \
zlib1g-dev \
libbz2-1.0 \
libglib2.0-0 \
libglib2.0-dev \
libjpeg-dev \
libpng-dev \
libkrb5-dev \
curl \
libcurl3-gnutls \
libpcre3-dev \
libpq-dev \
libpq5 \
gettext \
libcap-dev \
ca-certificates \
libc-client2007e-dev \
psmisc \
patch \
git \
libc-ares-dev \
libicu-dev \
e2fsprogs \
libxslt1.1 \
libxslt1-dev \
libc-client-dev \
xz-utils \
libexpat1-dev \
libaio-dev \
libtirpc-dev \
python-dev \
libsqlite3-dev \
libonig-dev \
lsof \
libxpm-dev \
libfreetype6-dev \
checkinstall \
zip \
libfcgi-dev \
libfcgi0ldbl \
libmhash-dev \
freetds-dev \
libmariadbclient-dev-compat \
unixodbc-dev \
pkg-config \
libcurl4-openssl-dev \
inetutils-ping \
" \
&& apt-get install -y ${buildDeps}
#设置时区
RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
#设置变量
ENV openssl=openssl-1.0.2u \
cmake=cmake-3.21.1 \
curl=curl-7.76.1 \
freetype=freetype-2.10.4 \
jpeg=jpeg-9d \
libgd=libgd-2.3.1 \
libmcrypt=libmcrypt-2.5.8 \
libpng=libpng-1.6.37 \
libvpx=libvpx-1.10.0 \
libzip=libzip-1.7.3 \
oniguruma=oniguruma-6.9.7.1 \
t1lib=t1lib-5.1.2 \
tiff=tiff-4.0.7 \
yasm=yasm-1.3.0 \
php=php-7.2.34
#将本地的一个文件或目录拷贝到容器的某个目录里
COPY $openssl.tar.gz \
$cmake.tar.gz \
$curl.tar.gz \
$freetype.tar.gz \
$jpeg.tar.gz \
$libgd.tar.gz \
$libmcrypt.tar.gz \
$libpng.tar.gz \
$libvpx.tar.gz \
$libzip.tar.gz \
$oniguruma.tar.gz \
$t1lib.tar.gz \
$tiff.tar.gz \
$yasm.tar.gz \
$php.tar.gz ./
#解压软件包
RUN ls *.tar.gz | xargs -n1 tar xzvf
#安装yasm
WORKDIR /usr/local/src/$yasm
RUN ./configure && make && make install
#安装libmcrypt
WORKDIR /usr/local/src/$libmcrypt
RUN ./configure && make && make install
#安装libvpx
WORKDIR /usr/local/src/$libvpx
RUN ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 --as=yasm && make && make install
#安装tiff
WORKDIR /usr/local/src/$tiff
RUN ./configure --prefix=/usr/local/tiff --enable-shared && make && make install
#安装libpng
WORKDIR /usr/local/src/$libpng
RUN ./configure --prefix=/usr/local/libpng --enable-shared && make && make install
#安装freetype
WORKDIR /usr/local/src/$freetype
RUN ./configure --prefix=/usr/local/freetype --enable-shared --enable-freetype-config && make && make install \
&& mkdir -p /usr/lib/pkgconfig/ \
&& cp /usr/local/freetype/lib/pkgconfig/freetype2.pc /usr/lib/pkgconfig/ \
&& ln -sf /usr/local/freetype/include/freetype2/* /usr/include/ \
&& ln -sf /usr/local/freetype/include/freetype2/* /usr/local/include \
&& echo '/usr/local/freetype/lib' >> /etc/ld.so.conf.d/freetype.conf \
&& ldconfig
#安装jpeg
WORKDIR /usr/local/src/$jpeg
RUN ./configure --prefix=/usr/local/jpeg --enable-shared && make && make install
#安装GD库
WORKDIR /usr/local/src/$libgd
RUN ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/lib64 --with-tiff=/usr/local/tiff && make && make install
#安装t1lib
WORKDIR /usr/local/src/$t1lib
RUN ./configure --prefix=/usr/local/t1lib --enable-shared && make without_doc && make install
#安装cmake
WORKDIR /usr/local/src/$cmake
RUN ./configure && make && make install
#安装libzip
WORKDIR /usr/local/src/$libzip
RUN mkdir build && cd build && cmake .. && make && make install
#安装oniguruma
WORKDIR /usr/local/src/$oniguruma
RUN ./autogen.sh &&./configure --prefix=/usr && make && make install
#安装openssl
WORKDIR /usr/local/src/$openssl
RUN ./config -fPIC shared zlib --openssldir=/usr/local/ssl/ enable-ec_nistp_64_gcc_128 && make depend && make && make install \
&& ln -s /usr/local/ssl/lib /usr/local/ssl/lib/x86_64-linux-gnu
#安装curl模块
WORKDIR /usr/local/src/$curl
RUN env PKG_CONFIG_PATH=/usr/local/ssl/lib/pkgconfig LDFLAGS=-Wl,-rpath=/usr/local/ssl/lib \
&& ./configure --without-nss --with-ssl=/usr/local/ssl --with-zlib --prefix=/usr/local/curl && make && make install
#编译安装php
#创建php-fpm运行用户和组
RUN groupadd www && useradd -g www www -s /bin/false
#安装php
WORKDIR /usr/local/src/$php
RUN export LD_LIBRARY_PATH=/usr/local/libgd/lib \
&& export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/" \
&& ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-gd=/usr/local/libgd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --with-openssl=/usr/local/ssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl=/usr/local/curl --enable-ctype --enable-mysqlnd --with-xpm-dir=/usr && make && make install
#配置php
RUN rm -rf /etc/php.ini \
&& cp php.ini-production /usr/local/php/etc/php.ini \
&& cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
&& cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
&& sed -i "s/;pid = run\/php-fpm.pid/pid = run\/php-fpm.pid/g" '/usr/local/php/etc/php-fpm.conf' \
&& sed -i "s/;daemonize = yes/daemonize = no/g" '/usr/local/php/etc/php-fpm.conf' \
&& sed -i "s/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/g" '/usr/local/php/etc/php-fpm.d/www.conf' \
&& sed -i "s/user = nobody/user = www/g" '/usr/local/php/etc/php-fpm.d/www.conf' \
&& sed -i "s/group = nobody/group = www/g" '/usr/local/php/etc/php-fpm.d/www.conf'
#切换到根目录
WORKDIR /root
#清理安装包
RUN rm -rf /usr/local/src/* && apt-get clean
#设置php-fpm运行端口
EXPOSE 9000
#设置php-fpm以--nodaemonize前台方式运行
CMD ["/usr/local/php/sbin/php-fpm"]
:wq! #保存退出
3、构建容器
docker build -t osyunwei/php-fpm:7.2.34 /usr/local/src
或者
docker build -t osyunwei/php-fpm:7.2.34 .
4、运行容器
docker image ls #查看docker镜像
docker run -itd --name php7 osyunwei/php-fpm:7.2.34 #运行容器
docker ps #查看容器
[root@master01 src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5aa7799b9c3 osyunwei/php-fpm:7.2.34 "/usr/local/php/sbin…" 14 seconds ago Up 13 seconds 9000/tcp php7
docker exec -it c5aa7799b9c3 /bin/bash #进入容器
exit #退出容器
5、上传php-fpm容器镜像到阿里云容器仓库
docker image ls #查看镜像id
[root@master01 src]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
osyunwei/php-fpm 7.2.34 7f81f37b4764 10 minutes ago 2.67GB
5.1重命名镜像标签
docker tag 7f81f37b4764 registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.2.34
#registry.cn-hangzhou.aliyuncs.com是阿里云容器仓库地址
#osyunwei是自己创建的命名空间
#php-fpm是自己创建的仓库名称
#:7.2.34是自定义php-fpm镜像的版本号
5.2登录阿里云仓库
docker login --username=你的阿里云登录账号 registry.cn-hangzhou.aliyuncs.com
[root@master01 src]# docker login --username=你的阿里云登录账号 registry.cn-hangzhou.aliyuncs.com
Password:刚才设置的固定密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@master01 src]#
5.3上传镜像到阿里云仓库
docker push registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.2.34
5.4拉取阿里云容器镜像
docker pull registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.2.34
至此,Docker下使用Dockerfile基于ubuntu基础镜像构建php-fpm容器镜像教程完成。