docker中部署nginx镜像挂载文件夹和文件并解决出错

首先拉取镜像

docker pull nginx

首先在宿主机创建要挂载的目录

mkdir -p /data/docker  
mkdir -p /data/docker/nginx/conf #存放配置文件

首先创建一个测试的nginx

因为不能挂载文件,只能挂载文件夹,所以先在一个test容器中复制一份配置文件。

先复制nginx.conf

docker run --name test -d nginx  
docker cp test:/etc/nginx/nginx.conf /data/docker/nginx/conf/

 配置文件先不需要改

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}

如果不知道配置文件的存放目录,可以进去容器查看一下。 

docker exec -it test /bin/bash

 然后运行你正式的nginx 容器 并设置挂载目录,记住赋予它权限。

docker run --privileged -it -p 80:80 \
-v /data/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/docker/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /data/docker/nginx/html:/usr/share/nginx/html:rw \
-v/data/docker/nginx/logs:/var/log/nginx -d nginx

 

 可以看到容器运行起来,并且目录已经挂载好。

但是查看/data/docker/conf/conf.d中发现是空的。

因为docker不能挂载文件,只能挂载文件夹,所以需要再复制default.conf 文件

docker cp test:/etc/nginx/conf.d/default.conf  /data/docker/nginx/conf/conf.d

里面的配置文件都不需要改。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 再编辑/data/docker/nginx/html 里面的index.html文件

然后重新启动,访问就可以成功

docker restart ID 
curl ip:port

做的过程中遇到了很多错误。

docker 部署 nginx 错误总结

error1:docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused \"rootfs_linux.go:58: mounting \\\"/data/docker/conf/nginx.conf\\\" to rootfs \\\"/var/lib/docker/devicemapper/mnt/31a568212964584f347be95e38e93d02ab9d432302d32b458559fdeeec3648e6/rootfs\\\" at \\\"/var/lib/docker/devicemapper/mnt/31a568212964584f347be95e38e93d02ab9d432302d32b458559fdeeec3648e6/rootfs/etc/nginx/nginx.conf\\\" caused \\\"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.

原因就是挂载出错,不能直接挂载文件,还有挂载的容器里的目录要正确,不能照搬其他文章的路径。

error2:curl: (7) Failed connect to 192.168.75.111:80; Connection refused。

原因是没有default.conf文件 ,有的只配置了nginx.conf 文件

error3:curl: (56) Recv failure: Connection reset by peer 

error4:

<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.15.7</center>
</body>
</html>

查看了一下日志文件,报错内容是这样的。

 *1 rewrite or internal redirection cycle while internally redirecting to "/index/index/page.html", client: 192.168.75.111, server: localhost, request: "GET / HTTP/1.1", host: "192.168.75.111:9992"

应该是配置文件没写对,因为一开始是复制的网上的,可能他改过配置,应该复制自己的测试nginx容器里面的配置文件。

error5:

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.7</center>
</body>
</html>

注意default.conf里面,路径就是你容器里面nginx的网站路径。写成宿主机的会出这个错。

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

最后祝大家平安夜快乐啊,今天实验室给每个人都发了苹果和橙子,好开心啊。。

  • 22
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
要在Docker部署NGINX挂载目录,你可以按照以下步骤操作: 1. 首先,创建一个用于存储NGINX配置文件和静态文件的目录。假设你创建了一个名为`nginx_data`的目录。 2. 创建一个名为`nginx.conf`的NGINX配置文件,并将其放置在`nginx_data`目录。你可以根据你的需求自定义这个配置文件。 3. 将你的静态文件放置在`nginx_data`目录,例如将HTML、CSS、JavaScript文件等放置在一个名为`static`的子目录。 4. 现在,创建一个Dockerfile来构建NGINX镜像。在Dockerfile,你需要指定NGINX基础镜像、复制`nginx.conf`到相应位置以及挂载`nginx_data`目录。 ```Dockerfile # 使用NGINX基础镜像 FROM nginx # 复制自定义的NGINX配置文件到容器 COPY nginx.conf /etc/nginx/nginx.conf # 挂载目录 VOLUME /path/to/nginx_data:/usr/share/nginx/html # 暴露NGINX默认端口(可选) EXPOSE 80 ``` 请将上述代码的`/path/to/nginx_data`替换为实际的`nginx_data`目录的路径。 5. 使用该Dockerfile构建NGINX镜像。打开终端,进入到包含Dockerfile的目录,然后执行以下命令: ```bash docker build -t mynginx . ``` 这将使用`mynginx`作为镜像名称构建NGINX镜像。请确保在运行此命令之前已经安装了Docker。 6. 构建镜像后,你可以使用以下命令运行NGINX容器并挂载`nginx_data`目录: ```bash docker run -d -p 80:80 -v /path/to/nginx_data:/usr/share/nginx/html --name mynginx-container mynginx ``` 这将在端口80上运行NGINX容器,并将主机上的`nginx_data`目录挂载到容器的`/usr/share/nginx/html`目录。请将上述命令的`/path/to/nginx_data`替换为实际的`nginx_data`目录的路径。 现在,你已经成功部署了一个挂载目录的NGINX容器。你可以通过访问`http://localhost`来查看NGINX服务器是否正常工作,并通过修改`nginx_data`目录文件来更新NGINX配置和静态文件
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值