技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页Docker>正文
Docker下使用Dockerfile基于centos基础镜像构建php-fpm容器镜像
2021年08月21日 Docker 暂无评论 ⁄ 被围观 733次+

一、CentOS 7.x 安装Docker-ce社区版本

https://www.osyunwei.com/archives/11592.html

二、准备安装软件包

cd /usr/local/src #建议先把需要安装的软件包下载到本地目录

1、下载php

https://www.php.net/distributions/php-7.1.33.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拉取官方基础镜像

#我们用centos系统进行构建

https://hub.docker.com/_/centos?tab=tags&page=1&ordering=last_updated

选择centos:7.9.2009版本

docker pull centos:7.9.2009 #拉取系统镜像

docker image ls #查看docker镜像

docker run -itd --name centos7.9 centos:7.9.2009 #运行容器

docker ps #查看容器

[root@master01 opt]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

adc6473335ee centos:7.9.2009 "/bin/bash" 48 seconds ago

docker exec -it adc6473335ee /bin/bash #进入容器

exit #退出容器

2、编写Dockerfile构建文件

cd /usr/local/src #进入目录

vi /usr/local/src/dockerfile #dockerfile文件必须和上一步下载的软件包在同一目录

#基于这个镜像进行操作

FROM centos:7.9.2009

#作者和邮箱

MAINTAINER osyunwei osyunwei@osyunwei.com

#指定容器里面的路径,为后面的RUN、CMD或者ENTERPOINT操作指定目录

WORKDIR /usr/local/src

#设置时区

RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone && echo 'Zone=Asia/Shanghai' >/etc/sysconfig/clock

#安装依赖包

RUN set -xe \

&& buildDeps=" \

apr* \

autoconf \

automake \

bison \

bzip2 \

bzip2* \

cloog-ppl \

cpp \

curl \

curl-devel \

fontconfig \

fontconfig-devel \

freetype \

freetype* \

freetype-devel \

gcc \

gcc-c++ \

gtk+-devel \

gd \

gettext \

gettext-devel \

glibc \

kernel \

kernel-headers \

keyutils \

keyutils-libs-devel \

krb5-devel \

libcom_err-devel \

libpng \

libpng-devel \

libjpeg* \

libsepol-devel \

libselinux-devel \

libstdc++-devel \

libtool* \

libgomp \

libxml2 \

libxml2-devel \

libXpm* \

libxml* \

libXaw-devel \

libXmu-devel \

libtiff libtiff* \

make \

mpfr \

ncurses* \

ntp \

openssl \

openssl-devel \

patch \

pcre-devel \

perl \

php-common \

php-gd \

policycoreutils \

telnet \

t1lib \

t1lib* \

nasm \

nasm* \

wget \

zlib-devel \

" \

&& yum install -y ${buildDeps}

#设置变量

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.1.33

#将本地的一个文件或目录拷贝到容器的某个目录里

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 \

&& ./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 --enable-gd-native-ttf --with-openssl=/usr/local/ssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --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/* && yum clean all

#设置php-fpm运行端口

EXPOSE 9000

#设置php-fpm以--nodaemonize前台方式运行

CMD ["/usr/local/php/sbin/php-fpm"]

:wq! #保存退出

3、构建容器

docker build -t osyunwei/php-fpm:7.1.33 /usr/local/src

或者

docker build -t osyunwei/php-fpm:7.1.33 .

4、运行容器

docker image ls #查看docker镜像

docker run -itd --name php7 osyunwei/php-fpm:7.1.33 #运行容器

docker ps #查看容器

[root@master01 src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

a7ded2ac8daf osyunwei/php-fpm:7.1.33 "/usr/local/php/sbin…" 9 seconds ago Up 7 seconds 9000/tcp php7

docker exec -it a7ded2ac8daf /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.1.33 282fac218771 3 hours ago 2.77GB

5.1重命名镜像标签

docker tag 282fac218771 registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.1.33

#registry.cn-hangzhou.aliyuncs.com是阿里云容器仓库地址

#osyunwei是自己创建的命名空间

#php-fpm是自己创建的仓库名称

#:7.1.33是自定义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.1.33

5.4拉取阿里云容器镜像

docker pull registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.1.33

至此,Docker下使用Dockerfile基于centos基础镜像构建php-fpm容器镜像教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2021 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接