docker 部署 nginx实例
主要知识点:
- 前台后台,容器与虚拟机之间的区别,容器中的应用都应该以前台执行
- 配置文件的挂载
编辑Dockerfile文件
FROM nginx
COPY fronts /root/fronts
CMD nginx -g "daemon off;"
创建镜像
$docker build -t fronts:v1 .
编辑配置文件
原配置文件,修改即可
nginx.conf
user nginx; # 此处需要修改为user root;权限问题
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;
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;
}
}
启动容器
这里启动容器我们有两个选择,一是覆盖/etc/nginx/nginx.conf根配置文件,
二是覆盖/etc/nginx/conf.d/default.conf文件。相比之下二选择更优,指责划分更加清晰。
docker run -p 8000:80 -v /absolute/path/nginx.conf:/etc/nginx/conf.d/default.conf -d nginx

更多精彩