这里默认你已经安装好docker,并准备好以下环境
一.环境
- Ubuntu 18.04.1 LTS
- php:7.2.4-cli-alpine3.7 (https://hub.docker.com/_/php/ 拉取)
- swoole-4.2.1 (https://pecl.php.net/package/swoole 下载)
- docker version
$ docker version
Client:
Version: 18.05.0-ce
API version: 1.37
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:16:13 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.05.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:14:23 2018
OS/Arch: linux/amd64
Experimental: false
二.基于pecl构建
$ mkdir build-swoole && cd build-swoole
$ vi dockerfile
在 Dockerfile 中写入如下内容
FROM php:7.2.4-cli-alpine3.7
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.7/main > /etc/apk/repositories && \
echo http://mirrors.ustc.edu.cn/alpine/v3.7/community >> /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add m4 autoconf make gcc g++ linux-headers
RUN pecl install swoole-4.2.1
RUN docker-php-ext-enable swoole
CMD \["php","-m"\]
$ docker build --no-cache -t php:7.2.4-swoole-alpine3.7 .
由于网络等原因,基于pecl构建的方式有时成功,有时失败。下面介绍一种更稳定的构建方式
三.编译方式构建
$ mkdir -p build-swoole/install && cd build-swoole
$ wget -c https://pecl.php.net/get/swoole-4.2.1.tgz -P ./install
$ vi Dockerfile
在 Dockerfile 中写入如下内容
FROM php:7.2.4-cli-alpine3.7
RUN echo http://mirrors.ustc.edu.cn/alpine/v3.7/main > /etc/apk/repositories && \
echo http://mirrors.ustc.edu.cn/alpine/v3.7/community >> /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add m4 autoconf make gcc g++ linux-headers
ADD ./install/swoole-4.2.1.tgz /tmp/
RUN cd /tmp/swoole-4.2.1 && phpize && ./configure && make && make install
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-enable swoole
CMD \["php","-m"\]
$ docker build --no-cache -t php:7.2.4-swoole-alpine3.7 .
tips: dockerfile中我加了 pdo_mysql 扩展
查看是否构建成功
$ docker run -it --rm --name swoole php:7.2.4-swoole-alpine3.7 sh -c "php -m | grep swoole"
swoole
出现swoole则表示构建成功