macOs安装docker且在docker上部署nginx+php
一 环境
- 系统:macOS Sonoma 14.6
- 芯片:Apple M3
- docker 版本:27.2.0
二 软件安装
2.1 docker下载:
Get Started | Docker进入官网,如图位置,点击mac版本的docker下载.
根据你电脑芯片类型来选择下载的版本
2.2安装
我们打开Docker
应用程序后,会有一些选择配置,我们按照如下配置即可。
这里我们选择Accept
选择默认配置就行,Docker
会自动设置一些大多数开发人员必要的配置。
这里可以选择直接跳过
出现以上的图标即可
到这里并没有正真的完成
2.3 版本查询以及问题处理
docker --version
Docker version 27.2.0, build 3ab4256
**问题一 :**如果输入这样命令出现以下情况,那么你就要去解决这些问题
docker --version
zsh:command not found:docker
那么应该怎么处理
那么你就要
cat ~/.zshrc
看看你的环境变量文件里面有没有docker
如果红色框内没有,那么依次执行
echo 'export PATH=/Applications/Docker.app/Contents/Resources/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
三 部署nginx和php
3.1 安装nginx
sudo docker pull nginx
问题二
Error response from daemon: Get “https://registry-1.docker.io/v2/”: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
为啥出现以上问题,因为没有配置国内镜像所以很容易导致链接失败
所以现在要配置国内镜像
进入该目录
然后找到daemon.json
进入改文件,然后将圈出来的编辑放入这个文件
"registry-mirrors": [
"https://docker.211678.top",
"https://docker.1panel.live",
"https://hub.rat.dev",
"https://docker.m.daocloud.io",
"https://do.nark.eu.org",
"https://dockerpull.com",
"https://dockerproxy.cn",
"https://docker.awsl9527.cn"
]
添加完后重启docker软件
命名、指定端口并运行nginx
sudo docker run --name mynginx -p 8080:80 -d nginx
–name mynginx 指定当前容器名称为 mynginx
-p 8080:80 将容器的 80 端口映射到主机的 8080 端口
-v ~/project/www:/usr/share/nginx/html 将主机的 ~/project/www 目录挂载到容器的 /www
-v ~/project/nginx/conf.d:/etc/nginx/conf.d 将主机的 ~/project/nginx/conf.d 目录挂载到容器的 /etc/nginx/conf.d
–link myphp:php 将 myphp 容器的网络并入 nginx 容器,实现容器间的通信
如果想将docker内的文件与docker外的文件相关关联
那么你可以在本地文件夹中创建对应文件
mkdir -p ~/project/nginx/www ~/project/nginx/logs ~/project/nginx/conf
www是项目路经
logs是nginx错误日志
conf 是nginx配置文件
可以看见nginx正常运行
接下来复制docker容器终端配置文件到宿主中
输入(ce1e83caf65f这个在上图查看安装中可见到)
docker cp ce1e83caf65f:/etc/nginx/nginx.conf ~/project/nginx/conf
可以查看一下原配置信息
接下来再运行一个新的(删除原来创建的nginx),前面是测试(开一个新的需要修改端口号和名字),输入如下:
docker run -d -p 8082:80 --name mynginx2 -v ~/project/nginx/www:/usr/share/nginx/html -v ~/project/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/project/nginx/logs:/var/log/nginx nginx
进入www创建
cd ~/project/nginx/www
vim index.html
#写一个hello world
<DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>my test docker</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
在网页上输入http://localhost:8082/index.html,就能看到hello Wrold
3.2 安装php
输入命令拉取PHP镜像
sudo docker pull php:8.0-fpm
命名并挂在
sudo docker run --name myphp -v ~/project/nginx/www:/www -d php:8.0-fpm
配置conf文件
mkdir ~/project/nginx/conf/conf.d
vim ~/project/nginx/conf/conf.d/code-php.conf
编辑写入配置:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
include fastcgi_params;
}
}
接下来可以把前面的nginx停掉,重新运行一个并把PHP接起来
docker run --name mynginx-php -p 8081:80 -d -v ~/project/nginx/www:/usr/share/nginx/html:ro -v ~/project/nginx/conf/conf.d:/etc/nginx/conf.d:ro --link myphp:php nginx
接下来编写一个index.php
<?php
phpinfo();
?>
在网址输入http://localhost:8081/index.php
我绑定的是7.4,所以显示的是7.4