Docker(01) Nginx容器部署,创建 docker 容器后修改挂载目录的方法
nanshan 2024-10-09 12:51 30 浏览 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 优点和缺点
优点
简单粗暴,在测试环境用的更多
缺点
如果是数据库、服务器相关的容器,创建新的容器,又得重新配置相关东西了
相关推荐
- 0722-6.2.0-如何在RedHat7.2使用rpm安装CDH(无CM)
-
文档编写目的在前面的文档中,介绍了在有CM和无CM两种情况下使用rpm方式安装CDH5.10.0,本文档将介绍如何在无CM的情况下使用rpm方式安装CDH6.2.0,与之前安装C5进行对比。环境介绍:...
- ARM64 平台基于 openEuler + iSula 环境部署 Kubernetes
-
为什么要在arm64平台上部署Kubernetes,而且还是鲲鹏920的架构。说来话长。。。此处省略5000字。介绍下系统信息;o架构:鲲鹏920(Kunpeng920)oOS:ope...
- 生产环境starrocks 3.1存算一体集群部署
-
集群规划FE:节点主要负责元数据管理、客户端连接管理、查询计划和查询调度。>3节点。BE:节点负责数据存储和SQL执行。>3节点。CN:无存储功能能的BE。环境准备CPU检查JDK...
- 在CentOS上添加swap虚拟内存并设置优先级
-
现如今很多云服务器都会自己配置好虚拟内存,当然也有很多没有配置虚拟内存的,虚拟内存可以让我们的低配服务器使用更多的内存,可以减少很多硬件成本,比如我们运行很多服务的时候,内存常常会满,当配置了虚拟内存...
- 国产深度(deepin)操作系统优化指南
-
1.升级内核随着deepin版本的更新,会自动升级系统内核,但是我们依旧可以通过命令行手动升级内核,以获取更好的性能和更多的硬件支持。具体操作:-添加PPAs使用以下命令添加PPAs:```...
- postgresql-15.4 多节点主从(读写分离)
-
1、下载软件[root@TX-CN-PostgreSQL01-252software]#wgethttps://ftp.postgresql.org/pub/source/v15.4/postg...
- Docker 容器 Java 服务内存与 GC 优化实施方案
-
一、设置Docker容器内存限制(生产环境建议)1.查看宿主机可用内存bashfree-h#示例输出(假设宿主机剩余16GB可用内存)#Mem:64G...
- 虚拟内存设置、解决linux内存不够问题
-
虚拟内存设置(解决linux内存不够情况)背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存...
- Elasticsearch性能调优(5):服务器配置选择
-
在选择elasticsearch服务器时,要尽可能地选择与当前业务量相匹配的服务器。如果服务器配置太低,则意味着需要更多的节点来满足需求,一个集群的节点太多时会增加集群管理的成本。如果服务器配置太高,...
- Es如何落地
-
一、配置准备节点类型CPU内存硬盘网络机器数操作系统data节点16C64G2000G本地SSD所有es同一可用区3(ecs)Centos7master节点2C8G200G云SSD所有es同一可用区...
- 针对Linux内存管理知识学习总结
-
现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的。对于内存部分需要知道:地址映射内存管理的方式缺页异常先来看一些基本的知识,在进程看来,内存分为内...
- MySQL进阶之性能优化
-
概述MySQL的性能优化,包括了服务器硬件优化、操作系统的优化、MySQL数据库配置优化、数据库表设计的优化、SQL语句优化等5个方面的优化。在进行优化之前,需要先掌握性能分析的思路和方法,找出问题,...
- Linux Cgroups(Control Groups)原理
-
LinuxCgroups(ControlGroups)是内核提供的资源分配、限制和监控机制,通过层级化进程分组实现资源的精细化控制。以下从核心原理、操作示例和版本演进三方面详细分析:一、核心原理与...
- linux 常用性能优化参数及理解
-
1.优化内核相关参数配置文件/etc/sysctl.conf配置方法直接将参数添加进文件每条一行.sysctl-a可以查看默认配置sysctl-p执行并检测是否有错误例如设置错了参数:[roo...
- 如何在 Linux 中使用 Sysctl 命令?
-
sysctl是一个用于配置和查询Linux内核参数的命令行工具。它通过与/proc/sys虚拟文件系统交互,允许用户在运行时动态修改内核参数。这些参数控制着系统的各种行为,包括网络设置、文件...
欢迎 你 发表评论:
- 一周热门
- 最近发表
- 标签列表
-
- 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)
