目录
docker安装nginx并挂载本地目录
/  

docker安装nginx并挂载本地目录

摘自 https://blog.csdn.net/wuzhangweiss/article/details/88932691

拉取nginx

docker pull nginx

创建文件夹

mkdir nginx

进入nginx目录创建多个文件夹

mkdir -p {conf,html,logs,conf.d}

进入conf文件夹,创建文件nginx.conf

cd conf
vi nginx.conf

将下面内容填入该文件


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
	    # proxy_pass http://127.0.0.1:8989;
            index  index.html index.htm;
        }

	
	location ^~/resources {
	    root myresources;
	    autoindex on;
	}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

运行

创建文件夹 nginx

docker run -d -p 80:80 --name nginx -v /media/nianshao/software/docker/nginx/logs:/var/log/nginx -v /media/nianshao/software/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /media/nianshao/software/docker/nginx/conf.d:/etc/nginx/conf.d -v /media/nianshao/software/docker/nginx/html:/usr/share/nginx/html nginx:latest

docker run -d
-p 80:80
--name nginx
-v /media/nianshao/software/docker/nginx/logs:/var/log/nginx
-v /media/nianshao/software/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /media/nianshao/software/docker/nginx/conf.d:/etc/nginx/conf.d -v /media/nianshao/software/docker/nginx/html:/usr/share/nginx/html nginx:latest

-v 主机上的目录:nginx镜像中的默认目录(docker)
-p 主机上的端口:nginx镜像中的默认端口

如果容器自动退出

可以再后面加上/bin/bash -c "tail -f /dev/null"

创建好容器,需要我们进入容器手动启动nginx,我们可以编写一启动nginx的脚本,然后将脚本弄成开机自启就ok了。