# 基于Alpine构建php7+nginx(1)

什么是Alpine？

　　Alpine 是一个基于musl libc和busybox的面向安全性的轻量级Linux发行版。简单来说就是一个精简的linux系统。最新的3.11.6标准版本安装包只有120M左右。

为什么基于Alpine构建我们的docker环境呢？

　　基于docker的部署方式给运维工作带来了极大的便利，但是镜像的大小有时候影响了部署的速度。基于apline构建的镜像可以明显减小镜像的存储占用。方便拷贝和下载。

如何快速构建镜像：

　　一般的web项目都是lnmp方式部署。但是考虑到数据独立存储。我在镜像中只集成了php73 和 nginx.

先写个启动脚本 run.sh，内容如下：

```bash
#!/bin/sh
mkdir -p /run/nginx
/usr/sbin/nginx
/usr/sbin/php-fpm7
tail -f /etc/hosts
```

再将如下内容复制到Dockerfile中，跟run.sh 放在一起。

```bash
FROM alpine:3.11
COPY run.sh /
 
RUN cd / && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk update  && \
    apk --no-cache add  curl  openssh bash libxml2-dev openssl-dev libjpeg-turbo-dev libpng-dev libxpm-dev freetype-dev gd-dev gettext-dev libmcrypt-dev  && \
    addgroup www  && \
    adduser -G www -D -s /sbin/nologin www  && \
    apk --no-cache add nginx && \
    apk --no-cache add php7 php7-fpm php7-opcache php7-curl php7-gd php7-mbstring php7-mysqli php7-json php7-mcrypt php7-redis php7-pdo  redis  && \
    rm -rf  /var/cache/apk/* && \
    chmod +x /run.sh
EXPOSE  80
ENTRYPOINT ["/run.sh"]
#CMD ["nginx", "-g", "daemon off;"]
 
```

然后执行命令

```bash
docker build -t alpine-php7.3.22-nginx1.16.1:v1 .
```

```bash
docker run  --rm --name test  alpine-php7.3.22-nginx1.16.1:v1
```

最终效果

![](/files/VgxZQ5r1F8QCoV5V3hf2)

至此，php-fpm和nginx都已启动。

可以将自己php和nginx的配置文件映射至容器的如下两个目录

> /etc/php7/php.ini
>
> /etc/nginx/conf.d/default.conf

如果线上使用，最好在Dockerfile中指定安装的php和nginx版本。并经过运维的审核。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/docker/docker-compose/ji-yu-alpine-gou-jian-php7+nginx1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
