马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在CentOS上利用Docker来运行Nginx是一个常见的做法,因为它提供了快速、一致的环境配置方式,而且可以很容易地扩展。下面是怎样通过Docker来运行Nginx的步骤:
1. 安装Docker
起首,确保你的CentOS体系上已经安装了Docker。如果还没有安装,你可以通过以下命令来安装Docker:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
centos 8
注如果安装报如下错误:
- Error:
- Problem 1: problem with installed package podman-1.0.5-1.gitf604175.module_el8.0.0+194+ac560166.x86_64
- - package podman-1.0.5-1.gitf604175.module_el8.0.0+194+ac560166.x86_64 requires runc, but none of the providers can be installed
- - package podman-2.2.1-7.module_el8.3.0+699+d61d9c41.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
- - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - cannot install the best candidate for the job
- - package runc-1.0.0-56.rc5.dev.git2abd837.module_el8.3.0+569+1bada2e4.x86_64 is filtered out by modular filtering
- - package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering
- Problem 2: problem with installed package buildah-1.5-3.gite94b4f9.module_el8.0.0+58+91b614e7.x86_64
- - package buildah-1.5-3.gite94b4f9.module_el8.0.0+58+91b614e7.x86_64 requires runc >= 1.0.0-26, but none of the providers can be installed
- - package buildah-1.16.7-4.module_el8.3.0+699+d61d9c41.x86_64 requires runc >= 1.0.0-26, but none of the providers can be installed
- - package docker-ce-3:20.10.6-3.el8.x86_64 requires containerd.io >= 1.4.1, but none of the providers can be installed
- - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.3-3.2.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.3-3.2.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- - cannot install the best candidate for the job
- - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.3-3.2.el8.x86_64 conflicts with runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.3-3.2.el8.x86_64 obsoletes runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-55.rc5.dev.git2abd837.module_el8.0.0+58+91b614e7.x86_64
- - package runc-1.0.0-56.rc5.dev.git2abd837.module_el8.3.0+569+1bada2e4.x86_64 is filtered out by modular filtering
- - package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering
- (try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
复制代码 必要实行如下命令, 再重新实行安装命令
- sudo yum erase podman buildah
复制代码
sudo systemctl start docker
sudo systemctl enable docker
2. 运行Nginx容器
安装完Docker后,你可以通过以下命令来运行一个Nginx容器:
- [/code] [code]docker pull nginx
复制代码 如果报错
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://hub-mirror.c.163.com",
"https://mirror.ccs.tencentyun.com",
"https://noohub.ru"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
- vim /etc/docker/daemon.json
复制代码
2.重启docker服务
重启历程
- sudo systemctl daemon-reload
复制代码 重启服务
- sudo systemctl restart docker
复制代码 然后再拉取,就成功了!
docker pull hub-mirror.c.163.com/library/nginx:latest
docker run -itd --name nginx -p 8088:80 nginx:latest
docker run --name my-nginx -p 8080:80 -d nginx
docker run --name my-nginx -p 8088:80 -d nginx
这个命令做了几件事:
- --name my-nginx:给容器命名为my-nginx。
- -p 8080:80:将容器的80端口映射到宿主机的8080端口。如许,你可以通过访问宿主机的8080端口来访问Nginx。
- -d:以后台模式运行容器。
- nginx:利用官方的Nginx镜像。
3. 验证Nginx是否运行
你可以通过访问宿主机的8080端口来验证Nginx是否正常运行:
curl http://localhost:8080
你应该会看到Nginx的默认欢迎页面。
4. 自定义Nginx配置
如果你必要自定义Nginx的配置,你可以创建一个自定义的nginx.conf文件,并将其挂载到容器中。例如:
mkdir /path/to/nginx-conf
echo 'server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } }' > /path/to/nginx-conf/nginx.conf
docker run --name my-nginx -p 8080:80 -v /path/to/nginx-conf:/etc/nginx/conf.d -d nginx
这里,-v /path/to/nginx-conf:/etc/nginx/conf.d将宿主机的/path/to/nginx-conf目录挂载到容器的/etc/nginx/conf.d目录,如许容器就会利用这个目录下的nginx.conf文件作为其配置文件。
5. 管理Nginx容器
一旦你的Nginx容器运行起来,你可以利用以下命令来管理它:
- 制止容器:docker stop my-nginx
- 启动容器:docker start my-nginx
- 重启容器:docker restart my-nginx
- 查看容器日记:docker logs my-nginx
通过这些步骤,你可以在CentOS上利用Docker轻松地运行和管理Nginx。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |