@[TOC]Docker构建PHP开发环境

推荐资源

悟尘记电子书 - 数据持久化存储与性能调优

获取镜像

官方镜像简介

Docker官网 PHP

19.png

php 版本区别 https://github.com/docker-library/docs/blob/master/php/README.md

  • php:<version>-cli : PHP CLI工具,不含有php-fpm
  • php:<version>-apache: Apache httpd 与 PHP (as mod_php) 并mpm_prefork在默认情况下使用
  • php:<version>-fpm: FastCGI 实现,含有php-fpm
  • php:<version>-alpine:最小化的linux版本,只有5M,公共的基础镜像,构建其它DockerFile使用
  • Debian系统:Debian 发行版本
    1. Debian 11 (bullseye) — 当前的稳定(stable)版
    2. Debian 10(buster) — 当前的旧的稳定(oldstable)版
    3. Debian 9(stretch) — 更旧的稳定(oldoldstable)版,现有长期支持
    4. Debian 8(jessie) — 已存档版本,现有扩展长期支持

Docker官网 NGINX

PHP

docker pull php:7.2.34-fpm
# 也可以下载其它版本

NGINX

docker pull nginx:1.21.4

查看下载的镜像

$ docker images
REPOSITORY   TAG          IMAGE ID       CREATED         SIZE
nginx        1.21.4       ea335eea17ab   4 days ago      141MB
php          7.2.34-fpm   28f52b60203d   11 months ago   398MB
# 查看镜像信息
$ docker image inspect nginx:1.21.4
[
    {
        "Id": "sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291",
        "RepoTags": [
            "nginx:1.21.4"
        ],
        "RepoDigests": [
            "nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-11-17T10:38:14.652464384Z",
        "Container": "8a038ff17987cf87d4b7d7e2c80cb83bd2474d66e2dd0719e2b4f7de2ad6d853",
        "ContainerConfig": {
            "Hostname": "8a038ff17987",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.21.4",
                "NJS_VERSION=0.7.0",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGQUIT"
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.21.4",
                "NJS_VERSION=0.7.0",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGQUIT"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 141490847,
        "VirtualSize": 141490847,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/b75e9dc88d758676d416f388fc66ba70c046c38703f099b31b5aa077e4198b38/diff:/var/lib/docker/overlay2/6f6c882105b26a3255e1e0ddd747439fa15144bcb406054f63eba01a4f729d07/diff:/var/lib/docker/overlay2/c98e2d255a0ce2df61af1349bf5eb18dd5fb02f507a4076723be8a940b81f062/diff:/var/lib/docker/overlay2/d99ed4c9ac5050329fc6e3baaf6bcb3504262d8b15425e02c27c1e9322e354fb/diff:/var/lib/docker/overlay2/3360850d369730a5ef8b4bc961468db9d1b249dcaf45938e8817d3a734a9bd0b/diff",
                "MergedDir": "/var/lib/docker/overlay2/3a0b87bca466fb3667065ff7de2bb96ad84f924da65161dc0a9e9a0b7003a99c/merged",
                "UpperDir": "/var/lib/docker/overlay2/3a0b87bca466fb3667065ff7de2bb96ad84f924da65161dc0a9e9a0b7003a99c/diff",
                "WorkDir": "/var/lib/docker/overlay2/3a0b87bca466fb3667065ff7de2bb96ad84f924da65161dc0a9e9a0b7003a99c/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325",
                "sha256:37380c5830feb5d6829188be41a4ea0654eb5c4632f03ef093ecc182acf40e8a",
                "sha256:ff4c727794302b5a0ee4dadfaac8d1233950ce9a07d76eb3b498efa70b7517e4",
                "sha256:49eeddd2150fbd14433ec1f01dbf6b23ea6cf581a50635554826ad93ce040b68",
                "sha256:1e8ad06c81b6baf629988756d90fd27c14285da4d9bf57179570febddc492087",
                "sha256:8525cde30b227bb5b03deb41bda41deb85d740b834be61a69ead59d840f07c13"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
$ docker image inspect php:7.2.34-fpm
[
    {
        "Id": "sha256:28f52b60203d1bf12eaafe8d2fabd7807f0954c44f425b572061957ba1797729",
        "RepoTags": [
            "php:7.2.34-fpm"
        ],
        "RepoDigests": [
            "php@sha256:9c84ae47fddb97b94d1d2e289635b7306142a5336bc4ece0a393458c5e0d2cef"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-12-11T10:57:53.906543729Z",
        "Container": "617f6a2155baa9a24ed6e3d40fdd93ed1f09bf85a5aa341f35fec2c2c7356732",
        "ContainerConfig": {
            "Hostname": "617f6a2155ba",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "9000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F",
                "PHP_VERSION=7.2.34",
                "PHP_URL=https://www.php.net/distributions/php-7.2.34.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.2.34.tar.xz.asc",
                "PHP_SHA256=409e11bc6a2c18707dfc44bc61c820ddfd81e17481470f3405ee7822d8379903"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"php-fpm\"]"
            ],
            "Image": "sha256:bd40e80d4ab8d91d2a284a5f6aad72c33c9985e3353c92a93f1e65431bd20765",
            "Volumes": null,
            "WorkingDir": "/var/www/html",
            "Entrypoint": [
                "docker-php-entrypoint"
            ],
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGQUIT"
        },
        "DockerVersion": "19.03.12",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "9000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F",
                "PHP_VERSION=7.2.34",
                "PHP_URL=https://www.php.net/distributions/php-7.2.34.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.2.34.tar.xz.asc",
                "PHP_SHA256=409e11bc6a2c18707dfc44bc61c820ddfd81e17481470f3405ee7822d8379903"
            ],
            "Cmd": [
                "php-fpm"
            ],
            "Image": "sha256:bd40e80d4ab8d91d2a284a5f6aad72c33c9985e3353c92a93f1e65431bd20765",
            "Volumes": null,
            "WorkingDir": "/var/www/html",
            "Entrypoint": [
                "docker-php-entrypoint"
            ],
            "OnBuild": null,
            "Labels": null,
            "StopSignal": "SIGQUIT"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 398055390,
        "VirtualSize": 398055390,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/bebacbcd4c959f9890ca8dd41ee8e6ec20f6f33c9642c822a55eb7750adbd5e3/diff:/var/lib/docker/overlay2/1cf79648132749087fd367a2ee41f3e93b4d902b154136e827f2ced04a08ff30/diff:/var/lib/docker/overlay2/ed76472920cfe7f6ad55bd86c9b1b00439a4d9367dfb41914684d49db8e8086d/diff:/var/lib/docker/overlay2/4501b25fd5783726f075429019049d567745739e38f07082fccfb7695ed5f3b6/diff:/var/lib/docker/overlay2/5f3d0174413ae8b3359b0a5821ad579a6a6f54e367218c12e5b3d5420a3215cb/diff:/var/lib/docker/overlay2/5633c3f562a1d84fc5aaca84c448fcb2661ffd369c41e5f9de9fb3aeab4adb53/diff:/var/lib/docker/overlay2/4e8878149f311b550d202bf0fb5c91b977f88923319ee365f071671f4833d484/diff:/var/lib/docker/overlay2/8af25c6dac955394065124ceaddbf0eee48be1c93d5020e3f1b0ca16f605017a/diff:/var/lib/docker/overlay2/d4ea600dc3b13081975b89a9e3695765568e3fa70c2b0737f7acbb6545922389/diff:/var/lib/docker/overlay2/6528a9a9ca2e0e770e2eb08c0ac035a49fb9737fe1d0148bb99e5d2862d74bb7/diff",
                "MergedDir": "/var/lib/docker/overlay2/1d138c89af18b155270c0c75ff25e99077ee8520be61079a229db15ddb819b1f/merged",
                "UpperDir": "/var/lib/docker/overlay2/1d138c89af18b155270c0c75ff25e99077ee8520be61079a229db15ddb819b1f/diff",
                "WorkDir": "/var/lib/docker/overlay2/1d138c89af18b155270c0c75ff25e99077ee8520be61079a229db15ddb819b1f/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9",
                "sha256:ddcd8d2fcf7eadeef5a1e569539568bd276784e3535cb942a51a19c538090e1e",
                "sha256:e45a78df7536d9ab717733086f8026d40bb78c2ee532cff284308424d0018828",
                "sha256:02eef72b445f93168347adc16dece8be0d697c462b4e3c293480d08e599127a8",
                "sha256:5dfc40c1b4dd9e2205c5239d0df36bc36799437ff2a88997d02bbf0f0cde75e7",
                "sha256:b65c3076245bd7c7ab8bf117972f6cc878fd5fad776850901337bb68f7ed5798",
                "sha256:197778d1001096914732113762fcc24c7601229b0f6e2dd9e6dc829204bc0b60",
                "sha256:9c60a520fbc245e87e2e8086b1d7029f159a99c38dd3757916305d7a8b6b5ad7",
                "sha256:70c4d730b0830e50978f56cab09a5674331ec1498cad1ff38229c8e0614fdc3a",
                "sha256:47003bc5e131de64223aa6b8290c75661495699fbd5eb68c00356bf642a05f90",
                "sha256:bc7ce6357b223090bf73ec8a69638fa6edd9edddad63c773aefbc95b2cfce3da"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

常用命令和配置阿里云镜像

查看系统信息

cat /etc/issue

 cat /etc/issue
Debian GNU/Linux 10 \n \l

 uname -a
Linux add813f24903 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 GNU/Linux

阿里云 Debian 镜像源 https://developer.aliyun.com/mirror/debian

cat > /etc/apt/sources.list << EOF
> deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
> EOF

# 更新源
apt-get update
# 安装
apt-get -y install inetutils-ping iproute2 net-tools vim psmisc procps

运行容器与配置

创建网络

docker network create dev_network

启动nginx

  • hosts 设置 127.0.0.1 test.com
  • 添加nginx目录,www
  • conf.d配置default.conf
server {
    listen       80;
    server_name  test.com;
    root   /www;
    index index.php  index.html index.htm;
    location / {
      try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        fastcgi_pass   php72:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
# linux 创建容器并启动
docker run -d -p 80:80 --name nginx --network dev_network \
-v "/web/www":/www \
-v "/web/nginx/conf.d":/etc/nginx/conf.d \
-v "/web/nginx/logs":/var/log/nginx  nginx:1.21.4

# windows 创建容器并启动
winpty docker run -d -p 80:80 --name nginx --network dev_network \
-v "E:\web/www":/www \
-v "E:\web/nginx/conf.d":/etc/nginx/conf.d \
-v "E:\web/nginx/logs":/var/log/nginx  nginx:1.21.4

#  容器内部命令使用
# 启动 
nginx 
# 重启 
nginx -s reload
# 查看端口
ss -tnl
State          Recv-Q         Send-Q                 Local Address:Port                  Peer Address:Port        Process
LISTEN         0              4096                      127.0.0.11:37627                      0.0.0.0:*
LISTEN         0              511                          0.0.0.0:80                         0.0.0.0:*

启动php

# linux 创建容器并直接启动
winpty docker run  -d --name php72 --network dev_network \
-v "/web/www":/www \
php:7.2.34-fpm

# windows 创建容器并直接启动
winpty docker run  -d --name php72 --network dev_network \
-v "E:\web/www":/www \
php:7.2.34-fpm

#  容器内部命令使用
# 启动php
php-fpm
# ss -tnl
State          Recv-Q         Send-Q                 Local Address:Port                  Peer Address:Port        Process
LISTEN         0              4096                      127.0.0.11:35599                      0.0.0.0:*
LISTEN         0              511                                *:9000                             *:*

容器执行命令

不进入容器并执行
docker exec  php72 bash -c 'echo a'
进入容器执行命令
docker exec -it  php72 bash

访问 test.com

18.png

备注

  • nginx 容器创建时不必非得指定 hostname, 直接【ping 容器名】可以成功
  • nginx fastcgi_pass php72:9000; // php72 是容器名,也可以设置hostname
  • 服务的配置修改后可以使用 docker restart|start|stop 重启
  • -v $PWD/www:/www 使用环境变量

安装PHP模块

安装gd库

apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ --with-freetype-dir=/usr/include/
docker-php-ext-install gd
# 查看模块 
# php -m

容器挂载卷性能低

挂在卷一致性级别

  • consistent: 默认值,完美的一致性,即主机和容器实时一致。
  • cached: 主机具有权威性。容器执行的写操作对主机是立即可见的,但是在主机上执行的写操作可能不会立即反应在容器内。
  • delegated: 容器的内容具有权威性。提供最弱保证,由容器执行的写操作可能不会立即反映在主机文件系统上,提供的性能要比其他配置好得多。

示例:

docker run \
    -v /Users/lixl.cn/project:/project:cached \
    -v /host/another-path:/mount/another-point:consistent \
    alpine command

Docker cp 容器与主机之间的数据拷贝

$ docker cp --help

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
        docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

rsync文件同步

推荐文档: https://blog.csdn.net/lhc198623/article/details/87862381

unison文件同步

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐