首先,拉去nginx最新镜像
docker pull nginx:latest
使用以下命令查看是否安装成功
docker images
![59600315fe17113870e6eaf2567b09e4.png](https://img-blog.csdnimg.cn/img_convert/59600315fe17113870e6eaf2567b09e4.png)
运行容器,映射到本地80端口
docker run --name nginx -p 80:80 -d nginx
访问
![a03c359c351341546acfd9a7ee50ba5d.png](https://img-blog.csdnimg.cn/img_convert/a03c359c351341546acfd9a7ee50ba5d.png)
nginx 配置
因为我需要使用nginx做静态资源服务器,所以还需要添加相应配置
首先copy出nginx 配置
mkdir ~nginx/conf ~nginx/logs
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/conf
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;
}
可以看到配置文件中include /etc/nginx/conf.d/*.conf,这就是我将要映射配置的路径
因为我打算还是用nginx中的默认配置文件,所以就把刚刚copy下来的nginx.conf给删掉了
rm ~/nginx/conf/nginx.conf
在刚刚创建好的~/nginx/conf路径下,新增配置
vim ~/nginx/conf/video.conf
添加server配置
server{
server_name localhost;
listen 80;
location ^~ /video/{
root /Users/yanyi/data/;
}
}
关闭删除刚刚启动的容器
docker stop nginx
docker rm nginx
重新启动新的容器
docker run --name nginx -p 80:80
-v ~/nginx/conf:/etc/nginx/conf.d
-v ~/nginx/logs:/var/log/nginx
-v ~/data:/User/yanyi/data/
-d nginx
访问资源
![9de98d7f97c4d862ac7215948b522ba6.png](https://img-blog.csdnimg.cn/img_convert/9de98d7f97c4d862ac7215948b522ba6.png)