docker仓库:https://hub.docker.com/search?q=nginx
# 拉取7.4-fpm版本的php镜像 PS C:\WINDOWS\system32> docker pull php:7.4-fpm # 拉取nginx镜像 PS C:\WINDOWS\system32> docker pull nginx # 拉取mysql:5.7镜像 PS C:\WINDOWS\system32> docker pull mysql:5.7 # 拉取redis:3.2镜像 PS C:\WINDOWS\system32> docker pull redis
在windows10宿主机上创建文件夹、文件,如下所示:
E:\docker\nginx E:\docker\nginx\conf #nginx配置 E:\docker\nginx\logs #nginx日志 E:\docker\www.docker.com #项目目录
C:\Users\2023020701>docker run -d -p 80:80 --name mynginx nginx b9d72bb687d4b849842fc406120099e8a0980cf6e23e6485651a15819d8ba753
C:\Users\2023020701>docker cp mynginx:/etc/nginx/nginx.conf E:/docker/nginx/conf C:\Users\2023020701>docker cp mynginx:/etc/nginx/conf.d/default.conf E:/docker/nginx/conf/conf.d/default.conf
C:\Users\2023020701>docker stop mynginx C:\Users\2023020701>docker rm mynginx
C:\Users\2023020701>docker run -d -p 8080:80 --name mynginx -v E:/docker/www.docker.com:/usr/share/nginx/html -v E:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v E:/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v E:/docker/nginx/logs:/var/log/nginx nginx
--name 为启动的容器命名
-p 映射端口,把本地宿主机的8008端口 映射到容器的80端口,访问本地8008端口即访问容器80端口
-v 映射本地文件目录 到 容器内部目录,比如项目代码目录
--network 关联容器网络
--ip 指定容器ip
--privileged 启用桥接
-d 后台启动一个容器,使用名为为php ,tag为 8.0-fpm 的镜像,本地不存在会从官方仓库拉取
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>test docker</h1> </body> </html>
C:\Users\2023020701>docker run -d -p 9000:9000 --name php80 -v E:/docker/www.docker.com:/usr/share/nginx/html php:8.0-fpm
以上命令中:
-d表示后台运行
-p表示将宿主机端口映射到本机端口
-v表示目录挂载 将宿主机目录挂载到容器目录【冒号前面是宿主机目录,冒号后是容器内的目录】
--name 指定容器名称
后面指定创建容器使用的镜像。
新建 E:\docker\php\conf 目录
C:\Users\2023020701>docker cp php80:/usr/local/etc/php/php.ini-production E:/docker/php/conf/php.ini
C:\Users\2023020701>docker stop php80 C:\Users\2023020701>docker rm php80
C:\Users\2023020701>docker run -d -p 9000:9000 --name php80 -v E:/docker/www.docker.com:/usr/share/nginx/html -v E:/docker/php/conf/php.ini:/usr/local/etc/php/php.ini php:8.0-fpm
E:/docker/www.docker.com 新建 index.php 文件,输入内容:
<?php phpinfo();
server { listen 80; listen [::]:80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass php80:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } other1: server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php(.*)$ { root /usr/share/nginx/html; fastcgi_pass php74:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } other2: server { listen 80; server_name 192.168.0.195; location / { if (!-e $request_filename){ rewrite ^/(.*) /index.php last; } root /var/www/html/admin; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html/admin; } location ~ \.php$ { root /var/www/html; fastcgi_pass 172.18.0.3:9000; #php容器的ip地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/admin/$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
PS C:\WINDOWS\system32> docker stop mynginx && docker rm mynginx
C:\Users\2023020701>docker run -d -p 8080:80 --name mynginx -v E:/docker/www.docker.com:/usr/share/nginx/html -v E:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v E:/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v E:/docker/nginx/logs:/var/log/nginx --link php80:8.0-fpm nginx
http://127.0.0.1:8088/ 或 http://localhost:8080/index.php
也可使用docker-compose服务编排,一键启动上述php和nginx等服务容器
使用同样的端口启动新的容器
C:\Users\2023020701>docker stop php80 C:\Users\2023020701>docker stop mynginx
内容如下,修改项目路径和配置文件路径为自己的电脑文件夹路径:
E:\docker 目录下,新建文件 docker-compose.yml
#yaml version: '3' services: php: container_name: php801 image: php:8.0-fpm restart: always #networks: # - nginx_network ports: - "9000:9000" volumes: - "E:/docker/www.docker.com:/usr/share/nginx/html" - "E:/docker/php/conf/php.ini:/usr/local/etc/php/php.ini" #- "H:/home/code/docker/php81/log:/var/log/php" #networks: # nginx_network: # ipv4_address: 172.18.0.3 nginx: container_name: mynginx2 image: nginx restart: always environment: - TZ=Asia/Shanghai ports: - "8080:80" depends_on: - "php" volumes: - "E:/docker/www.docker.com:/usr/share/nginx/html" - "E:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf" - "E:/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf" - "E:/docker/nginx/logs:/var/log/nginx" #networks: # nginx_network: # ipv4_address: 172.18.0.4
修改 E:\docker\nginx\conf\conf.d 目录 default.conf 配置文件
server { listen 80; listen [::]:80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass php801:9000; #!!! 修改成 docker-compose服务编排 配置文件的 php 容器 !!! fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
在上述文件存在的目录 执行 docker-compose up -d 命令启动容器, 访问8080端口查看项目运行情况
E:\docker>docker-compose up -d [+] Running 3/3 ✔ Network docker_default Created 0.0s ✔ Container php801 Started 0.4s ✔ Container mynginx2 Started 0.7s E:\docker>
浏览器访问地址: http://localhost:8080/index.php
进入php容器, 执行命令 docker exec –it php801 /bin/bash,
执行mysqli扩展安装
C:\Users\2023020701>docker exec -it php801 /bin/bash root@cdd58490b14d:/var/www/html# docker-php-ext-install mysqli Configuring for: PHP Api Version: 20200930 Zend Module Api No: 20200930 Zend Extension Api No: 420200930 ...
新建挂载配置文件夹, E:\docker\redis\ 目录下 新建data和conf两个文件夹。
特别说明:http://download.redis.io/redis-stable/redis.conf这里下载的是最新的配置文件 具体使用配置文件还是要根据具体的redis版本进行 文章末尾是redis 6.2.6即本文章使用的配置文件
bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问 protected-mode no #默认yes,开启保护模式,限制为本地访问 daemonize no #默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败 #requirepass foobared requirepass redis123456 #redis通过配置文件设置密码
把 redis.conf 文件上传到配置的文件夹下 E:/docker/redis/conf/ .
C:\Users\2023020701>docker run --restart=always --log-opt max-size=1024m --log-opt max-file=2 -p 6379:6379 --name myredis -v E:/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v E:/docker/redis/data:/data -d redis --appendonly yes --requirepass redis123456
docker容器内缺少命令的简单解决办法
docker exec -it container_name curl
如果容器内缺少curl命令,会显示类似的错误信息:
bash: curl: command not found
这时候就需要在容器内安装curl命令了。
我们可以使用下面的命令在容器内安装curl命令:
docker exec -it container_name apt-get update
docker exec -it container_name apt-get install curl
这两个命令将在容器内更新安装源,并安装curl命令。安装完成后,我们再次执行curl命令,应该就可以正常使用了。
首先,我们需要确认容器内是否缺少npm命令。我们可以使用下面的命令在容器内执行npm命令:
docker exec -it container_name npm
如果容器内缺少npm命令,会显示类似的错误信息:
bash: npm: command not found
这时候就需要在容器内安装npm命令了。
我们可以使用下面的命令在容器内安装npm命令:
docker exec -it container_name apt-get update
docker exec -it container_name apt-get install npm
这两个命令将在容器内更新安装源,并安装npm命令。安装完成后,我们再次执行npm命令,应该就可以正常使用了。
C:\Users\2023020701>docker exec -it php80 apt-get update C:\Users\2023020701>docker exec -it php80 apt-get install curl C:\Users\2023020701>docker exec -it php80 /bin/bash root@af8440509ed7:/var/www/html# curl -L -o /tmp/redis.tar.gz https://pecl.php.net/get/redis-6.0.2.tgz root@af8440509ed7:/var/www/html# cd /tmp root@af8440509ed7:/tmp# ls redis.tar.gz root@af8440509ed7:/tmp# tar -zxvf redis.tar.gz root@af8440509ed7:/tmp# mv redis-6.0.2/ /usr/src/php/ext/ root@af8440509ed7:/tmp# cd /usr/local/bin root@af8440509ed7:/usr/local/bin# docker-php-ext-install redis-6.0.2 root@af8440509ed7:/usr/local/bin# php -m [PHP Modules] Core ctype curl date dom fileinfo filter ftp hash iconv json libxml mbstring mysqlnd openssl pcre PDO pdo_sqlite Phar posix readline redis Reflection session SimpleXML sodium SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib [Zend Modules] root@af8440509ed7:/usr/local/bin# C:\Users\2023020701> 退出容器exit 重启php容器 docker restart myphp82
E:\docker\www.docker.com 新建文件 redis.php
<?php $redis = new Redis(); $redis->connect('172.17.0.2', 6379); $redis->auth('redis123456'); echo "Connection to server sucessfully"; // 查看服务是否运行 echo "Server is running: " . $redis->ping();
1). 停止 docker-compose一键启动的容器
2). 修改 E:\docker\nginx\conf\conf.d\default.conf 改成 fastcgi_pass php80:9000;
C:\Users\2023020701> docker pull mysql:8.0
C:\Users\2023020701> docker run -d --name mysql8.0 -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /docker/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /docker/mysql/data:/var/lib/mysql -v /docker/mysql/logs:/var/log/mysql mysql:8.0 6cd67e556adf92e2e121969d1bf0bf0d99e26ddbff18febe942dac45a028969f
E:\docker\mysql>docker exec -it mysql8.0 /bin/bash bash-4.4# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.35 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> create database school; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.01 sec) mysql> mysql> alter user 'root'@'%' identified with mysql_native_password by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> CREATE USER 'ck'@'%' IDENTIFIED WITH mysql_native_password BY 'ck123456'; Query OK, 0 rows affected (0.02 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'ck'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
E:\docker\mysql>docker exec -it php80 /bin/bash root@af8440509ed7:/var/www/html# docker-php-ext-install mysqli Configuring for: PHP Api Version: 20200930 Zend Module Api No: 20200930 Zend Extension Api No: 420200930
重启 php容器,打印phpinfo()信息
8.5 navicat 远程连接
<?php $servername = "127.0.0.1:3306"; $username = "ck"; $password = "ck123456"; $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";
http://127.0.0.1:8080/mysql.php,
报 Connection failed: Connection refused
解决方案:
将 DB_HOST 中的IP,填写成我们母机的IP地址即可。
window 中查看ip
C:\Users\2023020701>ipconfig /all ... IPv4 地址 . . . . . . . . . . . . : 192.168.23.45(首选) ...
修改ip,再次访问 http://127.0.0.1:8080/mysql.php,成功!
编辑 docker-compose.yml
#yaml version: '3' services: php: container_name: php801 image: php:8.0-fpm restart: always #networks: # - nginx_network ports: - "9000:9000" volumes: - "E:/docker/www.docker.com:/usr/share/nginx/html" - "E:/docker/php/conf/php.ini:/usr/local/etc/php/php.ini" #- "H:/home/code/docker/php81/log:/var/log/php" #networks: # nginx_network: # ipv4_address: 172.18.0.3 nginx: container_name: mynginx2 image: nginx restart: always environment: - TZ=Asia/Shanghai ports: - "8081:80" depends_on: - "php" volumes: - "E:/docker/www.docker.com:/usr/share/nginx/html" - "E:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf" - "E:/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf" - "E:/docker/nginx/logs:/var/log/nginx" #networks: # nginx_network: # ipv4_address: 172.18.0.4 redis: image: redis:latest container_name: myredis2 restart: always #privileged: true #root权限 ports: - "6375:6379" volumes: - "E:/docker/redis2/conf/redis.conf:/etc/redis/redis.conf" - "E:/docker/redis2/data:/data" command: redis-server /etc/redis/redis.conf mysql: image: mysql:8.0 container_name: mysql2 #privileged: true ports: - "3307:3306" volumes: - "E:/docker/mysql2/conf/my.cnf:/etc/mysql/my.cnf" - "E:/docker/mysql2/data:/var/lib/mysql" - "E:/docker/mysql2/logs:/var/log/mysql" environment: - MYSQL_ROOT_PASSWORD=123456 - MYSQL_USER=test - MYSQL_PASS=123
E:\docker>docker-compose up -d
[+] Running 5/5
✔ Network docker_default Created 0.0s
✔ Container mysql2 Started 0.5s
✔ Container php801 Started 0.9s
✔ Container myredis2 Started 0.9s
✔ Container mynginx2 Started 1.1s
附:
Windows平台下使用Docker搭建PHP环境(Nginx+PHP+MySQL+Redis)
windows下docker安装php环境(nginx,php,mysql,redis互联)
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn