Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
# 拉取镜像
docker pull nginx
# 简单运行
docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx
是的,你没有看错,一个简单的Nginx服务器就这样搭建起来了,访问http://localhost即可看到Nginx的欢迎页。需要使用它我们必须进入到容器内部进行更改配置文件操作,第一不是很方便,第二容器内没有现有的文件编辑器。所以这个时候我们需要在宿主机中集中管理Nginx的配置及日志等文件,接下来继续。
Nginx配置文件:/etc/nginx/nginx.conf
Nginx默认配置文件:/etc/nginx/conf.d/default.conf
Nginx日志文件:/var/log/nginx(默认路径,可以在nginx.conf中更改)
Nginx静态文件:/usr/share/nginx/html
# 复制some-nginx容器中的静态文件
docker cp some-nginx:/usr/share/nginx/html /data/nginx/html
# 复制some-nginx容器中的配置文件
docker cp some-nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf
# 复制some-nginx容器中的默认配置文件
docker cp some-nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf
docker run -it -d --name nginx --restart=on-failure:5 -p 8888:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/log:/var/log/nginx --privileged nginx
Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:430: container init caused \"rootfs_linux.go:58: mounting \\\"/data/nginx/conf/nginx.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/bf5dd9a2246c97d037f08199099f2a1a2a4c1f0d612a620e19e466e128baa049/merged\\\" at \\\"/var/lib/docker/overlay2/bf5dd9a2246c97d037f08199099f2a1a2a4c1f0d612a620e19e466e128baa049/merged/etc/nginx/nginx.conf\\\" caused \\\"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.
出错原因是docker -v 命令都是目录映射的,把nginx.conf当成了一个目录进行了创建,导致容器启动的时候找不到nginx.conf文件。
1、直接复制临时Nginx容器中的初始文件到对应目录下(参考上面docker拷贝容器文件到宿主机命令);
2、或者直接新建对应的文件,写入默认的配置,下面是一些初始默认配置文件:
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;
}
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初始配置:
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
然后再重新运行容器,问题解决,可以开启你的Nginx之旅了。
参考:https://hub.docker.com/_/nginx