当前环境:Centos7.6 (3.10.0-1062.1.1.el7.x86_64)
搜索Nginx镜像
docker search nginx
获取官方的镜像
下载镜像到本地
docker pull nginx
不指定tag
默认是最新版本,本文使用nginx1.17.6
docker pull nginx:1.17.6
查看本地镜像列表
docker images
创建Nginx容器外部挂载目录
mkdir -p /opt/data/nginx/conf
mkdir -p /opt/data/nginx/conf.d
mkdir -p /opt/data/nginx/html
mkdir -p /opt/data/nginx/logs
创建配置文件
在/opt/data/nginx/conf/
目录下创建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;
}
在/opt/data/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;
}
}
在/opt/data/nginx/html/
目录下创建index.html
文件
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Website!</title>
</head>
<body>
<h1>Hello Nginx!</h1>
</body>
</html>
运行Nginx容器
docker run -d --name nginx -p 80:80 \
-v /opt/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/data/nginx/logs:/var/log/nginx \
-v /opt/data/nginx/html:/usr/share/nginx/html \
-v /opt/data/nginx/conf.d:/etc/nginx/conf.d \
--restart=always --privileged=true nginx:1.17.6
访问下