Docker:PHP7镜像构建
一.前言
公司php项目准备迁入docker,kubernetes进行管理,本文将介绍如何制作镜像,如何安装php扩展。
官方文档:https://hub.docker.com/_/php
二.Dockerfile
1.安装依赖
apk update &&apk add --no-cache autoconf gcc g++ libtool \ make libmemcached zlib libmemcached-dev libpng \ libpng-dev freetype-dev libjpeg-turbo-dev gettext-dev libmcrypt-dev\
2.安装gd库
gettext sockets pdo_mysql mysqli 同样的方式安装
docker-php-ext-configure gd \ --with-freetype-dir=/usr/include/ \ --with-jpeg-dir=/usr/include/ \ --with-png-dir=/usr/include/ \ docker-php-ext-install -j$(nproc) gd gettext sockets pdo_mysql mysqli
3.安装redis扩展
swoole mcrypt redis memcached 用pecl 安装,可指定版本号
pecl install mcrypt pecl install igbinary pecl install swoole-4.4.17 pecl install redis-5.2.1 pecl install memcached docker-php-ext-enable memcached igbinary swoole redis memcached
4.dickerfile文件
FROM php:7.2.16-fpm-alpine #更换阿里源 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories\ #nobody uid和gid改为99 && sed -i s/65534/99/g /etc/passwd &&sed -i s/65534/99/g /etc/group \ && apk update &&apk add --no-cache autoconf gcc g++ \ libtool make libmemcached zlib libmemcached-dev libpng \ libpng-dev freetype-dev libjpeg-turbo-dev gettext-dev libmcrypt-dev\ &&docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd gettext sockets pdo_mysql mysqli\ #mcrypt &&pecl install mcrypt\ && docker-php-ext-enable mcrypt\ #igbinary && pecl install igbinary\ && docker-php-ext-enable igbinary\ #swoole && pecl install swoole-4.4.17\ && docker-php-ext-enable swoole\ #redis && pecl install redis-5.2.1\ && docker-php-ext-enable redis\ #memcached && pecl install memcached\ && docker-php-ext-enable memcached\ && mkdir -p /data/logs&&mkdir -p /data/www COPY php.ini $PHP_INI_DIR/php.ini COPY www.conf /usr/local/etc/php-fpm.d/www.conf WORKDIR /data/www
5.配置文件
php.ini
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [PHP] 2 engine = On 3 short_open_tag = Off 4 asp_tags = Off 5 precision = 14 6 output_buffering = 4096 7 zlib.output_compression = Off 8 implicit_flush = Off 9 unserialize_callback_func = 10 serialize_precision = 17 11 disable_functions = system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,dl,popen,pcntl_exec,socket_accept,socket_bind,socket_clear_error,socket_close,socket_connect,socket_create_listen,socket_create_pair,socket_create,socket_get_option,socket_getpeername,socket_getsockname,socket_last_error,socket_listen,socket_read,socket_recv,socket_recvfrom,socket_select,socket_send,socket_sendto,socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,socket_strerror,socket_write,stream_socket_client,stream_socket_server,pfsockopen,disk_total_space,disk_free_space,diskfreespace,getrusage,get_current_user,getmyuid,getmypid,dl,leak,listen,chgrp,link,symlink,dlopen,proc_nice,proc_get_stats,proc_terminate,shell_exec,sh2_exec,posix_getpwuid,posix_getgrgid,posix_kill,ini_restore,mkfifo,dbmopen,dbase_open,filepro,filepro_rowcount,posix_mkfifo 12 disable_classes = 13 zend.enable_gc = On 14 expose_php = On 15 max_execution_time = 30 16 max_input_time = 60 17 memory_limit = 128M 18 error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT 19 display_errors = Off 20 display_startup_errors = Off 21 log_errors = On 22 log_errors_max_len = 1024 23 ignore_repeated_errors = Off 24 ignore_repeated_source = Off 25 report_memleaks = On 26 track_errors = Off 27 html_errors = On 28 variables_order = "GPCS" 29 request_order = "GP" 30 register_argc_argv = Off 31 auto_globals_jit = On 32 post_max_size = 8M 33 auto_prepend_file = 34 auto_append_file = 35 default_mimetype = "text/html" 36 default_charset = "UTF-8" 37 doc_root = 38 user_dir = 39 enable_dl = Off 40 file_uploads = On 41 upload_max_filesize = 2M 42 max_file_uploads = 20 43 allow_url_fopen = On 44 allow_url_include = Off 45 default_socket_timeout = 60 46 [CLI Server] 47 cli_server.color = On 48 [Date] 49 date.timezone = "Asia/Shanghai" 50 [filter] 51 [iconv] 52 [intl] 53 [sqlite3] 54 [Pcre] 55 [Pdo] 56 [Pdo_mysql] 57 pdo_mysql.cache_size = 2000 58 pdo_mysql.default_socket= 59 [Phar] 60 [mail function] 61 SMTP = localhost 62 smtp_port = 25 63 mail.add_x_header = On 64 [SQL] 65 sql.safe_mode = Off 66 [ODBC] 67 odbc.allow_persistent = On 68 odbc.check_persistent = On 69 odbc.max_persistent = -1 70 odbc.max_links = -1 71 odbc.defaultlrl = 4096 72 odbc.defaultbinmode = 1 73 [Interbase] 74 ibase.allow_persistent = 1 75 ibase.max_persistent = -1 76 ibase.max_links = -1 77 ibase.timestampformat = "%Y-%m-%d %H:%M:%S" 78 ibase.dateformat = "%Y-%m-%d" 79 ibase.timeformat = "%H:%M:%S" 80 [MySQL] 81 mysql.allow_local_infile = On 82 mysql.allow_persistent = On 83 mysql.cache_size = 2000 84 mysql.max_persistent = -1 85 mysql.max_links = -1 86 mysql.default_port = 87 mysql.default_socket = 88 mysql.default_host = 89 mysql.default_user = 90 mysql.default_password = 91 mysql.connect_timeout = 60 92 mysql.trace_mode = Off 93 [MySQLi] 94 mysqli.max_persistent = -1 95 mysqli.allow_persistent = On 96 mysqli.max_links = -1 97 mysqli.cache_size = 2000 98 mysqli.default_port = 3306 99 mysqli.default_socket = 100 mysqli.default_host = 101 mysqli.default_user = 102 mysqli.default_pw = 103 mysqli.reconnect = Off 104 [mysqlnd] 105 mysqlnd.collect_statistics = On 106 mysqlnd.collect_memory_statistics = Off 107 [OCI8] 108 [PostgreSQL] 109 pgsql.allow_persistent = On 110 pgsql.auto_reset_persistent = Off 111 pgsql.max_persistent = -1 112 pgsql.max_links = -1 113 pgsql.ignore_notice = 0 114 pgsql.log_notice = 0 115 [Sybase-CT] 116 sybct.allow_persistent = On 117 sybct.max_persistent = -1 118 sybct.max_links = -1 119 sybct.min_server_severity = 10 120 sybct.min_client_severity = 10 121 [bcmath] 122 bcmath.scale = 0 123 [browscap] 124 [Session] 125 session.save_handler = files 126 session.use_strict_mode = 0 127 session.use_cookies = 1 128 session.use_only_cookies = 1 129 session.name = PHPSESSID 130 session.auto_start = 0 131 session.cookie_lifetime = 0 132 session.cookie_path = / 133 session.cookie_domain = 134 session.cookie_httponly = 135 session.serialize_handler = php 136 session.gc_probability = 1 137 session.gc_divisor = 1000 138 session.gc_maxlifetime = 1440 139 session.referer_check = 140 session.cache_limiter = nocache 141 session.cache_expire = 180 142 session.use_trans_sid = 0 143 session.hash_function = 0 144 session.hash_bits_per_character = 5 145 url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" 146 [MSSQL] 147 mssql.allow_persistent = On 148 mssql.max_persistent = -1 149 mssql.max_links = -1 150 mssql.min_error_severity = 10 151 mssql.min_message_severity = 10 152 mssql.compatibility_mode = Off 153 mssql.secure_connection = Off 154 [Assertion] 155 [COM] 156 [mbstring] 157 [gd] 158 [exif] 159 [Tidy] 160 tidy.clean_output = Off 161 [soap] 162 soap.wsdl_cache_enabled=1 163 soap.wsdl_cache_dir="/tmp" 164 soap.wsdl_cache_ttl=86400 165 soap.wsdl_cache_limit = 5 166 [sysvshm] 167 [ldap] 168 ldap.max_links = -1 169 [mcrypt] 170 [dba] 171 zend_extension=opcache.so 172 opcache.enable=1 173 opcache.enable_cli=0 174 opcache.memory_consumption=256 175 opcache.interned_strings_buffer=8 176 opcache.max_accelerated_files=10000 177 opcache.max_wasted_percentage=5 178 opcache.use_cwd=0 179 opcache.validate_timestamps=1 180 opcache.revalidate_freq=60 181 opcache.revalidate_path=0 182 opcache.save_comments=1 183 opcache.fast_shutdown=1 184 opcache.enable_file_override=0 185 opcache.optimization_level=1 186 opcache.consistency_checks=0 187 opcache.file_cache="/tmp/phpop" 188 opcache.huge_code_pages=1 189 opcache.fast_shutdown=1 190 [curl] 191 [openssl]
www.conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [global] 2 [www] 3 user = nobody 4 group = nobody 5 listen = 0.0.0.0:9000 6 pm = dynamic 7 pm.max_children = 30 8 pm.start_servers = 5 9 pm.min_spare_servers = 1 10 pm.max_spare_servers = 5 11 pm.process_idle_timeout = 20s; 12 pm.max_requests = 5000 13 slowlog = /data/logs/php/slow.log 14 request_slowlog_timeout = 5 15 security.limit_extensions = .php .html .htm 16 php_admin_value[error_log] = /data/logs/php/php-fpm.err.log 17 php_admin_flag[log_errors] = on 18 php_admin_value[memory_limit] = 128M
注:配置文件仅供参考