【03】dockerfile 構(gòu)建鏡像

docker 鏡像我通常會使用官方的,但是也有例外,比如php-fpm、ffmpeg鏡像,這些模塊化的應(yīng)用通常滿足不了所有場景,因?yàn)楣俜桨l(fā)布的鏡像無法把所有支持的模塊都編譯到鏡像里,因此docker提供了一種自建鏡像的方法dockerfile,它類似腳本,按照一定語法編寫完成后,在使用docker build命令將其構(gòu)建成需要的鏡像。

示例:

  • 基于cetnos7官方鏡像構(gòu)建一個(gè)php-fpm鏡像,使用源碼安裝編譯
  • 創(chuàng)建dockerfile,需要將php-5.6.36.tar.gz 源碼包與dockerfile放在同一級目錄下
cat <<eof> php56-dockerfile
from centos
copy php-5.6.36.tar.gz /root/
run curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum  -y install php-mcrypt libmcrypt libmcrypt-devel make gcc-c++ \
libxml2-devel openssl-devel libcurl-devel libjpeg.x86_64 \
libpng.x86_64 freetype libjpeg-devel libpng-devel.x86_64 \
freetype-devel.x86_64 libjpeg-turbo-devel libmcrypt-devel \
mysql-devel  bzip2 bzip2-devel gd ttf  freetype && \
cd /root/ && tar xvf php-5.6.36.tar.gz && \
cd php-5.6.36 && \
./configure --prefix=/php --with-config-file-path=/php/etc/ \
--enable-mysqlnd  --with-gd --enable-gd-native-ttf    \
--with-bz2 --with-curl  --enable-fpm  --enable-sockets   \
--with-jpeg-dir=/php/local  --with-png-dir=/php \
--with-freetype-dir=/php --with-iconv-dir=/php \
--enable-mbstring --enable-calendar --with-gettext \
--with-libxml-dir=/php --with-zlib --enable-xml \
--with-libdir=lib64 --enable-bcmath --with-openssl  \
--enable-dom --with-mysql=mysqlnd --with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd   --with-mcrypt   && \
make -j`grep -c processor /proc/cpuinfo` > /dev/null && \
make install && \
make clean && \
ln -s /php/bin/* /usr/bin/ && \
ln -s /php/sbin/* /usr/bin/ && \
cp php.ini-production  /php/etc/php.ini && \
cp /php/etc/php-fpm.conf.default /php/etc/php-fpm.conf && \
yum -y remove make gcc-c++ && \
yum clean all

expose 9000
cmd ["php-fpm", "--nodaemonize"]
eof

? 構(gòu)建命令
docker build -t php-fpm:5.6-centos7 -f php56-dockerfile ./

注意:構(gòu)建過程中會報(bào)錯(cuò),比如權(quán)限問題、文件或目錄不存在,根據(jù)提示查找錯(cuò)誤位置并修改dockerfile內(nèi)容,然后重新構(gòu)建即可


疑問

  1. 為什么使用cnetos鏡像作為dockerfile的base image?
    因?yàn)槲覀€(gè)人比較熟悉操作centos系統(tǒng),base image并非強(qiáng)制,你可以替換自己喜歡的base image
  2. 為什么源碼安裝php-fpm?
    擴(kuò)展php模塊時(shí)方便,并且國內(nèi)centos系統(tǒng)文檔多
  3. 為什么構(gòu)建的鏡像特別大,一般都在800M~1000M之間?
    這取決于base image (基礎(chǔ)鏡像),容器的設(shè)計(jì)就是每當(dāng)要修改base image時(shí),先將base image復(fù)制一層,然后再將修改操作應(yīng)用到新復(fù)制的鏡像層中
    比如docker官方主推ubantu作為base image,其鏡像大小只有100M左右,如果你dockerfile中只寫run echo "test" > /opt/test.txt
    你會發(fā)現(xiàn)構(gòu)建完成后的鏡像要比base image大了好幾十兆,其實(shí)你只是添加了一個(gè)幾KB的文件。
  4. 什么是基礎(chǔ)鏡像(base image)
    簡單來說,基礎(chǔ)鏡像就是沒有From或者FROM scratch開頭的Dockerfile所構(gòu)建出來的鏡像。比如alpine,這個(gè)很小的linux鏡像目前只有4M左右
    據(jù)說docker官方要使用alpine作為官方專用base image,但目前官方未公布。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容