十二、Docker部署nginx并挂载本地目录到镜像
拉取nginx
docker pull nginx:1.18
准备工作
问题:拉取nginx并运行为容器后,如果想要修改,那么就需要进入容器,这样很麻烦。
解决:因此,要将配置文件挂载出来。使用docker run -v 宿主机目录:容器目录
进行挂载。
失败案例:docker run -d --name web01 -p 10000:80 -v /usr/local/docker/nginx:/etc/nginx nginx:1.18 /bin/bash
此挂载是想将nginx容器的配置目录与宿主机同步,但是 这样去挂是没法成功的。
解决:先运行一个测试的nginx容器,并将里面的nginx配置文件内容复制出来,然后在进行挂载。
目录安排:我将所有Docker容器的东西放在/usr/local/docker
目录下,虽然挂载时会自动创建目录,但是,必须手动将其创建出来,并先写好对应的配置文件的内容。
nginx.conf配置文件
该配置文件在/usr/local/docker/nginx/conf
目录下
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(199, 146, 234)">user</span> nginx;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(199, 146, 234)">worker_processes</span> 1;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(199, 146, 234)">error_log</span> /var/log/nginx/error.<span style="box-sizing: border-box;color: rgb(255, 83, 112)">log warn</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(199, 146, 234)">pid</span> /var/run/nginx.<span style="box-sizing: border-box;color: rgb(199, 146, 234)">pid</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(238, 255, 255)">events</span> {</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">worker_connections</span> 1024;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">}</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(238, 255, 255)">http</span> {</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">include</span> /etc/nginx/mime.<span style="box-sizing: border-box;color: rgb(238, 255, 255)">types</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">default_type</span> application/<span style="box-sizing: border-box;color: rgb(255, 83, 112)">octet-stream</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">log_format</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">main </span> <span style="box-sizing: border-box;color: rgb(195, 232, 141)">'$remote_addr - $remote_user [$time_local] "$request" '</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(195, 232, 141)">'$status $body_bytes_sent "$http_referer" '</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(195, 232, 141)">'"$http_user_agent" "$http_x_forwarded_for"'</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">access_log</span> /var/log/nginx/access.<span style="box-sizing: border-box;color: rgb(255, 83, 112)">log </span> main;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">sendfile</span> on;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#tcp_nopush on;</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">keepalive_timeout</span> 65;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#gzip on;</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">include</span> /etc/nginx/conf.<span style="box-sizing: border-box;color: rgb(103, 110, 149)">d/*.conf;</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(103, 110, 149)">}</span></span>
default.conf
该默认配置文件在/usr/local/docker/nginx/conf.d
目录下
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(238, 255, 255)">server</span> { </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">listen</span> 80; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">server_name</span> localhost; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#charset koi8-r; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#access_log /var/log/nginx/log/host.access.log main; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(238, 255, 255)">location</span> / { </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#root /data/nginx/html; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">root</span> /usr/share/nginx/html; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">index</span> <span style="box-sizing: border-box;color: rgb(199, 146, 234)">index</span>.<span style="box-sizing: border-box;color: rgb(255, 83, 112)">html index</span>.htm; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#autoindex on; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#try_files $uri /index/index/page.html; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#try_files $uri /index/map/page.html; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> } </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#error_page 404 /404.html; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># redirect server error pages to the static page /50x.html </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">error_page </span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">500 502</span> <span style="box-sizing: border-box;color: rgb(255, 83, 112)">503 504</span> /50x.html; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(238, 255, 255)">location</span> = /50x.<span style="box-sizing: border-box;color: rgb(255, 83, 112)">html </span>{ </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(240, 113, 120)">root</span> /usr/share/nginx/html; </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> } </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># proxy the PHP scripts to Apache listening on 127.0.0.1:80 </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#location ~ \.php$ { </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># proxy_pass http://127.0.0.1; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#} </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#location ~ \.php$ { </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># root html; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># fastcgi_pass 127.0.0.1:9000; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># fastcgi_index index.php; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># include fastcgi_params; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#} </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># deny access to .htaccess files, if Apache's document root </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># concurs with nginx's one </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#location ~ /\.ht { </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)"># deny all; </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(103, 110, 149)">#} </span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">}</span>
挂载日志文件
/usr/local/docker/nginx/logs:/var/log/nginx
这样将nginx的日志文件挂载出来是可以的!
但是/usr/local/docker/nginx:/etc/nginx
直接挂载配置目录是不行的,对此我没有明白,如果你明白,我想向你请教!
挂载项目目录
/www:/usr/share/nginx/html
这样挂载项目目录同样是可行的,但是同样的问题,挂载配置目录,所有内容都会消失。但是先将内容写出来,然后再配置却可以 ,对此我不明白。
启动容器
使用下面的命令进行容器启动
docker run -d -p 10000:80 --name web01 -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/conf.d://etc/nginx/conf.d -v /www://usr/share/nginx/html -v /usr/local/docker/nginx/logs:/var/log/nginx nginx:1.18
通过实际测试可以,生效的配置文件是default.conf
注意:下面这个2个启动是我进行目录挂载测试的。
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(103, 110, 149)"># 可以挂载成功,容器中的文件会同步到宿主机</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">docker <span style="box-sizing: border-box;color: rgb(238, 255, 255)">run</span> -d --name web02 -p 10000:80 -v /usr/local/docker/nginx/logs:/var/log/nginx nginx:1.18 </span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">docker <span style="box-sizing: border-box;color: rgb(238, 255, 255)">run</span> -d --name web03 -p 10000:80 -v /www:/usr/share/nginx/html nginx:1.18 </span>
我是小白,期待和优秀的你一起同行!
小白
2020年12月13日