Nginx教程 2.Docker安装Nginx并挂载到宿主机

创建目录

一般将nginx目录挂载到宿主机方便操作,创建nginx目录:

mkdir /home/dockernginx
mkdir /home/dockernginx/ {conf,html,logs}

创建nginx配置文件如下:

vim /home/dockernginx/conf/nginx.conf

nginx 默认配置如下:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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 run --name nginx01 -d -p 80:80 --restart=always --privileged=true -v /home/dockernginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/dockernginx/html:/etc/nginx/html  -v /home/dockernginx/log:/var/log/nginx  nginx 
  • –name 容器名称
  • -d 后台运行
  • -p 80:80 端口映射
  • –privileged=true 防止挂载时权限不够
  • -v /home/dockernginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx配置文件挂载
  • -v /home/dockernginx/html:/etc/nginx/html nginx静态文件挂载
  • -v /home/dockernginx/log:/var/log/nginx nginx日志文件挂载

访问 http://ip 即可

你可能感兴趣的:(nginx,nginx,docker,运维)