docker安装nginx,挂载配置文件到主机目录

1 docker安装nginx

1.1 下载最新nginx镜像并启动

下面命令的意思是从镜像仓库拉取最新的nginx镜像,并启动一个nginx容器,这个容器的名字叫mynginx,并把容器内部的80端口映射到主机的8080端口。

docker run --name mynginx -d -p 8080:80 nginx

1.2 查看容器是否正常启动

通过docker ps查看容器运行状态

 docker ps 

1.3 进到容器内部查看内部结构

以交互式的方法进到容器内部,查询容器的内部结构

docker exec -it mynginx /bin/bash

1.4 修改nginx的页面内容

通过下面操作讲index.html修改为this is my nginx,访问页面发现修改生效

root@b9a3191eef22:/# cd usr/share/nginx/html/
root@b9a3191eef22:/usr/share/nginx/html# echo "this is my nginx" >index.html

1.5 下面是操作过程

[root@iZm5e8xpme70yxqoa4zoydZ html]# docker run --name mynginx -d -p 8080:80 nginx
b9a3191eef22a778a71bbf4029a5e4092c5287e0e0e2ac9a4b04de03f9f879ce
[root@iZm5e8xpme70yxqoa4zoydZ html]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b9a3191eef22        nginx               "/docker-entrypoint.…"   18 seconds ago      Up 17 seconds       0.0.0.0:8080->80/tcp                mynginx
[root@iZm5e8xpme70yxqoa4zoydZ html]# docker exec -it mynginx /bin/bash
root@b9a3191eef22:/# ls -lrt
total 80
dr-xr-xr-x  12 root root    0 Mar 22  2021 sys
drwxr-xr-x   2 root root 4096 Oct  3 09:00 home
drwxr-xr-x   2 root root 4096 Oct  3 09:00 boot
drwxr-xr-x   1 root root 4096 Oct 11 00:00 var
drwxr-xr-x   1 root root 4096 Oct 11 00:00 usr
drwxr-xr-x   2 root root 4096 Oct 11 00:00 srv
drwxr-xr-x   2 root root 4096 Oct 11 00:00 sbin
drwx------   2 root root 4096 Oct 11 00:00 root
drwxr-xr-x   2 root root 4096 Oct 11 00:00 opt
drwxr-xr-x   2 root root 4096 Oct 11 00:00 mnt
drwxr-xr-x   2 root root 4096 Oct 11 00:00 media
drwxr-xr-x   2 root root 4096 Oct 11 00:00 lib64
drwxr-xr-x   2 root root 4096 Oct 11 00:00 bin
-rwxrwxr-x   1 root root 1202 Oct 12 02:03 docker-entrypoint.sh
drwxr-xr-x   1 root root 4096 Oct 12 02:03 lib
drwxrwxrwt   1 root root 4096 Oct 12 02:03 tmp
drwxr-xr-x   1 root root 4096 Oct 12 02:03 docker-entrypoint.d
drwxr-xr-x   1 root root 4096 Oct 29 02:45 etc
dr-xr-xr-x 101 root root    0 Oct 29 02:45 proc
drwxr-xr-x   5 root root  340 Oct 29 02:45 dev
drwxr-xr-x   1 root root 4096 Oct 29 02:45 run
root@b9a3191eef22:/# cd usr/share/nginx/html/
root@b9a3191eef22:/usr/share/nginx/html# echo "this is my nginx" >index.html
root@b9a3191eef22:/usr/share/nginx/html# exit
exit

2 docker文件挂载并修改nginx配置文件

2.1 建立目录和拷贝容器内部nginx配置文件

第一步:先通过下面命令建立目录

mkdir -p /home/app/nginx/html
mkdir -p /home/app/nginx/log

第二步:通过docker cp将启动的nginx容器中的配置文件拷贝到主机的对应目录

docker cp mynginx:/etc/nginx/nginx.conf /home/app/nginx/nginx.conf
docker cp mynginx:/etc/nginx/conf.d/ /home/app/nginx/conf.d/

 

2.2 通过挂载方式启动nginx

第一步:先通过下面停止容器

docker stop mynginx

第二步:删除容器

docker rm mynginx

第三步:通过挂载方式启动容器

docker run --name mynginx -v /home/app/nginx/html:/usr/share/nginx/html -v /home/app/nginx/conf.d:/etc/nginx/conf.d -v /home/app/nginx/log:/var/log/nginx -v /home/app/nginx/nginx.conf:/etc/nginx/nginx.conf  -d -p 8080:80 nginx

第四步:进到主机目录修改index.html为<h1>this is big message</h1>,访问页面发现生效

下面是具体操作方法

[root@iZm5e8xpme70yxqoa4zoydZ html]# mkdir -p /home/app/nginx/html
[root@iZm5e8xpme70yxqoa4zoydZ html]# mkdir -p /home/app/nginx/log
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# docker cp mynginx:/etc/nginx/nginx.conf /home/app/nginx/nginx.conf
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# docker cp mynginx:/etc/nginx/conf.d/ /home/app/nginx/conf.d/
[root@iZm5e8xpme70yxqoa4zoydZ conf.d]# cd /home/app/nginx/
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# ls
conf.d  html  log  nginx.conf
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Oct 29 10:56 conf.d
drwxr-xr-x 2 root root 4096 Oct 29 10:29 html
drwxr-xr-x 2 root root 4096 Oct 29 10:29 log
-rw-r--r-- 1 root root  648 Sep  7 23:38 nginx.conf
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# docker stop mynginx
mynginx
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# docker rm mynginx
mynginx
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# docker run --name mynginx -v /home/app/nginx/html:/usr/share/nginx/html -v /home/app/nginx/conf.d:/etc/nginx/conf.d -v /home/app/nginx/log:/var/log/nginx -v /home/app/nginx/nginx.conf:/etc/nginx/nginx.conf  -d -p 8080:80 nginx
c2363eaf2fff30773be934187c0e6995561d1abd96ba661c0b01b225e0da56ca
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Oct 29 10:56 conf.d
drwxr-xr-x 2 root root 4096 Oct 29 10:29 html
drwxr-xr-x 2 root root 4096 Oct 29 11:04 log
-rw-r--r-- 1 root root  648 Sep  7 23:38 nginx.conf
[root@iZm5e8xpme70yxqoa4zoydZ nginx]# cd html/
[root@iZm5e8xpme70yxqoa4zoydZ html]# ll
total 0
[root@iZm5e8xpme70yxqoa4zoydZ html]# vim index.html

编写内容<h1>this is big message</h1>

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dream21st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值