龙空技术网

Docker(01) Nginx容器部署,创建 docker 容器后修改挂载目录的方法

心平氣和 22

前言:

目前各位老铁们对“nginx修改默认根目录”可能比较讲究,大家都想要剖析一些“nginx修改默认根目录”的相关文章。那么小编同时在网络上收集了一些关于“nginx修改默认根目录””的相关内容,希望朋友们能喜欢,你们快快来了解一下吧!

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 bullseye InRelease

Get:2 bullseye-security InRelease [48.4 kB]

Hit:3 bullseye InRelease

Get:4 bullseye-updates InRelease [44.1 kB]

Get:5 bullseye-backports InRelease [49.0 kB]

Get:6 bullseye-security/main Sources [217 kB]

Get:7 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 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 优点和缺点

优点

简单粗暴,在测试环境用的更多

缺点

如果是数据库、服务器相关的容器,创建新的容器,又得重新配置相关东西了

标签: #nginx修改默认根目录 #centos62挂载新硬盘