一个老生常谈的内容,初学 docker 都会搭建一个 nginx 练习,网上资料一抓一大把。那为什么有这篇文章呢,因为我碰到了一个问题,一个端口引发的知识填坑。
先搞镜像
docker pull nginx
查看 nginx 镜像里面配置文件、日志文件的具体位置
这里有些关键的位置需要挂载到外头
- 配置文件
/etc/nginx/nginx.conf
- 配置文件
/etc/nginx/conf.d/default.conf
- 默认首页文件夹
/usr/share/nginx/html
- 日志文件
/var/log/nginx
先创建对应挂载目录:
$ ls DockerVolume/nginx
conf conf.d html logs
在 conf
文件夹中创建 nginx.conf
配置文件;
把镜像中的配置文件全部拷贝进去
# docker run -i -t nginx /bin/bash
# more /etc/nginx/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
配置文件
把镜像中的配置文件全部拷贝进去
# docker run -i -t nginx /bin/bash
# more /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
#}
}
随便搞个 index.html 首页丢进去 html 文件夹
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover">
<title>nginx</title>
</head>
<body>
<h1>欢迎使用nginx!</h1>
</body>
</html>
准备工作已完成,创建容器并挂载
docker run -d -p 5000:80 \
-v /Users/sayyid/DockerVolume/nginx/html:/usr/share/nginx/html \
-v /Users/sayyid/DockerVolume/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/sayyid/DockerVolume/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v /Users/sayyid/DockerVolume/nginx/logs:/var/log/nginx \
--name nginx nginx \
查看是否启动成功
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
51e49881e7f9 nginx "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:6000->80/tcp nginx
出现问题
我一开始开的是 6000
端口进行映射,浏览器访问就是加载不出来,但是 curl
可以获取到数据,edge、chrome、safari
都不能访问。
$ curl http://localhost:6000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover">
<title>nginx</title>
</head>
<body>
<h1>欢迎使用nginx!</h1>
</body>
</html>
edge
错误内容: 似乎http://localhost:6000/ 上的网页可能有问题,或者该网页可能已永久移到新的网址。
错误代码:ERR_UNSAFE_PORT
safari
错误内容: Not allowed to use restricted network port
错误代码:WebKitErrorDomain:103
找了半天发现是 6000
端口的事情,浏览器阻塞某些端口,尽管它们不在系统端口范围内,其中一些端口广泛用于本地 Web 开发.
从网上找了资料记录一下避免下次再犯这种错误:
# 系统预留
端口号可以分为三个范围:“已知端口”、“注册端口”以及“动态和/或专用端口”。
“已知端口”是从 0 到 1023 的端口。
“注册端口”是从 1024 到 49151 的端口。
“动态和/或专用端口”是从 49152 到 65535 的端口。理论上,不应为服务分配这些端口。
# chrome 浏览器预留(不完全统计)
功能描述 - 端口号
tcpmux - 1
echo - 7
discard - 9
systat - 11
daytime - 13
netstat - 15
qotd - 17
chargen - 19
ftp data - 20
ftp access - 21
ssh - 22
telnet - 23
smtp - 25
time - 37
name - 42
nicname - 43
domain - 53
priv-rjs - 77
finger - 79
ttylink - 87
supdup - 95
hostriame - 101
iso-tsap - 102
gppitnp - 103
acr-nema - 104
pop2 - 109
pop3 - 110
sunrpc - 111
auth - 113
sftp - 115
uucp-path - 117
nntp - 119
NTP - 123
loc-srv /epmap - 135
netbios - 139
imap2 - 143
BGP - 179
ldap - 389
smtp+ssl - 465
exec - 512
login - 513
shell - 514
printer - 515
tempo - 526
courier - 530
chat - 531
netnews - 532
uucp - 540
remotefs - 556
nntp+ssl - 563
stmp? - 587
ldap+ssl - 636
ldap+ssl - 993
pop3+ssl - 995
nfs - 2049
PasswordServer - 3659
lockd - 4045
X11 - 6000
Alternate IRC [Apple addition] - 6665
Alternate IRC [Apple addition] - 6666
Standard IRC [Apple addition] - 6667
Alternate IRC [Apple addition] - 6668
Alternate IRC [Apple addition] - 6669
扩展知识
检查 dokcer IP:
docker inspect nginx
docker inspect --format='{{.NetworkSettings.IPAddress}}' nginx
复制文件:
docker cp nginx:/etc/nginx/nginx.conf /Users/sayyid/DockerVolume/nginx/conf/nginx.conf