Docker容器部署 Nginx服务
首先需要先在自己的Docker中,下载Nginx镜像。
docker pull nginx
配置Nginx
第一步,生成宿主机的文件夹以及宿主机的配置文件
mkdir -p /home/nginx /home/nginx/sharefiles && vim /home/nginx/nginx.conf
第二步,修改nginx.conf 配置文件
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
limit_rate 50k;# 限制下载速度
location / {
root /usr/share/nginx/html/download;
autoindex on; #开启索引功能
autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非 GMT 时间
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
第三步、启动Nginx的Docker容器
docker run -d --name nginx -p 80:80 -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/sharefiles:/usr/share/nginx/html/download -v /data/nginx/conf.d:/etc/nginx/conf.d nginx
参数说明
- –name:指定容器的名称,容器名称不能重复
- -v:挂载数据卷,宿主机路径:容器路径
- -p:指定映射端口,宿主机端口:容器端口
- -d:指定容器后台运行
运行成功后,通过脚本,查看自己的容器是否启动。
docker ps # 查看运行中的容器
或者
docker ps -a # 查看所有的容器
启动成功后,只要输入对应的ip:80 即可