一. 直接启动nginx镜像
1. 下载nginx镜像
docker pull nginx
2. 运行镜像
docker run -p 8080:80 --name web -d nginx
3. 网址查看
xx.xx.xx.xx:8080
二. 挂在文件启动nginx镜像
1. 拷贝docker文件到本地
docker cp web:/etc/nginx/nginx.conf /root/data/config/nginx/conf/
docker cp web:/etc/nginx/conf.d/default.conf /root/data/config/nginx/conf/
docker cp web:/usr/share/nginx/html /root/data/config/nginx/html/
2. 删除之前启动的容器“web”
docker remove -f web
3.启动镜像
docker run --privileged -d -p 8080:80 --name web -v /root/data/config/nginx/html:/usr/share/nginx/html -v /root/data/config/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/data/config/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf:ro -v /root/data/config/nginx/log:/var/log/nginx nginx
备注:docker启动镜像,nginx监听的是容器的端口,需要在启动时通过-p做端口映射
二. ngnix配置https
1. openssl生成自签证书
openssl genrsa -out server.key 2048 #不带加密
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
2. 监听端口
server {listen 443 ssl;ssl_certificate /root/data/cert/server.crt;ssl_certificate_key /root/data/cert/server.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;server_name xx.xx.xx.xx;location / {proxy_pass http://xx.xx.xx.xx:8888/;}
}
3.启动镜像
docker run --privileged -d -p 8080:80 -p 443:443 --name web -v /root/data/config/nginx/html:/usr/share/nginx/html -v /root/data/config/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/data/config/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf:ro -v /root/data/config/nginx/log:/var/log/nginx -v /root/data/cert/server.crt:/root/data/cert/server.crt -v /root/data/cert/server.key:/root/data/cert/server.key nginx
4.访问网址
https://xx.xx.xx.xx