Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 。
Nginx 镜像库地址
通过 Sort by 查看其他版本的 Nginx,默认是最新版本 nginx:latest。
查看可用版本:
docker search nginx
取最新版的 Nginx 镜像
$ docker pull nginx:latest
查看本地镜像
docker images
运行容器
docker run -it -p 8080:80 --name nginx -d --restart=always -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/www:/usr/share/nginx/html -v $PWD/logs:/var/log/nginx --privileged --net=host nginx
参数说明:
-i:表示运行容器
-t:表示容器启动后会进入其命令行。
加入这两个参数后,容器创建就能登录进去,即分配一个伪终端。
--name :为创建的容器命名。
-v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
-d:在run后面加上-d参数,则会创建一个守护式容器在后台运行。
-p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射
--privileged:可以满足我们的定制化需求
(- 使container内的root拥有真正的root权限,否则只是外部的一个普通用户权限
- 可以看到很多host上的设备,并且可以执行mount
- 允许你在docker容器中启动docker容器)
--net=host:
1.创建服务时不需要再做端口映射了。
(比如docker容器内在8080端口起了一个web server.不加的话需要把本机的某个port比如7979和docker内的8080做一个映射关系,访问的时候访问7979. 加了net=host则直接访问8080。)
2.使得创建的容器进入命令行后名称显示为主机的名称而不是一串id.
(比如显示root@sc:/#而不是root@3b8e647e5f79:/#)
--name nginx 表示给这个容器起了一个 nginx 的名字。
第一个 "-v",是配置文件的映射,前边代表宿主机,后面代表容器,解释为宿主机的 nginx 配置文件,映射到容器内的 nginx 子目录下的配置文件上,将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
第二个 "-v" ,是宿主机上目录的映射,映射到容器内的同名的目录下面,这样实现的好处就是不用每次都要登入容器内进行文件的操作,直接宿主机 /home/nginx/www 目录即等同于操作容器。
将我们自己创建的 www 目录挂载到容器的/home/nginx/www
(/usr/share/nginx/html)
将我们自己创建的 logs 挂载到容器的 /var/log/nginx
【注】
!!! 报错
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:75: mounting "/data/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
解决方案:
不启动
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf
重新启动nginx前,运行docker rm -f nginx
并删除目录下自己创建的文件config,启动nginx
进入nginx容器把配置文件复制到 $PWD/conf/ 里面 然后再次启动
第一步
docker run -it -p 8080:80 --name nginx -d --restart=always -v $PWD/www:/usr/share/nginx/html -v $PWD/logs:/var/log/nginx --privileged --net=host nginx
发现没错 正常启动
第二步
进入容器 获取文件
docker exec -it nginx bash
第三步
这里主要获取配置文件路径的
cd /etc/nginx/
第四步
退出容器
exit
第五步
cd /docker/nginx/conf/
第六步 注意 “ . ” 很重要
docker cp 95e2b70fdf49:/etc/nginx/nginx.conf $PWD/conf/nginx.conf .
或者
docker cp [CONTAINER ID]:/etc/nginx/nginx.conf .
ps:#使用id或names【nginx】,文件copy成功后 把nginx容器先删除掉
第七步
docker rm -f nginx
第八步
docker run -it -p 8080:80 --name nginx -d --restart=always -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/www:/usr/share/nginx/html -v $PWD/logs:/var/log/nginx --privileged --net=host nginx
使用ip+80端口成功访问,修改/conf/nginx.conf配置文件即可生效。
PWD/conf/nginx.conf
中 nginx.conf
配置
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;
server {
listen 8080;
server_name localhost;
# Vue路由模式为history需添加的配置
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
root /data/www;
index index.html;
}
# 获取真实IP以及Websocket需添加的配置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 客户端Body大小限制(文件上传大小限制配置)
client_max_body_size 5m;
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root html;
}
}
include /etc/nginx/conf.d/*.conf;
}
在www
目录下创建index.html和50x.html文件
index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
至此,docker安装nginx与配置已经全部完成,欢迎大家点赞评论!!!