最近在工作中用到了docker,不得不给大家推荐一下,真乃神器,下面分享一下我的镜像构建步骤(正常情况下1小时内可以完成):
0.准备工作
先在本地建立一个工作目录,我这里是~/docker/centos/,此目录下新建download目录,然后下载这几个包:libxml2-2.9.3.tar.gz、nginx-1.8.0.tar.gz、pcre-8.38.tar.gz、php-7.0.33.tar.gz、zlib-1.2.11.tar.gz、libiconv-1.14.tar.gz (下载链接: https://pan.baidu.com/s/1JceNQGndSGcTOQBiRnERvA 提取码: mfsv )
1.拉取centos镜像作为基础镜像
docker pull centos:7
2.启动镜像容器
docker run -d -i -t -p 80:80 centos:7 /bin/bash
3.安装gcc 和 gcc-c++
yum install -y gcc gcc-c++ automake autoconf libtool make openssl openssl-devel wget
4.创建所需目录
mkdir /web
mkdir /opt/nginx
mkdir /opt/php
mkdir /opt/packages
mkdir /download/source
5.关闭容器,重新启动容器并且映射宿主机目录
docker run -d -i -t -p 80:80 -v ~/docker/centos/download:/temp centos:7 /bin/bash
6.拷贝所需文件
cd /temp
cp * /download/source
7.安装pcre (不需要编译,解压就行)
tar -zxvf pcre-8.38.tar.gz
8.安装zlib (不需要编译,解压就行)
tar -zxvf zlib-1.2.11.tar.gz
9.安装nginx
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/opt/nginx --with-pcre=/download/source/pcre-8.38 --with-zlib=/download/source/zlib-1.2.11
make
make install
10.安装libxml2
tar -zxvf libxml2-2.9.3.tar.gz
cd libxml2-2.9.3
./configure --prefix=/opt/packages/libxml2 --with-python=no
make
make install
11.安装libiconv (这里有个小问题 https://blog.csdn.net/zsl10/article/details/52143713)
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/opt/packages/libiconv
make && make install
12.安装php
yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libmcrypt-devel mhash-devel bzip2-devel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install -y libmcrypt-devel mhash-devel mcrypt
tar -zxvf php-7.0.33.tar.gz
cd php-7.0.33
./configure --prefix=/opt/php/php-7.0.33 \
--with-iconv-dir=/opt/packages/libiconv \
--with-libxml-dir=/opt/packages/libxml2 \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-kerberos \
--with-libdir=lib64 \
--with-mysqli \
--with-openssl \
--with-openssl-dir= \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--with-config-file-path=/opt/php/php-7.0.33/etc
make
make install
13.配置php
cd /download/source/php-7.0.33
cp php.ini-production /opt/php/php-7.0.33/etc/php.ini
cd /opt/php/php-7.0.33/etc
cp php-fpm.conf.default php-fpm.conf
cd /opt/php/php-7.0.33/etc/php-fpm.d
cp www.conf.default www.conf
14.设置环境变量
编辑 /etc/profile 加入
export NGINX_HOME=/opt/nginx
export PATH=$PATH:$NGINX_HOME/sbin
export PHP_HOME=/opt/php/php-7.0.33
export PATH=$PATH:$PHP_HOME/bin
export PATH=$PATH:$PHP_HOME/sbin
执行 source /etc/profile
vi ~/.bashrc
添加 source /etc/profile
15.开机自动启动[https://blog.csdn.net/qq_34815528/article/details/93301220](systemctl 命令需要在特殊权限下使用,所以先退出容器,打包镜像)
docker run --privileged=true -d -i -t -p 80:80 -v ~/docker/centos/download:/temp centos:7 /usr/sbin/init
vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
vi /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/opt/php/php-7.0.33/sbin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动服务
systemctl start nginx.service
systemctl start php-fpm.service
开机自动启动
systemctl enable nginx.service
systemctl enable php-fpm.service
16.时区设置
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
17.提交保存镜像
docker commit [containerId] centos:7
18.编写配置文件,开始调试
docker run --privileged=true -d -i -t -v ~/docker/centos/hosts:/etc/hosts -v ~/docker/centos/nginx/conf/conf.d:/opt/nginx/conf/conf.d -v ~/docker/centos/nginx/conf/nginx.conf:/opt/nginx/conf/nginx.conf -v ~/docker/centos/nginx/logs:/opt/nginx/logs -v ~/workspace/code/php:/web -v ~/docker/centos/php/php.ini:/opt/php/php-7.0.33/etc/php.ini -p 80:80 centos:7 /usr/sbin/init
19.发布镜像
登录阿里云镜像仓库
本地打tag
推送云端仓库