温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker怎么自定义镜像构建php7

发布时间:2022-03-24 17:43:34 来源:亿速云 阅读:159 作者:iii 栏目:web开发

这篇文章主要讲解了“docker怎么自定义镜像构建php7”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“docker怎么自定义镜像构建php7”吧!

首先进行简单的docker安装。

要进行自定义镜像,我们需要选择一个基础镜像进行构建自己的镜像:其实说白了,就是在一个有基础定义好的容器内,执行安装各种程序的命令,生成 所谓的dockerfile 文件,既然如此第一步我们首先需要找一个本地的镜像作为基础镜像来操作即可:

1

docker怎么自定义镜像构建php7
如上图所示,我们来以centos为基础镜像,来构建一个dockerfile

2第二步我们需要构建一个目录,用于存放dockerfile文件

docker怎么自定义镜像构建php7

在root下构建docker_demo目录,存放 dockerfile文件以及需要安装的程序文件即可,因为我要搭建php的自定义环境,所以我们再来搞一个php7的压缩包即可

wget http://am1.php.net/get/php-7.0.0.tar.gz/from/this/mirror

docker怎么自定义镜像构建php7

然后改名字

docker怎么自定义镜像构建php7

现在php nginx都有了,至于composer可以在php安装成功以后再自行操作即可~~~

接下来就是编写dockerfile文件了,在此之前,简单了解下dockerfile编写的关键字格式:

from 代表基于哪个镜像

run 安装软件使用

maintainer 镜像的创建者

cmd 容器启动时执行的命令,但是一个dockerfile中只能有一条cmd命令,多条则只执行最后一条cmd

entrypoint 容器启动时执行的命令,但是一个dockerfile中只能有一条cmd命令,多条则只执行最后一条

user 使用哪个用户运行container

expose 容器内部服务暴露的端口,主机上还需要在run容器时,做端口映射:

docker run -d -p 80:8080 centos6xxx

上边命令表示把容器内部的8080端口映射到主机80端口上

env 用来设置环境变量

add 将主机上的文件拷贝到container内的对应路径,所有拷贝到容器中的文件和文件夹权限为0755,uid和gid为0,如果文件是可识别的压缩格式,则docker会帮忙解压缩,add只有在build镜像的时候运行一次,后面运行container的时候不会再重新加载了。

例子如:

add nginx-1.12.2.tar.gz /usr/local/src

volume 可以将本地文件夹或者其他容器的文件夹挂在到容器内。

workdir 切换目录使用,(相当于cd目录)

onbuild 指定的命令在构建镜像时不执行,而是在它的子镜像中执行。

学完了dockerfile基础命令,我们来试着搞一搞这个环境.

docker pull centos

首先下载一个基础镜像,如果有这一步请忽略,下面是我的dockerfile

# base image
# 基础镜像
from docker.io/centos

# maintainer编写者
maintainer xy61521@163.com

# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx 来吧nginx 和php提前都放进基础镜像的/usr/local/src目录下,方便编译安装
add nginx-1.12.2.tar.gz /usr/local/src
add php-7.0.0.tar.gz /usr/local/src

# running required command 安装nginx的一系列乱七八糟的依赖包
run yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
run yum install -y libxslt-devel -y gd gd-devel geoip geoip-devel pcre pcre-devel
run useradd -m -s /sbin/nologin nginx

# change dir to /usr/local/src/nginx-1.12.2
workdir /usr/local/src/nginx-1.12.2

# execute command to compile nginx
run ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install

#先装个本地mysql
run yum install -y wget
run wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
run rpm -ivh mysql57-community-release-el7-8.noarch.rpm
run yum install -y mysql-server


#截止此,开始安装php,宇宙惯例,开始安装一些编译的依赖包
run yum -y install epel-release
run yum -y install libmcrypt-devel
run yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel
workdir /usr/local/src/php-7.0.0
#编译 安装
run ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-config-file-scan-dir=/usr/local/php7/etc/php.d --with-mcrypt=/usr/include --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-gd --with-iconv --with-zlib --enable-xml --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache && make && make install

run cp php.ini-production /usr/local/php7/etc/php.ini

构建成功dockerfile文件之后,docker build进行构建

docker build -t centos_lnmp:v1 .

后边的.代表相对路径当前目录,也可使用绝对路径

然后就是漫长的等待

docker怎么自定义镜像构建php7

直到构建成功镜像,至此我们重新开始

docker images

docker怎么自定义镜像构建php7

我们看到该镜像已经构建成功(有一点几率构建失败,失败的话删除容器和镜像重新构建即可),然后运行

docker run -dt -p 80:80 centos_lnmp:v1

docker怎么自定义镜像构建php7

成功后则可进入容器,配置nginx php 。

感谢各位的阅读,以上就是“docker怎么自定义镜像构建php7”的内容了,经过本文的学习后,相信大家对docker怎么自定义镜像构建php7这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI