docker 启动 nginx 报错 open() “/etc/nginx/nginx.conf“ failed (2: No such file or directory)

这篇具有很好参考价值的文章主要介绍了docker 启动 nginx 报错 open() “/etc/nginx/nginx.conf“ failed (2: No such file or directory)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

docker 启动 nginx 时报错 open() “/etc/nginx/nginx.conf” failed (2: No such file or directory)

问题描述

在学习 docker 的 volume 挂载时,用 nginx 镜像来做测试,当我想指定目录挂载数据卷,启动容器时,却发现容器没有正常运行,我用 docker ps -a 命令查看该容器时,状态为 Exited,我进一步查看容器日志输出 docker logs -fnt 3ab28f555fe4 发现报错 open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

解决过程

完整解决过程如下:

[root@VM-4-15-centos mysql]# docker run -d -P -v /data/data/nginx:/etc/nginx --name nginx_node3 nginx
3ab28f555fe49411c297c6f586fcfa7c28e6839a247ee3c5dd54ac58a231d587

# 查看正在运行的容器发现没有 nginx_node3
[root@VM-4-15-centos nginx]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS                                                                                  NAMES
d3a66ee96fda   nginx                 "/docker-entrypoint.…"   4 minutes ago       Up 4 minutes       0.0.0.0:49154->80/tcp, :::49154->80/tcp                                                nginx_node2
76f4658c2f44   nginx                 "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes       0.0.0.0:49153->80/tcp, :::49153->80/tcp                                                nginx_node1
e780a1aa1a78   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql_5.7
a68bf3079841   portainer/portainer   "/portainer"             2 days ago          Up 2 days          8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                          portainer
559bf38ed20c   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 days ago          Up 2 days          0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch

# 查看所有容器 发现 nginx_node3 status 为 Exited
[root@VM-4-15-centos nginx]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS                          PORTS                                                                                  NAMES
3ab28f555fe4   nginx                 "/docker-entrypoint.…"   About a minute ago   Exited (1) About a minute ago                                                                                          nginx_node3
d3a66ee96fda   nginx                 "/docker-entrypoint.…"   4 minutes ago        Up 4 minutes                    0.0.0.0:49154->80/tcp, :::49154->80/tcp                                                nginx_node2
76f4658c2f44   nginx                 "/docker-entrypoint.…"   6 minutes ago        Up 6 minutes                    0.0.0.0:49153->80/tcp, :::49153->80/tcp                                                nginx_node1
e780a1aa1a78   mysql:5.7             "docker-entrypoint.s…"   About an hour ago    Up About an hour                0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql_5.7
a68bf3079841   portainer/portainer   "/portainer"             2 days ago           Up 2 days                       8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                          portainer
559bf38ed20c   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 days ago           Up 2 days                       0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch

# 查看 nginx_node3 容器内部日志,发现有报错
[root@VM-4-15-centos nginx]# docker logs -fnt 3ab28f555fe4
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
2022/12/08 07:34:50 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
/docker-entrypoint.sh: Configuration complete; ready for start up
[root@VM-4-15-centos nginx]# 

# 解决方法
# 1. 先去除 -v 挂载参数 启动容器
[root@VM-4-15-centos nginx]# docker run -d -P --name nginx_node3 nginx
# 2. 进入容器查看 /etc/nginx 目录下文件是否正常(一般正常启动则没有问题)
[root@VM-4-15-centos nginx]# docker exec -it nginx_node3 /bin/bash
root@50b982999237:/# cd /etc/nginx/
root@50b982999237:/etc/nginx# ls
conf.d    fastcgi_params    mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@50b982999237:/etc/nginx# exit
exit
[root@VM-4-15-centos nginx]#

# 3. 将容器内 /etc/nginx 目录拷贝到宿主机目录
[root@VM-4-15-centos nginx]# docker cp 50b982999237:/etc/nginx /data/data/nginx
[root@VM-4-15-centos nginx]# ll
total 28
drwxr-xr-x 2 root root 4096 Dec  8 15:38 conf.d
-rw-r--r-- 1 root root 1007 Oct 19 15:56 fastcgi_params
-rw-r--r-- 1 root root 5349 Oct 19 15:56 mime.types
lrwxrwxrwx 1 root root   22 Oct 19 17:32 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  648 Oct 19 17:32 nginx.conf
-rw-r--r-- 1 root root  636 Oct 19 15:56 scgi_params
-rw-r--r-- 1 root root  664 Oct 19 15:56 uwsgi_params

# 4. 删除 nginx_node3 这个容器
[root@VM-4-15-centos nginx]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS                                                                                  NAMES
50b982999237   nginx                 "/docker-entrypoint.…"   8 minutes ago       Up 8 minutes       0.0.0.0:49156->80/tcp, :::49156->80/tcp                                                nginx_node3
d3a66ee96fda   nginx                 "/docker-entrypoint.…"   15 minutes ago      Up 15 minutes      0.0.0.0:49154->80/tcp, :::49154->80/tcp                                                nginx_node2
76f4658c2f44   nginx                 "/docker-entrypoint.…"   17 minutes ago      Up 17 minutes      0.0.0.0:49153->80/tcp, :::49153->80/tcp                                                nginx_node1
e780a1aa1a78   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql_5.7
a68bf3079841   portainer/portainer   "/portainer"             2 days ago          Up 2 days          8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                          portainer
559bf38ed20c   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 days ago          Up 2 days          0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
[root@VM-4-15-centos nginx]# docker rm -f 50b982999237
50b982999237
[root@VM-4-15-centos nginx]# 

# 5. 重启启动容器并挂载指定目录
[root@VM-4-15-centos nginx]# docker run -d -P -v /data/data/nginx:/etc/nginx --name nginx_node3 nginx
abe892822eed95f323e1d3f19187398867ed0ec3f1a676e54db40cfe0d62a3f2
[root@VM-4-15-centos nginx]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS                                                                                  NAMES
abe892822eed   nginx                 "/docker-entrypoint.…"   3 seconds ago       Up 2 seconds       0.0.0.0:49157->80/tcp, :::49157->80/tcp                                                nginx_node3
d3a66ee96fda   nginx                 "/docker-entrypoint.…"   15 minutes ago      Up 15 minutes      0.0.0.0:49154->80/tcp, :::49154->80/tcp                                                nginx_node2
76f4658c2f44   nginx                 "/docker-entrypoint.…"   18 minutes ago      Up 18 minutes      0.0.0.0:49153->80/tcp, :::49153->80/tcp                                                nginx_node1
e780a1aa1a78   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql_5.7
a68bf3079841   portainer/portainer   "/portainer"             2 days ago          Up 2 days          8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                          portainer
559bf38ed20c   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 days ago          Up 2 days          0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
[root@VM-4-15-centos nginx]# 
# 查看日志输出 运行正常
[root@VM-4-15-centos nginx]# docker logs -fnt abe892822eed
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/12/08 07:48:19 [notice] 1#1: using the "epoll" event method
2022/12/08 07:48:19 [notice] 1#1: nginx/1.23.2
2022/12/08 07:48:19 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/12/08 07:48:19 [notice] 1#1: OS: Linux 3.10.0-1160.76.1.el7.x86_64
2022/12/08 07:48:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/08 07:48:19 [notice] 1#1: start worker processes
2022/12/08 07:48:19 [notice] 1#1: start worker process 22
2022/12/08 07:48:19 [notice] 1#1: start worker process 23

# 如何验证挂载是否达到预期?
[root@VM-4-15-centos nginx]# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
# 在宿主机挂载目录下修改 nginx.conf 文件 keepalive_timeout  60;
[root@VM-4-15-centos nginx]# vim nginx.conf
[root@VM-4-15-centos nginx]# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  60;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

# 进入容器内部查看修改的 nginx.conf 发现已经生效。
[root@VM-4-15-centos nginx]# docker exec -it nginx_node3 /bin/bash
root@abe892822eed:/# cd /etc/nginx/
root@abe892822eed:/etc/nginx# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  60;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


问题总结

此类指定目录挂载数据卷的问题还出现在例如mysql等依赖配置文件的容器服务部署。解决方法可以作为统一参考,依葫芦画瓢。
背后原理有待细纠,后续搞明白了,我也会再来更新。
社区大佬们也可以帮忙解释一下!文章来源地址https://www.toymoban.com/news/detail-518171.html

到了这里,关于docker 启动 nginx 报错 open() “/etc/nginx/nginx.conf“ failed (2: No such file or directory)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包