docker部署Nginx
下载docker镜像
- 启动docker
- 运行命令 docker nginx:版本号 下载某版本的Nginx
启动Nginx容器
- docker run -d --name test-nginx -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/develop:/develop -p:80:80 nginx 启动Nginx并将容器命名为test-nginx、映射主机端口80
- 运行测试
[root@localhost ~]# docker run -d --name test-nginx -p:80:80 nginx e8d788fb291ae633a33cfaea34cb61a4665c32d4022dd8d29f1f386ecd615d2e [root@localhost ~]# curl localhost:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
- -d 后台运行
- –name 给容器命名
- -p 暴露端口, 宿主机端口:容器内部端口
- -v 挂载容器目录至主机
关于docker中运行Nginx容器之后项目无法访问的问题-Nginx配置
- Nginx需要暴露配置文件和项目部署文件
- Nginx配置文件:/etc/nginx/nginx.conf
- Nginx项目文件:/develop
- 可选文件:日志文件
- Nginx配置文件中的路径应为容器内映射地址的路径
http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name 服务器地址或域名; #charset koi8-r; #access_log logs/host.access.log main; location / { root /develop/项目路径; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
- 关于在外部修改配置文件之后需要重启容器
转载请注明出处