阿里云 CentOS 7 使用 docker 安装 Nginx

一、步骤

1.拉取镜像

这里指定版本1.18.0,不指定默认下载最新.

powershell 复制代码
拉取命令: docker pull nginx:1.18.0
shell 复制代码
执行结果:
1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete 
0b20d28b5eb3: Pull complete 
1576642c9776: Pull complete 
c12a848bad84: Pull complete 
03f221d9cf00: Pull complete 
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
docker.io/library/nginx:1.18.0

检查镜像:docker images | grep nginx
nginx                   1.18.0    c2c45d506085   4 years ago     133MB

2.创建挂载目录

当容器被删除时,并不希望已生成的数据丢失.所以这些数据需要保存到服务器真实磁盘上.就需要用到了挂载技术.

powershell 复制代码
磁盘创建挂载目录: mkdir -p /data/nginx/{html,logs,conf,conf.d,ssl}

3.阿里云配置80端口安全组

4.试启动并检查

powershell 复制代码
启动命令: docker run --name nginx -d -p 80:80 nginx:1.18.0

检查进程: docker ps 
powershell 复制代码
复制容器文件到磁盘目录:
docker cp nginx:/usr/share/nginx/html /data/nginx 
docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/ 
docker cp nginx:/etc/nginx/conf.d /data/nginx

5.移除当前启动的nginx容器

powershell 复制代码
docker stop nginx
docker rm nginx

检查: docker ps 

5.挂载正式启动

powershell 复制代码
docker run --name nginx -p 80:80 \
    -v /data/nginx/html:/usr/share/nginx/html \
    -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf/ \
    -v /data/nginx/conf.d:/etc/nginx/conf.d/ \
    -v /data/nginx/logs:/var/log/nginx \
    -v /data/nginx/ssl:/etc/nginx/ssl \
    -d --restart=always nginx:1.18.0
    
tip:如果想改端口可以修改conf.d目录下config修改  
如果启动不成功可以通过 docker logs ID(run语句后会返回一个随机串)  追踪日期

总结

安装完成

其他:

powershell 复制代码
删除镜像: docker rmi nginx:1.18.0
# 查看运行中容器
docker ps
# 查看所有容器包括停止的
docker ps -a
# 只显示容器id
docker ps -q

docker cp
# 从容器复制到磁盘
docker cp inginx:/etc/nginx/nginx.conf ./nginx.conf
# 从磁盘复制到容器
docker cp ./config.txt inginx:/etc/
# 复制目录
cp -r /usr/local/nginx/html /data/nginx/html

docker logs a5602defd2d03aa30b0c561966ade97ead511cd1068cf0ce5c17d8715e21e250
相关推荐
Zhu75818 分钟前
【配置优化】docker29及以上版本配置优化。
docker
小谦笔记30 分钟前
阿里云,开始硬了
阿里云·云计算
阿提说说32 分钟前
阿里云大模型ACP认证考试测试题
阿里云·云计算·acp认证
成为你的宁宁40 分钟前
【Prometheus监控Nginx/Mysql/Redis/Docker/Rabbitmq】
mysql·nginx·prometheus
土星云SaturnCloud44 分钟前
土星云AI边缘计算-算法运行环境搭建:Docker部署全流程实操
服务器·人工智能·docker·ai·边缘计算
阿里-于怀1 小时前
告别 Ingress Nginx:云原生 API 网关 Gateway API 使用指引
nginx·云原生·gateway
難釋懷1 小时前
Nginx应用场景
运维·nginx
持梦远方1 小时前
Nginx 静态资源挂载与前端部署实战笔记
linux·前端·笔记·nginx
fanzhonghong1 小时前
javaWeb后端开发之Linux项目部署3和Docker部署1
linux·服务器·前端·docker
石小千1 小时前
Docker-排查占用磁盘空间大问题
运维·docker·容器