業務 API 1.0 服務,因服務器遷移須要對線上服務編寫 Dockerfile 進行容器化改造,因爲以前接觸 Docker 較少,故用本文檔記錄 Dockerfile 編寫過程。php
不變更代碼的狀況下,進行容器化支持mysql
Dockerfile
選用,官方歷史維護的PHP 5.3.29
版本。由 Github 上某熱心網友官方中止維護前 fork 下來並進行修改的。nginx
借鑑地址爲: www.github.com/helderco/do…git
RUN sed -i "s#[a-z]\+.debian.org#mirrors.163.com#g" /etc/apt/sources.list
複製代碼
docker-php-ext*
等腳本進行安裝pecl
工具安裝,須要將擴展的配置添加到php.ini
中源碼
編譯安裝(phpize),須要在php.ini
中,添加對應配置# No.1 - docker 腳本
RUN set -ex \
&& docker-php-ext-install \
bcmath \
bz2 \
ftp \
mbstring \
mcrypt \
pcntl \
pdo_mysql \
mysqli \
shmop \
soap \
sockets \
sysvsem
# No.2 - pecl (適用於高版本php鏡像)
RUN pecl install redis \
&& docker-php-ext-enable redis
# No.3 - 源碼
RUN set -ex \
&& cd /tmp \
&& curl -sL -C - --retry 5 --retry-delay 3 http://pecl.php.net/get/memcache-2.2.5.tgz -o memcache-2.2.5.tgz \
&& tar -xzf memcache-2.2.5.tgz \
&& cd /tmp/memcache-2.2.5 \
&& phpize \
&& ./configure && make && make install \
&& rm -rf /tmp/memcache-2.2.5* \
&& echo 'extension="memcache.so"' >> $PHP_INI_CONF \
複製代碼
RUN
指令,sed
進行文件修改。COPY
指令,將寫好的php-fpm.conf
導入鏡像內。# No.1 - sed
RUN set -ex; \
sed -i -e "" $PHP_FPM_CONF; \
sed -i "s|;daemonize = .*|daemonize = no|" $PHP_FPM_CONF; \
sed -i "s|listen = .*|listen = 9000|" $PHP_FPM_CONF; \
sed -i "s|;emergency_restart_threshold = .*|emergency_restart_threshold = 10|" $PHP_FPM_CONF; \
sed -i "s|;emergency_restart_interval = .*|emergency_restart_interval = 1m|" $PHP_FPM_CONF; \
sed -i "s|;process_control_timeout = .*|process_control_timeout = 5s|" $PHP_FPM_CONF; \
sed -i "s|;rlimit_files = .*|rlimit_files = 51200|" $PHP_FPM_CONF; \
sed -i "s|;rlimit_core = .*|rlimit_core = 0|" $PHP_FPM_CONF; \
sed -i "s|pm.max_children = .*|pm.max_children = 14|" $PHP_FPM_CONF; \
sed -i "s|pm.start_servers = .*|pm.start_servers = 6|" $PHP_FPM_CONF; \
sed -i "s|pm.min_spare_servers = .*|pm.min_spare_servers = 3|" $PHP_FPM_CONF; \
sed -i "s|pm.max_spare_servers = .*|pm.max_spare_servers = 10|" $PHP_FPM_CONF; \
sed -i "s|;pm.max_requests = .*|pm.max_requests = 512|" $PHP_FPM_CONF; \
sed -i "s|;catch_workers_output = yes|catch_workers_output = yes|g" $PHP_FPM_CONF;
複製代碼
apt
安裝 : apt-get install nginx-full
源碼
安裝: 下載官方源碼包,而後編譯安裝# No.1 - apt 安裝
RUN apt-get update && apt-get install -y --no-install-recommends \
nginx-full \
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
&& apt-get clean \
&& rm -r /var/lib/apt/lists/
# No.2 - 源碼安裝 (未測試,僅列邏輯)
# 1. 可能須要提早 apt-get install 安裝 libpcre3-dev 包
# 2. curl -sL https://nginx.org/download/nginx-${NGX_VERSION}.tar.gz -o nginx-${NGX_VERSION}.tar.gz
# 3. ./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
複製代碼
COPY
指令,將寫好的配置導入鏡像內supervisor
配置導入鏡像,並在Dockerfile
中添加ENTRYPOINT
指令,用於啓動php-fpm
和nginx
服務# supervisor config
[supervisord]
nodaemon=true
loglevel=error
[program:php]
command=/usr/local/sbin/php-fpm
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes = 0
stderr_logfile_maxbytes = 0
[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes = 0
stderr_logfile_maxbytes = 0
# Dockerfile
ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
複製代碼
系統的倉庫源選用國內鏡像github
# 科大
mirrors.ustc.edu.cn
# 網易
mirrors.163.com
複製代碼
儘可能用官方維護的base鏡像
或Dockerfile
, 網絡不佳,可先pull
下來而後推送到公司內部倉庫.redis
服務日誌儘可能重定向到標準輸出中,方便查看日誌.sql
# nginx
RUN ln -sf /dev/stdout /xxx/nginx/logs/access.log \
&& ln -sf /dev/stderr /xxx/nginx/logs/error.log
# php
sed -i "s|;error_log = .*|error_log = /dev/stderr|g" /etc/php7/php-fpm.conf && \
sed -i "s|;catch_workers_output = yes|catch_workers_output = yes|g" /etc/php7/php-fpm.d/www.conf \
# supervisor
[supervisord]
...
loglevel=error
[program:php]
...
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
...
[program:nginx]
...
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
...
複製代碼
nginx 設置軟連到 stdout 或 stderr 便可。docker
php 須要在配置文件([global]下)把 error_log 設置爲 /dev/stderr, ([www])並添加參數 catch_workers_output = yes (若有 slow log ,一樣設置 stderr 便可)。shell
supervisor 注意配置裏,每一個 program 都要設置對應的 logfile 位置,一樣是 stdout 或 stderr。bash
容器內服務務必要前臺運行
# supervisor
nodaemon = true
# nginx
/usr/local/nginx/sbin/nginx -g 'daemon off;'
# php (修改配置)
daemonize = no
複製代碼
OS 系統選用
# 鏡像小,軟件安裝方便速度快,版本控制稍遜色
alpine
# 鏡像稍大,軟件安裝較爲便利,版本控制作的好
debian
複製代碼
編寫Dockerfile
時儘量保證版本控制,便於後續迭代發佈。
使用時,刪除不須要的 php 擴展,可加速鏡像構建過程。
Dockerfile
製做過程,對於特殊環境要求,須要邊找資料邊進行測試,過於複雜的環境配置,能夠適當增長鏡像分層,以便構建鏡像過程當中能夠重複利用 cache。