赞
踩
创建目录dockerfile ,包含php,nginx,redis 对应的dockerfile文件,mysql是直接安装在电脑上。
- FROM nginx:latest
-
- # 镜像信息
- LABEL authors="wang"
-
- # 设置工作目录
- WORKDIR /var/wwwroots
-
- # 端口
- EXPOSE 80
-
- CMD ["nginx", "-g", "daemon off;"]
运行dockerfile,这里是在phpstorm中编辑运行的,下面是启动配置页面。
- FROM redis:5.0
- # 镜像信息
- LABEL authors="wang"
-
- # 对外暴露Redis默认端口6379
- EXPOSE 6379
-
- # 启动Redis服务器
- CMD [ "redis-server", "/etc/redis/redis.conf" ]
- FROM php:7.4-fpm
-
- # 镜像信息
- LABEL authors = "wang"
-
- # 设置env配置
-
- # 设置时区
- ENV TZ = Asia/Shanghai
-
- # 更换为阿里云镜像,更新软件包列表
- RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
- sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
- apt-get update && \
- apt-get upgrade -y
-
- # 安装依赖
- RUN docker-php-ext-install pdo_mysql mysqli pdo
- RUN docker-php-ext-install tokenizer
- RUN docker-php-ext-install json
- RUN docker-php-ext-install bcmath
- RUN docker-php-ext-install fileinfo
- RUN docker-php-ext-install pcntl
- RUN docker-php-ext-install sockets
-
-
- # 安装zip扩展 需要放到phpredis之前
- RUN apt-get install -y zip unzip libzip-dev && \
- docker-php-ext-install zip
-
- # 安装phpredis扩展
- RUN apt-get install -y git && \
- git clone https://github.com/phpredis/phpredis.git /usr/src/php/ext/phpredis && \
- docker-php-ext-configure phpredis && \
- docker-php-ext-install phpredis && \
- apt-get purge -y --auto-remove git && \
- rm -rf /usr/src/php/ext/phpredis
-
- # 安装XML扩展
- RUN apt-get install -y libxml2 libxml2-dev && \
- docker-php-ext-install xml
-
- # 安装intl扩展
- RUN apt-get install -y libicu-dev \
- && docker-php-ext-install intl
-
- # 安装cURL扩展
- RUN apt-get install -y libcurl4-openssl-dev && \
- docker-php-ext-install curl
-
- # 安装gd库
- RUN apt-get install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev && \
- docker-php-ext-configure gd --with-freetype --with-jpeg && \
- docker-php-ext-install gd
-
- # 清理缓存
- RUN apt-get clean \
- && rm -rf /var/lib/apt/lists/*
-
- # 安装composer
- RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
-
- # 设置工作目录
- WORKDIR /var/wwwroots
-
- # 端口
- EXPOSE 9000
-
- # 设置容器启动时执行的命令
- CMD ["php-fpm"]
-
-
-
注意PHP这里,如果在运行结果后 php-fpm
没有运行起来,提示php-fpm
找不到,那就需要提前执行 docker pull php:7.4-fpm
不知道什么毛病,刚好本人遇到了这样的问题,还白白浪费了一下午的时间。
到了这里,如果上面都运行没有问题,那就需要设置nginx配置了。注意上面挂载的宿主机上nginx的配置目录 /Users/wang/Documents/docker/nginx/conf
在该目录下面,创建default.conf
和 proxy-php
文件,
注意文件中的目录是在docker中的目录,而不是宿主机上的目录,在宿主机上增加配置会自动同步到docker中,因为上面进行了目录挂载,会自动同步。
default.conf内容如下:
- server {
- listen 80;
- server_name b2c-api.wangqy.cn;
- root /var/wwwroots/yunyou/mall/b2c/b2c-api/public;
-
-
- include /etc/nginx/conf.d/proxy-php;
- }
-
- server {
- listen 80;
- server_name xinchenghui.wangqy.cn;
- root /var/wwwroots/ruijie/xinchenghui/xinchenghui-api/public;
-
- include /etc/nginx/conf.d/proxy-php;
- }
proxy-php 内容如下:
-
- location ~* (runtime|application)/{
- return 403;
- }
-
- location ~ \.php(.*)$ {
- fastcgi_pass 172.17.0.4:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
-
- location / {
- index index.html index.htm index.php;
- if (!-e $request_filename) {
- rewrite ^(.*)$ /index.php?s=/$1 last;
- break;
- }
- }
-
然后在/etc/hosts 文件中 增加 虚拟域名配置
127.0.0.1 b2c-api.wangqy.cn
另外在代码中连接数据库 和redis 不能直接使用127.0.0.1
直接连接 而应该使用 host.docker.internal
进行连接
注意在proxy-php文件中的 fastcgi_pass 172.17.0.4:9000;
其中的172.17.0.4 是docker中的php的桥接网络IP地址,可以在phpstorm工具服务界面中查看,也可以通过命令 docker inspect 容器名
查看
查看指定容器的网络信息, php74 是容器名
docker inspect php74
-
- [
- {
- "Id": "105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d",
- "Created": "2024-05-14T05:15:24.827276542Z",
- "Path": "docker-php-entrypoint",
- "Args": [
- "php-fpm"
- ],
- "State": {
- "Status": "running",
- "Running": true,
- "Paused": false,
- "Restarting": false,
- "OOMKilled": false,
- "Dead": false,
- "Pid": 94294,
- "ExitCode": 0,
- "Error": "",
- "StartedAt": "2024-05-14T05:15:25.132710792Z",
- "FinishedAt": "0001-01-01T00:00:00Z"
- },
- "Image": "sha256:9b213ca7c7ce743d98d7f0c87e9489dc1768ba86068aebd2bc07ffc23bb65a1c",
- "ResolvConfPath": "/var/lib/docker/containers/105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d/resolv.conf",
- "HostnamePath": "/var/lib/docker/containers/105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d/hostname",
- "HostsPath": "/var/lib/docker/containers/105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d/hosts",
- "LogPath": "/var/lib/docker/containers/105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d/105034676d065f4158e106c5c11e49fddfac643230acb536d06bde44424fa75d-json.log",
- "Name": "/php74",
- "RestartCount": 0,
- "Driver": "overlay2",
- "Platform": "linux",
- "MountLabel": "",
- "ProcessLabel": "",
- "AppArmorProfile": "",
- "ExecIDs": null,
- "HostConfig": {
- "Binds": [
- "/Users/wang/Documents/docker/www:/var/wwwroots:rw",
- "/Users/wang/Documents/docker/php74/conf:/usr/local/etc/php74/conf.d:rw"
- ],
- "ContainerIDFile": "",
- "LogConfig": {
- "Type": "json-file",
- "Config": {}
- },
- "NetworkMode": "default",
- "PortBindings": {
- "9000/tcp": [
- {
- "HostIp": "0.0.0.0",
- "HostPort": "9000"
- }
- ]
- },
- "RestartPolicy": {
- "Name": "always",
- "MaximumRetryCount": 0
- },
- "AutoRemove": false,
- "VolumeDriver": "",
- "VolumesFrom": null,
- "CapAdd": null,
- "CapDrop": null,
- "CgroupnsMode": "private",
- "Dns": null,
- "DnsOptions": null,
- "DnsSearch": null,
- "ExtraHosts": null,
- "GroupAdd": null,
- "IpcMode": "private",
- "Cgroup": "",
- "Links": null,
- "OomScoreAdj": 0,
- "PidMode": "",
- "Privileged": true,
- "PublishAllPorts": false,
- "ReadonlyRootfs": false,
- "SecurityOpt": [
- "label=disable"
- ],
- "UTSMode": "",
- "UsernsMode": "",
- "ShmSize": 67108864,
- "Runtime": "runc",
- "ConsoleSize": [
- 0,
- 0
- ],
- "Isolation": "",
- "CpuShares": 0,
- "Memory": 0,
- "NanoCpus": 0,
- "CgroupParent": "",
- "BlkioWeight": 0,
- "BlkioWeightDevice": null,
- "BlkioDeviceReadBps": null,
- "BlkioDeviceWriteBps": null,
- "BlkioDeviceReadIOps": null,
- "BlkioDeviceWriteIOps": null,
- "CpuPeriod": 0,
- "CpuQuota": 0,
- "CpuRealtimePeriod": 0,
- "CpuRealtimeRuntime": 0,
- "CpusetCpus": "",
- "CpusetMems": "",
- "Devices": null,
- "DeviceCgroupRules": null,
- "DeviceRequests": null,
- "KernelMemory": 0,
- "KernelMemoryTCP": 0,
- "MemoryReservation": 0,
- "MemorySwap": 0,
- "MemorySwappiness": null,
- "OomKillDisable": null,
- "PidsLimit": null,
- "Ulimits": null,
- "CpuCount": 0,
- "CpuPercent": 0,
- "IOMaximumIOps": 0,
- "IOMaximumBandwidth": 0,
- "MaskedPaths": null,
- "ReadonlyPaths": null
- },
- "GraphDriver": {
- "Data": {
- "LowerDir": "/var/lib/docker/overlay2/2e6021b6700b5027f674ebd09a1edba03e8eb1206841345fff352dce7e2d8561-init/diff:/var/lib/docker/overlay2/yc4ug25aqz1cxco8ljjk2howv/diff:/var/lib/docker/overlay2/5ssuvx29v9reyu58lmmnv9d19/diff:/var/lib/docker/overlay2/0u6klhv7t5mgd6qbl4pi1i1t4/diff:/var/lib/docker/overlay2/e2y9yoonuksy9o4wzzcnoidr6/diff:/var/lib/docker/overlay2/vkqhte8cl4thhyrzlyrlzgmdw/diff:/var/lib/docker/overlay2/xdhfg9zowkjxkjhoze0cr8s63/diff:/var/lib/docker/overlay2/bfn5tkz66cgboswqmaf711sqj/diff:/var/lib/docker/overlay2/l28kf5u8v06mwsi32rtb7vp3k/diff:/var/lib/docker/overlay2/iu71k88gwuwwg12s2rmphc6uc/diff:/var/lib/docker/overlay2/uxnkhprr9yerpqftlkag751ph/diff:/var/lib/docker/overlay2/yigemfya0uanj35fibkqag7wb/diff:/var/lib/docker/overlay2/s59silndov17qc3qy5e1tvd7t/diff:/var/lib/docker/overlay2/bcdoudyi68o51gyeyw7i441dx/diff:/var/lib/docker/overlay2/c18k02nievkwwlcloaps55ifl/diff:/var/lib/docker/overlay2/w70okq84twvkhgdsx6slfidzm/diff:/var/lib/docker/overlay2/kaezxrhfapuigi9zhrmqmaew8/diff:/var/lib/docker/overlay2/vn8npz62txyaolhxg4yk4zkak/diff:/var/lib/docker/overlay2/v3v0m6jgyinwgh8imnu7b2hfn/diff:/var/lib/docker/overlay2/mqclgluku95ekqx43fhi1vnul/diff:/var/lib/docker/overlay2/69xykaz6meum0tstmd6qoaff7/diff:/var/lib/docker/overlay2/lliq7mxzq2444wrkdh20ta29p/diff:/var/lib/docker/overlay2/bpuhj7itnqmw67muxtnbocvfh/diff:/var/lib/docker/overlay2/9roa7lv5dm7fsbmaui5yu12yr/diff:/var/lib/docker/overlay2/sze10ara19f8iyiy8jvjgqys4/diff:/var/lib/docker/overlay2/dtuk80o444nup960egqmdcprh/diff:/var/lib/docker/overlay2/4k788ok9nphm5zq2x3gcaexyr/diff:/var/lib/docker/overlay2/3ef3g1yw0agni013v8ma8isdn/diff:/var/lib/docker/overlay2/is55uyj5lyq4tnu10jnrvn395/diff:/var/lib/docker/overlay2/s59idj0mtg2awe6rk8fkuigh0/diff:/var/lib/docker/overlay2/tesrvbmlo78yv5r5lnn3ofzqh/diff:/var/lib/docker/overlay2/hznt0bbxm5cnvc7geagae8un2/diff:/var/lib/docker/overlay2/tfwujletzwm5wsplrmh8gp9bb/diff:/var/lib/docker/overlay2/47gy563ypjqrio2ljgrd67195/diff:/var/lib/docker/overlay2/s2wj9voctpitxdb2zmkdhzxom/diff:/var/lib/docker/overlay2/397bo6iyybu183rzki0mqr9kb/diff:/var/lib/docker/overlay2/naqvgbfi4mbpg4flqp0wrnwao/diff:/var/lib/docker/overlay2/3cp5kfp6sasgfn1lkegb1qxm4/diff:/var/lib/docker/overlay2/xq7jgcumen1dfslpu8ctbu0u0/diff:/var/lib/docker/overlay2/g8sie7kwn4gz454qctc8mh1k3/diff:/var/lib/docker/overlay2/i2rxot0aitr5273fx73xz5rk1/diff:/var/lib/docker/overlay2/4ms8ga29qmo7ogpjkh6nmq6as/diff:/var/lib/docker/overlay2/ovh2ztqbvjs55gw927aer9f27/diff:/var/lib/docker/overlay2/pna9448azp8aw1kj5nlexljx9/diff:/var/lib/docker/overlay2/x17pdbze0q6n5t8ssgr3l1r42/diff:/var/lib/docker/overlay2/wpm8o60q2d0q0b44f4wzzr5h6/diff:/var/lib/docker/overlay2/kc3ipasux65lktpwvincmg9t2/diff:/var/lib/docker/overlay2/boqxf8c7lavolm9zn02f6ng4c/diff:/var/lib/docker/overlay2/pdrlroke92uldelntbbtr652j/diff:/var/lib/docker/overlay2/agt2lix71qe2awu5ccllyam6a/diff:/var/lib/docker/overlay2/xu3qlcbhe2xuru01twum1jm2l/diff:/var/lib/docker/overlay2/osa89hlr5d4ppt68e7m2ypi9k/diff:/var/lib/docker/overlay2/v6u7hifglxaog81ubf9arc1y7/diff:/var/lib/docker/overlay2/gj2s2ljg0lf53q4mycpdpcbp9/diff:/var/lib/docker/overlay2/orwdfn8iovuwblib4s2iyef44/diff:/var/lib/docker/overlay2/jptvz5dwrnv87f959o929q7ex/diff:/var/lib/docker/overlay2/hmydv1v4k9odt3zhu4xyne57v/diff:/var/lib/docker/overlay2/rt2liuuzjybuhrl028zdfn7ax/diff:/var/lib/docker/overlay2/njpmvcijjchwoivr4au5d1nf5/diff:/var/lib/docker/overlay2/vpmkajrcuzhsk5yq36xqu7c24/diff:/var/lib/docker/overlay2/q6nz5du3f7hei14il82pem9i7/diff:/var/lib/docker/overlay2/x34w2htjom6w0xd68hsfhaniw/diff:/var/lib/docker/overlay2/j09r89jnwwakdti57znyzbxdc/diff:/var/lib/docker/overlay2/iq1hhuz484luccirszrv14lgz/diff:/var/lib/docker/overlay2/uxkr7l3067d3nayy3cfjzzwgc/diff:/var/lib/docker/overlay2/008df4prlx2wmfn5w3sujxefe/diff:/var/lib/docker/overlay2/c5quspq2q3mn8s3ppyc96wa79/diff:/var/lib/docker/overlay2/ahrhnwwe8i9res3qekq8gsglj/diff:/var/lib/docker/overlay2/j9nmzzdyfyx0537rd9ljr2gvp/diff:/var/lib/docker/overlay2/t4xzm5jnhhfyyodim1b4o2bt4/diff:/var/lib/docker/overlay2/ndma4875lf94v9ej3qhdu6ym6/diff:/var/lib/docker/overlay2/xgf3vhth592fw9xkfxrzkiab2/diff:/var/lib/docker/overlay2/rt5t9szt01ir4mhtumo2aq48l/diff:/var/lib/docker/overlay2/lnyvfvsnss00vyd57zevkz1nj/diff:/var/lib/docker/overlay2/xxcy3fh5y207qv2pq4gr0h2rg/diff:/var/lib/docker/overlay2/cuqvkr2qogc455vsqkqsrti0z/diff:/var/lib/docker/overlay2/wbco5ie9p31t65thcyxtqfksf/diff:/var/lib/docker/overlay2/y4t68332y0hrqxyo6c549fdhj/diff:/var/lib/docker/overlay2/qcjlq8pxoc6bjy72md4cml48c/diff:/var/lib/docker/overlay2/pgzggv5aell2ncduwltj9lslz/diff:/var/lib/docker/overlay2/a4pd7vwkxl5q6muukvc7z0y2j/diff:/var/lib/docker/overlay2/33baffrld39tff7oy2dsoctf5/diff:/var/lib/docker/overlay2/8mamvb79qov04aqtwl1r65enf/diff:/var/lib/docker/overlay2/iqf3l6n20g1vngs6ub9m8ynwd/diff:/var/lib/docker/overlay2/k0kx6ytyk9usogyjm2ovetfxd/diff:/var/lib/docker/overlay2/999460e93cacece01a40a39dedbad1f5d08597edee83591134bcd842dcc2ef58/diff:/var/lib/docker/overlay2/029ac59704e8abb37fcc3a8ddde791e81b482034e735d6a705b12547c66a31c9/diff:/var/lib/docker/overlay2/47f657978550f7190ba3cace33d5b71a21e6888c248f0dbc24fd4a7054433ffd/diff:/var/lib/docker/overlay2/56a0b058d8bf05ff75394d583f77f6bfefe5ad1d80ca2e09e2d60643c7d18237/diff:/var/lib/docker/overlay2/aaf80e5c6396a849f0b5fabfb4b7d240b14e7ed573fa5961ebb4fbddc632e73e/diff:/var/lib/docker/overlay2/6c9d0957e52ab481a42eaa677d1e955032e325628ce8f1a1ce85ed8d7123cc60/diff:/var/lib/docker/overlay2/9b007b91fcb0652dd96339f4261062537309ad69430ed2637d058d67ca146171/diff:/var/lib/docker/overlay2/b3e201c1ac95f87acf3e2a01b176c3e1c10bbde657ba1d6b85a050050b2bcbc8/diff:/var/lib/docker/overlay2/f032b7f0c6393b15fe444e4a181881a7cea296a0360590e0e644072f731f588f/diff:/var/lib/docker/overlay2/80aeaf97528f8be33c00101a22b2bf8b6d996559f00d05fa5ab8db26c2be5098/diff",
- "MergedDir": "/var/lib/docker/overlay2/2e6021b6700b5027f674ebd09a1edba03e8eb1206841345fff352dce7e2d8561/merged",
- "UpperDir": "/var/lib/docker/overlay2/2e6021b6700b5027f674ebd09a1edba03e8eb1206841345fff352dce7e2d8561/diff",
- "WorkDir": "/var/lib/docker/overlay2/2e6021b6700b5027f674ebd09a1edba03e8eb1206841345fff352dce7e2d8561/work"
- },
- "Name": "overlay2"
- },
- "Mounts": [
- {
- "Type": "bind",
- "Source": "/Users/wang/Documents/docker/www",
- "Destination": "/var/wwwroots",
- "Mode": "rw",
- "RW": true,
- "Propagation": "rprivate"
- },
- {
- "Type": "bind",
- "Source": "/Users/wang/Documents/docker/php74/conf",
- "Destination": "/usr/local/etc/php74/conf.d",
- "Mode": "rw",
- "RW": true,
- "Propagation": "rprivate"
- }
- ],
- "Config": {
- "Hostname": "105034676d06",
- "Domainname": "",
- "User": "",
- "AttachStdin": false,
- "AttachStdout": false,
- "AttachStderr": false,
- "ExposedPorts": {
- "9000/tcp": {}
- },
- "Tty": true,
- "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_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=42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312",
- "PHP_VERSION=7.4.33",
- "PHP_URL=https://www.php.net/distributions/php-7.4.33.tar.xz",
- "PHP_ASC_URL=https://www.php.net/distributions/php-7.4.33.tar.xz.asc",
- "PHP_SHA256=924846abf93bc613815c55dd3f5809377813ac62a9ec4eb3778675b82a27b927",
- "TZ== Asia/Shanghai"
- ],
- "Cmd": [
- "php-fpm"
- ],
- "Image": "9b213ca7c7ce",
- "Volumes": null,
- "WorkingDir": "/var/wwwroots",
- "Entrypoint": [
- "docker-php-entrypoint"
- ],
- "OnBuild": null,
- "Labels": {
- "authors": "= wang"
- },
- "StopSignal": "SIGQUIT"
- },
- "NetworkSettings": {
- "Bridge": "",
- "SandboxID": "02e2fd40cc1ac1e6d30f298e021803315dc1f75d34d7eadb80f2c884ebe0679b",
- "HairpinMode": false,
- "LinkLocalIPv6Address": "",
- "LinkLocalIPv6PrefixLen": 0,
- "Ports": {
- "9000/tcp": [
- {
- "HostIp": "0.0.0.0",
- "HostPort": "9000"
- }
- ]
- },
- "SandboxKey": "/var/run/docker/netns/02e2fd40cc1a",
- "SecondaryIPAddresses": null,
- "SecondaryIPv6Addresses": null,
- "EndpointID": "0a60a4bf108d37a3b2560fef982ded1688b88269153b5b8bd0cfaa0e44850fa3",
- "Gateway": "172.17.0.1",
- "GlobalIPv6Address": "",
- "GlobalIPv6PrefixLen": 0,
- "IPAddress": "172.17.0.4",
- "IPPrefixLen": 16,
- "IPv6Gateway": "",
- "MacAddress": "02:42:ac:11:00:04",
- "Networks": {
- "bridge": {
- "IPAMConfig": null,
- "Links": null,
- "Aliases": null,
- "NetworkID": "d57fe86e0e638fdd3f5af6016c2cd4829764c82c4654b174cfbb08a94738863b",
- "EndpointID": "0a60a4bf108d37a3b2560fef982ded1688b88269153b5b8bd0cfaa0e44850fa3",
- "Gateway": "172.17.0.1",
- "IPAddress": "172.17.0.4",
- "IPPrefixLen": 16,
- "IPv6Gateway": "",
- "GlobalIPv6Address": "",
- "GlobalIPv6PrefixLen": 0,
- "MacAddress": "02:42:ac:11:00:04",
- "DriverOpts": null
- }
- }
- }
- }
- ]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。