创建配置、数据、日志目录、首页
mkdir -p /apps/nginx/{config,data,logs,home}
配置docker-compose.yml文件
version: '3.3'
services:
nginx:
image: nginx
restart: always
hostname: nginx
container_name: nginx
privileged: true
ports:
- 80:80
volumes:
#此处为nginx配置文件
- /apps/nginx/config/nginx.conf:/etc/nginx/nginx.conf
#此处为nginx示例的index目录
- /apps/nginx/home/:/usr/share/nginx/html/
#此处为日志目录
- /apps/nginx/logs/:/var/log/nginx/
nginx配置文件 nginx.conf
文件目录放在/apps/nginx/config, 与上面挂载的数据卷目录一致
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server{
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.html;
}
}
}
编写一个index.html,放在/apps/nginx/home下,与挂载的目录一致
vim index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docker搭建nginx</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
启动服务,docker-compose up -d
访问地址,服务器的ip,搭建成功,