1、创建目录
mkdir -p /etc/nginx /opt/nginx/html
2、创建配置文件
touch /etc/nginx/nginx.conf
- 配置文件添加默认内容
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; 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 html; } } }
3、拉取镜像
docker pull nginx
4、启动容器
docker run -it -d --name nginx \ --restart=always \ -p 80:80 \ -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf \ -v /opt/nginx/html:/usr/share/nginx/html \ nginx:latest
两个-v 分别表示配置文件和nginx工作空间目录的映射,冒号左边是宿主机,右边是容器的,这样修改宿主机的配置文件和工作空间就可以同步到容器
5、查看 Nginx 启动日志
docker logs nginx
6、其它命令
- 启动
docker start nginx
- 关闭
docker stop nginx
- 重启
docker restart nginx