Docker中安装nginx

Docker中安装nginx

步骤:

1 、docker pull nginx

 2、docker images

3、docker run -d -p 80:80 --name nginx nginx

4、docker ps

5、查看是否可以访问

注意:我这里是安装在云服务器的docker中

出现上面的页面,说明nginx已经正常启动了。


将nginx的配置文件、日志目录映射到宿主机

6. 在宿主机中创建nginx相关目录

首先在宿主机中创建一个用于存放nginx相关文件的目录,这里我就在 /opt 目录下新建了一个 docker_nginx 的目录文件。

然后在 docker_nginx 目录下新建 conf 、log 、html这三个目录,目录含义是:

conf:存放nginx的相关配置文件,比如 nginx.conf

log:存放nginx的日志文件

html:存放相关静态资源文件

执行命令:

mkdir -p  /opt/docker_nginx_data/{conf,conf.d,html,log}

7. 

docker cp nginx:/etc/nginx/nginx.conf /opt/docker_nginx_data/conf/nginx.conf

这条命令的作用,就是复制nginx的docker容器中的 /etc/nginx/nginx.conf 文件到宿主机下的 /opt/docker_nginx_data/conf/ 目录下。这样可以直接进行默认的配置文件的编辑。

8. 修改nginx.conf

9. docker cp nginx:/usr/share/nginx/html/index.html /opt/docker_nginx_data/html/

同样的,我们也需要把nginx的默认首页文件,复制到宿主机下面

10. docker cp nginx:/etc/nginx/conf.d/default.conf /opt/docker_nginx_data/conf.d/default.conf

复制一份default.conf文件到宿主机对应的目录下

 11. 编辑default.conf文件,修改文件中的 access_log 以及 root 配置,效果如下

12 重启nginx

先停止:docker stop 5tg

启动:

docker run -p 80:80 --name nginx 
    -v /opt/docker_nginx_data/html/:/usr/share/nginx/html 
    -v /opt/docker_nginx_data/log:/var/log/nginx 
    -v /opt/docker_nginx_data/conf/nginx.conf:/etc/nginx/conf 
    -d nginx

原文地址:https://www.cnblogs.com/Edward-Wang/p/12049557.html