阿里云 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
相关推荐
江湖有缘19 分钟前
零基础入门:使用 Docker 快速部署 Organizr 个人主页
java·服务器·docker
礼拜天没时间.2 小时前
深入Docker架构——C/S模式解析
linux·docker·容器·架构·centos
猫头虎2 小时前
如何使用Docker部署OpenClaw汉化中文版?
运维·人工智能·docker·容器·langchain·开源·aigc
会周易的程序员2 小时前
openplc runtimev4 Docker 部署
运维·c++·物联网·docker·容器·软件工程·iot
小Pawn爷3 小时前
1.Docker基础
运维·docker·容器
chinesegf3 小时前
清理docker残留镜像images
运维·docker·容器
小Pawn爷3 小时前
2.Docker的存储
运维·docker·容器
礼拜天没时间.4 小时前
自定义镜像制作——从Dockerfile到镜像
linux·docker·容器·centos·bash
bugcome_com4 小时前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
luffy54594 小时前
windows下通过docker-desktop创建redis实例
windows·redis·docker·容器