1、安装 nginx 镜像
docker pull nginx
2、在主机中创建挂载目录
- 挂载 nginx 的静态文件目录
mkdir /your/dir/html
- 挂载 nginx 的配置目录
mkdir /your/dir/conf/conf.d
- 挂载 nginx 的日志目录
mkdir /your/dir/log
3、配置文件
将 nginx.conf 配置文件放在 /your/dir/conf/ 下。
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;
autoindex on;
gzip on;
client_max_body_size 100M;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
include /etc/nginx/conf.d/*.conf;
}
将 default.conf 配置文件放在 /your/dir/conf/conf.d 下。
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/access.log main;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
4、启动容器
将容器 80 端口映射到主机 80 端口。
docker run \
--name docker-nginx \
-d -p 80:80 \
-v /your/dir/config/nginx.conf:/etc/nginx/nginx.conf \
-v /your/dir/config/conf.d:/etc/nginx/conf.d \
-v /your/dir/html:/usr/share/nginx/html \
-v /your/dir/log:/var/log/nginx \
nginx