使用容器工具docker进行nginx服务安装
关于docker的安装在之前已经说明,今天我们就来使用docker工具安装nginx服务,并配置前端服务。nginx的安装也可以自己下载离线包进行安装,通过docker安装也是便于我们巩固之前的docker工具使用。
首先需要准备好基础的网络环境,并且安装好docker工具,接下来进行nginx的安装及基本配置操作。

已经安装了docker,配置了网络
实际操作:
1.使用docker命令拉取nginx镜像文件

当我们本地没有镜像文件时,会通过网络自动下载nginx的最新版本

此时我们通过命令查看本地镜像文件,可以看到nginx
2.使用docker run命令来启动一个nginx容器。
docker run --name nginx -p 8080:80 -d nginx:latest

此处需要注意后面加的是仓库名和tag 运行成功后出来一串字符串,无其它内容
3.接下来创建nginx所需的文件目录
mkdir -p /opt/nginx/html
mkdir -p /opt/nginx/conf
mkdir -p /opt/nginx/logs
4.将nginx容器内数据复制到创建的文件目录
docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /opt/nginx/

拷贝完成后存在文件内容
5.准备好以上文件后,我们将nginx创建的容器删除并重新组织创建容器并添加映射关系【映射指的是系统内物理路径到容器内的对应关系】

nginx容器删除完成
重新运行新命令启动nginx容器
docker run -d --name nginx -p 8080:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx:latest

此时,nginx容器正常启动。
6.通过浏览器进行访问测试并通过命令查看nginx的版本。

能够访问到nginx欢迎界面,说明部署成功。
[root@host8 nginx]# docker image inspect nginx:latest | grep -i version
"NGINX_VERSION=1.23.3",
"NJS_VERSION=0.7.9",
"DockerVersion": "20.10.23",
"NGINX_VERSION=1.23.3",
"NJS_VERSION=0.7.9",

通过命令查看到我们的nginx版本
7.通过查看我们可以看到配置文件nginx.conf来配置我们的前端服务。当然nginx服务也可作为代理服务或者提供负载均衡等功能。
[root@host8 conf]# pwd
/opt/nginx/conf
[root@host8 conf]# ls
conf.d nginx.conf