Docker(01) Nginx容器部署,创建 docker 容器后修改挂载目录的方法
nanshan 2024-10-09 12:51 25 浏览 0 评论
1. 搜索并下载nginx镜像
root@hongpon316:~# docker images 查看当前有哪些镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
tomcat latest fb5657adc892 20 months ago 680MB
centos latest 5d0da3dc9764 23 months ago 231MB
root@hongpon316:~# docker pull nginx 拉取镜像
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@hongpon316:~# docker images 查看当前有哪些镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
nginx latest 605c77e624dd 20 months ago 141MB
tomcat latest fb5657adc892 20 months ago 680MB
centos latest 5d0da3dc9764 23 months ago 231MB
2. 运行测试
2.1 运行并创建容器
root@hongpon316:~# docker run -d -p 3340:80 --name mynginx_01 nginx:latest
927c9bcbc0f52ca46b115491ffef3cec2240e56cc036a3218d8925159c6e1020
root@hongpon316:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
927c9bcbc0f5 nginx:latest "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 0.0.0.0:3340->80/tcp, :::3340->80/tcp mynginx_01
docker run: 这是 Docker 命令,用于创建和运行容器。
-d: 这是一个选项,表示容器应以“后台模式”(detached mode)运行,也就是在后台运行而不附加到当前终端会话。
-p 3340:80: 这是一个选项,用于将主机的端口 3340 映射到容器的端口 80。这样,可以通过访问主机的端口 3340 来访问容器中运行的应用程序。
--name mynginx_01: 这是一个选项,用于指定容器的名称为 mynginx_01。这样,可以使用这个名称来管理和操作容器。
nginx:latest: 这是要使用的 Docker 镜像的名称和标签。在这种情况下,使用 nginx 镜像的最新版本。
注意:3340端口需要在阿里云安全组中打开,才能访问!Linux主机上一般默认是3344端口开启。
2.2 测试
3. nginx的相关配置信息
root@hongpon316:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
927c9bcbc0f5 nginx:latest "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:3340->80/tcp, :::3340->80/tcp mynginx_01
d61fa338ef1f tomcat:latest "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp mytomcat_01
root@hongpon316:~# docker exec -it mynginx_01 /bin/bash
root@927c9bcbc0f5:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
docker exec: 这是 Docker 命令,用于在运行的容器中执行命令。
-it: 这是两个选项的组合,分别为 -i 和 -t。-i 选项表示要开启一个交互式会话,而 -t 选项表示要分配一个伪终端(pseudo-TTY)。这样,你可以与容器的 shell 进行交互。
mynginx_01: 这是容器的名称或容器的 ID。在这种情况下,它是 mynginx_01。
/bin/bash: 这是要在容器中执行的命令。在这里,我们使用 /bin/bash 来启动一个 Bash shell。
root@927c9bcbc0f5:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@927c9bcbc0f5:/# cd /etc/nginx
root@927c9bcbc0f5:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@927c9bcbc0f5:/etc/nginx#
3.1 whereis nginx
显示 Nginx 在容器内的位置。
/usr/sbin/nginx: 这是 Nginx 可执行文件的路径。
/usr/lib/nginx: 这是 Nginx 的库文件路径。
/etc/nginx: 这是 Nginx 的配置文件目录。
/usr/share/nginx: 这是 Nginx 的共享文件目录。
3.2 cd /etc/nginx
conf.d: 这是 Nginx 配置文件的子目录,通常用于存储虚拟主机配置等。
fastcgi_params: 这是 FastCGI 参数文件。
mime.types: 这是 MIME 类型映射文件。
modules: 这是 Nginx 模块目录,其中包含 Nginx 所需的模块文件。
nginx.conf: 这是 Nginx 的主配置文件。
scgi_params: 这是 SCGI 参数文件。
uwsgi_params: 这是 uWSGI 参数文件
4. 访问测试
5. 安装vim
我们使用Nginx往往需要编写配置文件,但是Nginx官方镜像没有安装vim,需要我们手动进行安装。使用以下命令进行安装:
apt-get update
apt-get install vim
当我们修改了配置文件,只要重新启动容器docker restart 容器id,改动就可以生效了。
如果vim终端不能复制,可以在vim界面输入:set mouse=r
二、创建 docker 容器后修改挂载目录的方法
1.查看容器的挂载路径
1.1 获取容器 mynginx_01 的挂载点信息(方法一)
docker inspect -f "{{.Mounts}}" mynginx_01
1.2 获取容器mynginx_01的详细信息(方法二)
没有挂载路径
2. 创建容器后但没有配置挂载目录的修改方法
2.1 将容器commit为新的镜像,再将新镜像重新运行的同时配置好挂载信息。
2.1.1 重新commit为新的镜像
docker commit -m="add paths" -a="Eufeo" mynginx_01 nginx_addpaths
docker commit: 基于正在运行的容器创建一个新的镜像。
-m="add paths": 提交消息,用于描述镜像的变更或添加的功能。在这个例子中,提交消息为 "add paths"。
-a="Eufeo": 作者信息,用于指定提交的作者。在这个例子中,作者为 "Eufeo"。
mynginx_01: 要提交为镜像的容器的名称或容器ID。
nginx_addpaths: 创建的新镜像的名称。
2.1.2 将新的镜像nginx_addpaths启动,并配置挂载信息
root@hongpon316:/# docker run -d --name new_mynginx -p 3345:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx_addpaths
117617031f01155158d4b2df825eeafcf792a7eb4fcb674d2449797ffb6e875c
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
docker run: 创建并运行一个新的容器。
-d: 在后台模式下运行容器。
--name new_mynginx: 将容器命名为 "new_mynginx"。
-p 3345:80: 将主机的端口 3345 映射到容器的端口 80。这样,当你访问主机的 3345 端口时,请求将被转发到容器的 80 端口。
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf: 将主机上的 /data/nginx/conf/nginx.conf 文件挂载到容器的 /etc/nginx/nginx.conf 文件。这样,容器中的 NGINX 配置文件将使用主机上的文件。
-v /data/nginx/log:/var/log/nginx: 将主机上的 /data/nginx/log 目录挂载到容器的 /var/log/nginx 目录。这样,容器中的 NGINX 日志文件将写入到主机上的目录。
-v /data/nginx/html:/usr/share/nginx/html: 将主机上的 /data/nginx/html 目录挂载到容器的 /usr/share/nginx/html 目录。这样,容器中的 NGINX 网页文件将使用主机上的文件。
2.1.3 报错:mounting "/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf"
此处有一个报错信息:
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/data/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
其原因和解决办法参考:Docker - 解决创建 nginx 容器尝试挂载 nginx.conf 文件时报错: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: - 小菠萝测试笔记 - 博客园 (cnblogs.com)
2.1.4 报错信息的根因
不支持直接挂载文件,只能挂载文件夹
想要挂载文件,必须宿主机也要有对应的同名文件
2.1.5 解决办法
1.可以先不挂载 nginx.conf
2.先从容器中复制 nginx.conf 出来
3.然后可以自行修改 nginx.conf,自定义配置项
4.创建正式使用的 nginx 容器
root@hongpon316:/# docker ps 查看当前运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
862ead87f829 nginx:latest "/docker-entrypoint.…" 36 minutes ago Up 36 minutes 0.0.0.0:3340->80/tcp, :::3340->80/tcp mynginx_01 有一个正在运行的nginx容器
root@hongpon316:/# docker exec -it mynginx_01 /bin/bash
root@862ead87f829:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@862ead87f829:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@862ead87f829:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@862ead87f829:/etc/nginx# cat nginx.conf 查看nginx.conf的配置文件
root@862ead87f829:/# read escape sequence ctrl+p+q
root@hongpon316:/# docker cp mynginx_01:/etc/nginx/nginx.conf /data/ 从 mynginx_01 容器中复制 nginx.conf 出来
Successfully copied 2.56kB to /data/
root@hongpon316:/#
然后将该nginx.conf文件复制进/data/nginx/conf
2.1.6 再次启动新的镜像,并重新挂载
root@hongpon316:/# docker run -d --name new_mynginx -p 3345:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx_addpaths 重新输入挂载命令(因为之前挂载没有成功,但是容器已经创建,因此需要使用docker ps -a删除名为nginx_addpaths的容器再输入该命令)
3d267f39cdb4bd55ca18adc0998c35bb55628baeb35b8e6279f6d6857be30f09
root@hongpon316:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d267f39cdb4 nginx_addpaths "/docker-entrypoint.…" 56 seconds ago Up 56 seconds 0.0.0.0:3345->80/tcp, :::3345->80/tcp new_mynginx
862ead87f829 nginx:latest "/docker-entrypoint.…" 13 hours ago Up 18 minutes 0.0.0.0:3340->80/tcp, :::3340->80/tcp mynginx_01
root@hongpon316:/# docker inspect -f "{{.Mounts}}" new_mynginx 查看容器new_mynginx的挂载点
[{bind /data/nginx/conf/nginx.conf /etc/nginx/nginx.conf true rprivate} {bind /data/nginx/log /var/log/nginx true rprivate} {bind /data/nginx/html /usr/share/nginx/html true rprivate}]
root@hongpon316:/#
2.1.7 查看挂载目录及其结构
root@hongpon316:/# apt-get update
Hit:1 http://mirrors.cloud.aliyuncs.com/debian bullseye InRelease
Get:2 http://mirrors.cloud.aliyuncs.com/debian-security bullseye-security InRelease [48.4 kB]
Hit:3 https://download.docker.com/linux/debian bullseye InRelease
Get:4 http://mirrors.cloud.aliyuncs.com/debian bullseye-updates InRelease [44.1 kB]
Get:5 http://mirrors.cloud.aliyuncs.com/debian bullseye-backports InRelease [49.0 kB]
Get:6 http://mirrors.cloud.aliyuncs.com/debian-security bullseye-security/main Sources [217 kB]
Get:7 http://mirrors.cloud.aliyuncs.com/debian-security bullseye-security/main amd64 Packages [246 kB]
Fetched 605 kB in 1s (930 kB/s)
Reading package lists... Done
root@hongpon316:/# apt-get install tree
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 15 not upgraded.
Need to get 49.6 kB of archives.
After this operation, 118 kB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/debian bullseye/main amd64 tree amd64 1.8.0-1+b1 [49.6 kB]
Fetched 49.6 kB in 0s (489 kB/s)
Selecting previously unselected package tree.
(Reading database ... 45414 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1+b1_amd64.deb ...
Unpacking tree (1.8.0-1+b1) ...
Setting up tree (1.8.0-1+b1) ...
Processing triggers for man-db (2.9.4-2) ...
root@hongpon316:/# tree /data
/data
├── nginx
│ ├── conf
│ │ └── nginx.conf
│ ├── html
│ └── log
│ ├── access.log
│ └── error.log
└── nginx.conf
4 directories, 4 files
root@hongpon316:/#
以上的内容,仅解决挂载的相关问题。在挂载后会【docker nginx 403 forbidden】的问题。请参考资料修正BUG。
2.1.8 优点和缺点
优点
无需停止 Docker 服务,不影响其他正在运行的容器
旧容器有的配置和数据,新容器也会有,不会造成数据或配置丢失,对新旧容器都没有任何影响
缺点
需要生成新的镜像和容器,管理镜像和容器的时间成本会上升
2.2 删除原有容器,重新创建新的容器
2.2.1 优点和缺点
优点
简单粗暴,在测试环境用的更多
缺点
如果是数据库、服务器相关的容器,创建新的容器,又得重新配置相关东西了
相关推荐
- MongoDB 从入门到实战:.NET 平台完整指南
-
一、什么是MongoDBMongoDB是一种功能强大且灵活的NoSQL数据库,适用于处理大规模的半结构化数据和高并发场景。它不依赖于固定的表结构和关系模型,而是以文档的形式存储数据,每个文档可...
- NET Framework安装失败的原因及解决方法
-
大家好我是艾西,一个做服务器租用的游戏爱好者兼网络架构系统环境问题网络工具人。在我们平时使用PC安装某些程序会出现.NETFramework缺失的提示,那么也会有很多的小伙伴搞不懂什么原因导致的,这...
- 这可是全网eNSP安装最完整,最详细的图解,没有之一(常见问题)
-
eNSP安装大纲eNSP安装详细图解篇幅较长,会分三篇更完。急需安装的朋友可以在文末获取图解文档和所需软件工具。ENSP安装常见问题和解决方案Vbox安装错误eNSP在安装的过程当中,经常会出现一...
- 如何在windows 2012安装.NET Framework3.5
-
Windowsserver2012R2,自带的是.NETFramework4.5,如果想装SQLserver2008或者SQLserver2012需要安装.ENTFramework...
- 3款国内可用的「Chrome」扩展下载网站
-
身为程序员,有几个不使用Chrome浏览器提升下编码效率呢?Chrome拥有众多丰富强大的扩展程序,今天给大家分享三个国内可用的Chrome扩展下载网站,收藏一下吧,不然下次就找不到我咯!C...
- 下载 Windows 10 应用商店程序离线包方法
-
有厂商为了图方便,会把Windows10应用商店里面的UMP应用改成EXE程序版本。例如之前「网易云音乐」UMP版本简洁清爽,获得不少用户推荐,后来官方懒得更新了,直接把UMP版本...
- 极速安装!NET Framework 3.5零距离指南!
-
.NETFramework3.5是一款由微软开发的应用程序框架,它为许多Windows应用程序提供了基础支持。它的新版本带来了许多令人兴奋的功能和改进,比如增强的XML和JSON处理能力以及强大的...
- Microsoft.NET离线运行库合集发布 2021
-
软件介绍.NET是微软具有战略意义的框架,也是装机必不可少的框架,想要一个一个安装略显繁琐,再加上很多电脑小白不知道怎么下载,不小心就下载到某某高速加载器,这个运行库极大解决了这个问题,采用微软官方....
- 缺少.net framework 3.5怎么办?(缺少.net4.5.1或以上环境)
-
很多电脑用户在玩某些程序游戏时都会遇到一个头痛的问题,弹出缺少“NETFramework3.5”的提示。微软从Windows8开始默认屏蔽了“.NET3.5”,如果用户有需要就必须选择在线安装...
- Windows11无法正常安装.net 3.5组件的解决方法
-
最近因公司部分电脑升级至Windows11之后,重新安装某些需要加载.net3.5组件的应用软件时,都提示无法完成加载或安装.net3.5而导致无法完成安装。使用离线安装包亦一样无法完成安装。一...
- 离线安装.Net Framework 3.5(离线安装.net framework 4.0)
-
前言.Net3.5已经越来越少用到了,但是偶尔还是会遇到一些老软件需要。而Win10、Win11的系统,直接在控制面板的里添加,经常会添加失败!解决方法首先需要一个系统的ISO镜像来提取sxs文件夹:...
- Jenkins 11个使用技巧,90%以上的人没用过
-
一、Performance插件兼容性问题自由风格项目中,有使用Performance插件收集构建产物,但是截至到目前最新版本(Jenkinsv2.298,Performance:v3.19),此...
- 6款Linux常用远程连接工具,你最中意哪一款?
-
点击上方头像关注我,每周上午09:00准时推送,每月不定期赠送技术书籍。本文2106字,阅读约需6分钟Hi,大家好。远程连接的实现方法有很多,概括地说有两种,一种是用系统自带的远程连接,另外一种是用...
- Linux常用远程连接工具介绍,总有一款适合你
-
作为运维或者网工最常用就是ssh远程和远程桌面工具,本文就介绍几个常用的远程连接工具,你在用哪一款呢SecureCRT介绍:我觉得这个是最好的SSH工具,没有之一。SecureCRT支持SSH,同时支...
- 终极软路由网络设置,ESXi虚拟机安装iKuai+openWrt双路由系统
-
本内容来源于@什么值得买APP,观点仅代表作者本人|作者:BigBubbleGum本文是软路由系列的第五篇,也是折腾时间最长的一篇,在ESXi下分别独立安装和使用iKuai和openWrt...
你 发表评论:
欢迎- 一周热门
-
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
[常用工具] OpenCV_contrib库在windows下编译使用指南
-
极空间如何无损移机,新Z4 Pro又有哪些升级?极空间Z4 Pro深度体验
-
Ubuntu系统Daphne + Nginx + supervisor部署Django项目
-
WindowsServer2022|配置NTP服务器的命令
-
WIN11 安装配置 linux 子系统 Ubuntu 图形界面 桌面系统
-
解决Linux终端中“-bash: nano: command not found”问题
-
UOS服务器操作系统防火墙设置(uos20关闭防火墙)
-
NBA 2K25虚拟内存不足/爆内存/内存占用100% 一文速解
-
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)