Docker制作PHP镜像并运行

一、docker制作PHP镜像

https://cloud.tencent.com/developer/article/1636417

1、自定义网络

shell>docker network create lnmp

2、创建mysql容器(容器同步时间)

shell>docker run --name lnmp_mysql --net lnmp \
--restart unless-stopped \
-d -p 3310:3306 -v /data/app/mysql/conf:/etc/mysql/conf.d \
-v /data/app/mysql/data:/var/lib/mysql \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 \
--character-set-server=utf8 --collation-server=utf8_general_ci

3、创建PHP容器

创建一个Dockerfile

FROM php:7.3-fpm-alpine3.11
RUN apk update && apk add m4 autoconf make gcc g++ linux-headers && \
    docker-php-ext-install pdo_mysql opcache mysqli && \
    apk del m4 autoconf make gcc g++ linux-headers

EXPOSE 9000
WORKDIR /app
CMD [ "php-fpm", "-F"]

docker build -t php:v2 .

docker run -d –name php –net lnmp -v /data/app/nginx/html:/usr/src/myapp php:v2

4、创建nginx容器

4.1创建php.conf文件

server {
    listen 80;
    server_name example.ctnrs.com;
    index index.php index.html;

    access_log logs/www.ctnrs.com_access.log;
    error_log logs/www.ctnrs.com_error.log;

    location / {
        root /wwwroot;
    }

    location ~* \.php$ {
        root /wwwroot;
        fastcgi_pass lnmp_php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

创建一个index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
    <h1>我的第一个标题</h1>
    <p>我的第一个段落。</p>
</body>
</html>

创建nginx的Dockerfile

FROM nginx
COPY php.conf /usr/local/nginx/conf/vhost/php.conf
COPY index.html /usr/share/nginx/html/index.html
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx
WORKDIR /usr/share/nginx/html

docker build -t nginx:v1 .

docker run –name lnmp_nginx –net lnmp -d -p 88:80 -v /wwwroot:/usr/share/nginx/html nginx:v2

二、docker-compose部署PHP