Docker 安装 Nginx

Docker 安装 Nginx

查找Docker Hub 上的 nginx 镜像
[root@iz2ze7rz0layh39ol2qog2z ~]# docker search nginx

在这里插入图片描述

拉取官方的镜像
$ docker pull nginx
下载结束后,查看镜像
[root@iz2ze7rz0layh39ol2qog2z ~]# docker images

可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

运行Nginx
[root@iz2ze7rz0layh39ol2qog2z ~]# docker run -p 8080:80 --name nginxtest nginx

命令是启动docker中的Nginx容器,命名为nginxtest,并将宿主机(服务器)的8080端口和容器的80端口映射。

查看容器启动情况
[root@iz2ze7rz0layh39ol2qog2z ~]# docker ps

1553760140150

显示names为nginxtest的容器,说明容器已经启动成功。

通过浏览器访问

因为将Nginx容器的80端口映射到了服务器8080端口上了,所以访问服务器的8080端口

1553760332700

说明Nginx也启动成功了。

查看容器Nginx配置

在容器中开启一个交互模式的终端,这样就可以查看容器中的文件

[root@iz2ze7rz0layh39ol2qog2z ~]#  docker exec -it 4fdb96249c92 /bin/bash
或者
[root@iz2ze7rz0layh39ol2qog2z ~]#  docker exec -it nginxtest /bin/bash

4fdb96249c92 --容器的CONTAINER ID (通过docker ps查询)

nginxtest – 容器的别名

1553761213697

如上图所示,可以通过命令查看容器的文件。

查看nginx.conf配置文件目录

root@4fdb96249c92:/# nginx -t

)

查看一下Nginx目录结构

1553761546695

我安装的这个版本目录结构和网上的教程不太一样,导致映射配置文件后,怎么配置都无法生效。nginx.conf并没有在etc/nginx/conf目录下。

主机中的nginx.conf挂载到容器的nginx.conf

将服务器的配置文件挂载到容器中,这样我们修改配置文件会方便一些。

退出nginx容器,将容器中的nginx.conf先拷贝到宿主机中:

root@4fdb96249c92:/etc/nginx# exit
exit
[root@iz2ze7rz0layh39ol2qog2z ~]# docker cp nginxtest:/etc/nginx/nginx.conf /data/nginx/conf/backnginx.conf   
将容器中/etc/nginx/nginx.conf文件 复制到宿主机的/data/nginx/conf目录下,并命名为backnginx.conf

1553764189366

停止nginx容器,删除这个测试容器。重新创建一个容器,并进行配置文件映射

[root@iz2ze7rz0layh39ol2qog2z ~]# docker stop nginxtest

[root@iz2ze7rz0layh39ol2qog2z ~]# docker rm nginxtest

1553764951357

docker run  --name nginx -d -p 80:80 
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
-v /data/nginx/logs:/var/log/nginx  
-v /data/nginx/html:/usr/share/nginx/html 
nginx

这样就可以将配置文件、log、静态页面映射到宿主机中。需要修改或者查看直接在宿主机中修改或者查看就可以了。需要注意的是,配置文件虽然映射到宿主机中,但是如需配置路径,还需配置成容器中的路径

其他命令
修改配置文件重启nginx容器
docker exec -it nginx service nginx reload

查询docker中nginx容器的日志的前10行
docker logs --tail="10" nginx

docker容器中安装vim(如果下载不下来,需要配置下国内镜像)
apt-get update
apt-get install vim
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值