栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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为this is big message,访问页面发现生效

下面是具体操作方法

[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

编写内容this is big message

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361303.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号