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

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

一、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.0.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

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拉取官方基础镜像

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

#我们使用debian进行构建

docker pull debian:stable #下载镜像

docker image ls #查看docker镜像

docker run -itd --name debian debian:stable #运行容器

docker ps #查看容器

[root@master01 src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

835309e4eb70 debian:stable "bash" 22 hours ago Up 22 hours debian

[root@master01 src]#

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

root@835309e4eb70:/# cat /etc/issue #查看系统版本

Debian GNU/Linux 10 \n \l

root@835309e4eb70:/# cat /etc/debian_version #查看系统版本

10.10

exit #退出容器

2、编写Dockerfile构建文件

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

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

#基于这个镜像进行操作

FROM debian:stable

#作者和邮箱

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

#RUN镜像操作指令

#更新debian 10系统的163源

RUN mv /etc/apt/sources.list /etc/apt/sources.list-bak

RUN echo "deb http://mirrors.163.com/debian/ buster main non-free contrib" > /etc/apt/sources.list && \

echo "deb http://mirrors.163.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list && \

echo "deb http://mirrors.163.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list && \

echo "deb-src http://mirrors.163.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list && \

echo "deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list && \

echo "deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list && \

echo "deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib" >> /etc/apt/sources.list && \

echo "deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib" >> /etc/apt/sources.list

#安装依赖包

RUN apt-get update && apt-get -y install vim nano 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

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

COPY openssl-1.0.2u.tar.gz cmake-3.21.1.tar.gz curl-7.76.1.tar.gz freetype-2.10.4.tar.gz jpegsrc.v9d.tar.gz libgd-2.3.1.tar.gz libmcrypt-2.5.8.tar.gz libpng-1.6.37.tar.gz libvpx-1.10.0.tar.gz libzip-1.7.3.tar.gz oniguruma-6.9.7.1.tar.gz openssl-1.1.1k.tar.gz php-7.0.33.tar.gz t1lib-5.1.2.tar.gz tiff-4.0.7.tar.gz yasm-1.3.0.tar.gz ./

#解压软件包

RUN ls *.tar.gz | xargs -n1 tar xzvf

#安装yasm

WORKDIR /usr/local/src/yasm-1.3.0

RUN ./configure && make && make install

#安装libmcrypt

WORKDIR /usr/local/src/libmcrypt-2.5.8

RUN ./configure && make && make install

#安装libvpx

WORKDIR /usr/local/src/libvpx-1.10.0

RUN ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 && make && make install

#安装tiff

WORKDIR /usr/local/src/tiff-4.0.7

RUN ./configure --prefix=/usr/local/tiff --enable-shared && make && make install

#安装libpng

WORKDIR /usr/local/src/libpng-1.6.37

RUN ./configure --prefix=/usr/local/libpng --enable-shared && make && make install

#安装freetype

WORKDIR /usr/local/src/freetype-2.10.4

RUN ./configure --prefix=/usr/local/freetype --enable-shared --enable-freetype-config && make && make install \

&& 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-9d

RUN ./configure --prefix=/usr/local/jpeg --enable-shared && make && make install

#安装GD库

WORKDIR /usr/local/src/libgd-2.3.1

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-5.1.2

RUN ./configure --prefix=/usr/local/t1lib --enable-shared && make without_doc && make install

#安装cmake

WORKDIR /usr/local/src/cmake-3.21.1

RUN ./configure && make && make install

#安装libzip

WORKDIR /usr/local/src/libzip-1.7.3

RUN mkdir build && cd build && cmake .. && make && make install

#安装oniguruma

WORKDIR /usr/local/src/oniguruma-6.9.7.1

RUN ./autogen.sh &&./configure --prefix=/usr && make && make install

#安装openssl

WORKDIR /usr/local/src/openssl-1.0.2u

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-7.76.1

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

RUN \cp -frp /usr/lib/x86_64-linux-gnu/libXpm.so* /usr/lib/ \

&& \cp -frp /usr/lib/x86_64-linux-gnu/libltdl.so* /usr/lib/

#创建php-fpm运行用户和组

RUN groupadd www && useradd -g www www -s /bin/false

#安装php7.0.x

WORKDIR /usr/local/src/php-7.0.33

RUN export LD_LIBRARY_PATH=/usr/local/libgd/lib \

&& ./configure --prefix=/usr/local/php70 --with-config-file-path=/usr/local/php70/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/php70/etc/php.ini \

&& cp /usr/local/php70/etc/php-fpm.conf.default /usr/local/php70/etc/php-fpm.conf \

&& cp /usr/local/php70/etc/php-fpm.d/www.conf.default /usr/local/php70/etc/php-fpm.d/www.conf \

&& sed -i "s/;pid = run\/php-fpm.pid/pid = run\/php-fpm.pid/g" '/usr/local/php70/etc/php-fpm.conf' \

&& sed -i "s/;daemonize = yes/daemonize = no/g" '/usr/local/php70/etc/php-fpm.conf' \

&& sed -i "s/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/g" '/usr/local/php70/etc/php-fpm.d/www.conf' \

&& sed -i "s/user = nobody/user = www/g" '/usr/local/php70/etc/php-fpm.d/www.conf' \

&& sed -i "s/group = nobody/group = www/g" '/usr/local/php70/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/php70/sbin/php-fpm"]

:wq! #保存退出

3、构建容器

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

或者

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

4、运行容器

docker image ls #查看docker镜像

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

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 b452aca76025 /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.0.33 590b12aed572 19 minutes ago 2.73GB

5.1重命名镜像标签

docker tag 590b12aed572 registry.cn-hangzhou.aliyuncs.com/osyunwei/php-fpm:7.0.33

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

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

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

#:7.0.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.0.33

5.4拉取阿里云容器镜像

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

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

扩展阅读:

docker ps -a #查看正在运行的容器

docker stop php-fpm #停止正在运行的容器

docker start php-fpm #启动一个已经停止的容器

docker rm php-fpm #删除容器

     

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

给我留言

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



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