用Docker制作PHP环境镜像

Author Avatar
Moon 4月 14, 2018

准备工作

开始制作镜像

  1. 拉取php官方镜像
1
docker pull php:7.2.4-cli-alpine3.7
  1. 进入alpine镜像
1
docker run -it php:7.2.4-cli-alpine3.7 sh
  1. 更新系统包
1
apk update && apk upgrade -y
  1. 安装依赖

PHP 7.2:

1
2
3
4
5
6
# icu-dev用于后面安装intl扩展
# libpng-dev用于后面安装gd扩展
# openssl-dev用于后面安装mongodb扩展
apk add icu-dev libpng-dev libmcrypt-dev openssl-dev autoconf g++ make
# 如果安装swoole支持,需要安装以下依赖
apk add nghttp2-dev hiredis-dev

PHP 5.6:

1
apk add icu-dev libpng-dev libmcrypt-dev openssl-dev autoconf g++ make

  1. 安装扩展

PHP 7.2:

1
docker-php-ext-install bcmath intl gd opcache pdo_mysql

PHP 5.6:

1
docker-php-ext-install bcmath intl gd mcrypt mysql mysqli opcache pdo_mysql

  1. 安装PECL扩展并启用

PHP 7.2:

1
2
3
4
pecl install mcrypt mongodb igbinary
pecl install channel://pecl.php.net/mcrypt-1.0.1

pecl install redis swoole

1
2
docker-php-ext-enable mcrypt mongodb igbinary
docker-php-ext-enable redis swoole

PHP 5.6:

1
2
pecl install mongo igbinary
pecl install memcache redis

1
2
docker-php-ext-enable mongo igbinary
docker-php-ext-enable memcache redis
  1. 提交当前状态
1
docker commit [CONTAINER_ID] [REPOSITORY[:TAG]]

请自行替换上面的CONTAINER_IDREPOSITORYTAG

  1. 推送到docker hub
    1
    docker push mooncool/php-cli:5.6