目录
安装Nginx镜像
docker pull nginx
简单启动
创建启动容器
docker run --name nginx -p 80:80 -d nginx
- --name="nginx": 为容器指定一个名称
- -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
- -d: 后台运行容器,并返回容器ID;
查看容器进程
docker ps
![]()
更改配置文件nginx.conf
- 进入容器:docker exec -it nginx bash
- nginx.conf配置文件在 /etc/nginx/ 下面,但是你使用vim nginx.conf 或者vi nginx.conf,会发现vi或者vim命令没有用,解决办法:apt-get update 完成之后 apt-get install vim
- 此时你就可以自己定制nginx.con文件了,改好配置文件之后重启容器,步骤,先把容器停了
- docker stop nginx 然后重启 docker start nginx
挂载配置文件启动
开启共享文件夹
在Docker of windows的Setting 中进行配置,将盘符进行共享
此处为windows用户的开机密码:
在本机上创建相应的目录
conf目录下创建 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; }
conf.d 目录下创建 default.conf
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; #autoindex on; #try_files $uri /index/index/page.html; #try_files $uri /index/map/page.html; } #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 /usr/share/nginx/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; #} }
- 以上两个都是服务器的配置文件
- logs文件夹是存放日志的,保持空目录即可
- www文件夹是放置网站文件的 放一个用一个index.html测试
创建启动容器
需要自己准备好nginx.conf 和default.conf
docker run --name nginx -p 9999:80 -v /D/home/nginx/www:/usr/share/nginx/html -v /D/home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /D/home/nginx/logs:/var/log/nginx -v /D/home/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest
- --name="nginx": 为容器指定一个名称
- -p: 指定端口映射,格式为:主机(宿主)端口:容器端口,可以使用多个-p做多个端口映射(-p 5000:23 -p 5001:22)
- -d: 后台运行容器,并返回容器ID;
- -i:表示运行容器
- -t:表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
- -v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
- 第一个“-v”,是配置文件的映射,前边代表宿主机,后面代表容器,解释为宿主机的 nginx 配置文件,映射到容器内的 nginx 子目录下的配置文件上。
- 第二个 "-v" ,是宿主机上目录的映射,映射到容器内的同名的目录下面,同样的前边代表宿主机,后面代表容器,这样实现的好处就是不用每次都要登入容器内进行文件的操作,直接宿主机 /home/nginx/www 目录即等同于操作容器。
- 注意本地路径的写法, D:\abc ⇒ /D/abc
查看容器进程&启动状态
docker命令安装nginx与挂载配置文件启动-详细完整版
最新推荐文章于 2024-06-07 14:57:06 发布