目录
从hup上pull镜像
docker pull nginx
创建将要挂载的目录
mkdir /usr/local/docker/
mkdir -p /usr/local/docker/nginx-test/{conf,conf.d,html,logs}
创建配置文件
vim /usr/local/docker/nginx-test/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;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
vim /usr/local/docker/nginx-test/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
vim /usr/local/docker/nginx-test/html/index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>系统时间</title>
</head>
<body>
<div id="datetime">
<script>
setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
</script>
</div>
</body>
启动容器
docker run --rm --name nginx-test -p 81:80 -v /usr/local/docker/nginx-test/html:/usr/share/nginx/html -v /usr/local/docker/nginx-test/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx-test/logs:/var/log/nginx -v /usr/local/docker/nginx-test/conf.d:/etc/nginx/conf.d -d nginx
访问nginx