docker pull nginx
先启动一个默认配置的 Nginx 容器实例(8081主机端口,80容器端口):
docker run --name runoob-nginx-test -p 8081:80 -d nginx
得到容器id:6aa898fb8dd4
在浏览器中打开 http://192.168.64.131:8081/,效果如下:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
运行上面的容器可以把容器内部默认的配置文件复制出来,然后后面使用他的默认配置文件。
创建目录 nginx, 用于挂载后面的相关东西
mkdir -p /opt/nginx/html /opt/nginx/logs /opt/nginx/conf
docker cp 6aa898fb8dd4:/etc/nginx/nginx.conf /opt/nginx/conf //把默认的配置文件也拷贝过来,方便直接使用
docker cp 6aa898fb8dd4:/etc/nginx/conf.d/default.conf /opt/nginx/conf/conf.d //把默认的配置文件也拷贝过来,方便直接使用(注意conf.d是个文件夹,里面有default.conf文件)
docker cp 6aa898fb8dd4:/usr/share/nginx/html/index.html /opt/nginx/html //把默认的静态文件也拷贝过来,方便直接使用
docker cp 6aa898fb8dd4:/usr/share/nginx/html/50x.html /opt/nginx/html //把默认的静态文件也拷贝过来,方便直接使用
- html: 目录将映射为 nginx 容器配置的虚拟目录。
- logs: 目录将映射为 nginx 容器的日志目录。
- conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
- ssl: 目录为后面作https配置用,为可选项。
挂载主机刚才创建和拷出来的文件启动容器 docker run -d -p 80:80 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx
-
-p 80:80: 将容器的 80 端口映射到主机的 80 端口。
-
--name nginx:将容器命名为 nginx。
-
-v /opt/nginx/html:/usr/share/nginx/html:将我们自己创建的 html目录挂载到容器的 /usr/share/nginx/html。
-
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-
-v /opt/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
- -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d: 将conf.d目录挂载,后面进行二级域名反向代理映射端口时用,不配做这个后面配置不生效
-
在浏览器中打开 http://192.168.64.131:80/,效果如下:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.