首先拉取镜像

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;
    }

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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐