1. 程式人生 > >構建swoole docker映象(基於alpine基礎映象)

構建swoole docker映象(基於alpine基礎映象)

這裡預設你已經安裝好docker,並準備好以下環境

一.環境

  1. Ubuntu 18.04.1 LTS
  2. php:7.2.4-cli-alpine3.7 (https://hub.docker.com/_/php/ 拉取)
  3. swoole-4.2.1 (https://pecl.php.net/package/swoole 下載)
  4. 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