docker nginx 配置文件在哪?如何修改?如何挂载?

昨天在dockers安装了nginx,安装倒是很简单docker pull nginx:版本号,不写版本号默认是pull最新版。运行就是docker run --name nginx -p 8080:80 -d nginx然后访问·localhost:8080就可以看到nginx的欢迎界面了。
虽然docker安装很方便,但是对于刚入门的要想修改里面的配置文件就不简单了,
网上有很多讲解如何在本地目录上挂载配置文件的文章。但是对于我这种新手来说看得晕头转向。
希望我这篇文章可以给被dockers墙上的程序员一点帮助-_-。
我们安装完nginx 后会发现配置文件在哪,日志文件目录在哪,web项目放到那个目录下,等等。
关于docker的简单的原理解释可以参考docker
实际上docker run一个镜像会生成一个容器,而我们需要修改的配置文件日志文件等等都在这个容器里,但是要想进入容器修改配置却不容易,下面介绍二种方法。

一在容器里面修改

  1. 运行docker run --name docker_nginx -d -p 8080:80 nginxId
  2. 进入容器bash
[root@localhost nginx]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
34d0daa6b54c        nginx               "nginx -g 'daemon ..."   10 seconds ago      Up 9 seconds        0.0.0.0:8080->80/tcp                docker_nginx
a2926eeff28f        mysql:5.7           "docker-entrypoint..."   2 weeks ago         Up 22 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   hardcore_perlman
[root@localhost nginx]# docker exec -it 34d0 bash
root@34d0daa6b54c:/# 

注意(docker 的Id可以简写为完整id的前几个字符)

  • 如果我们只是想要查看配置文件可以使用
    cat /etc/nginx/nginx.conf
  • 如果我们想要使用vim /etc/nginx/nginx.conf 修改会提示vim: command not found
root@34d0daa6b54c:/etc/nginx/conf.d# vim /etc/nginx/nginx.conf
bash: vim: command not found

因为容器是与本地几乎隔绝的,所以我们要先安装vim

1. 先更新包管理
apt-get update
2. 安装vim
apt-get install vim
时间比较慢,如果有找到如何更新安装源的小伙伴贴到评论区分享下

安装完成后就可以修改配置文件了
// 退出容器

root@34d0daa6b54c:/etc/nginx/conf.d# exit
exit
[root@localhost nginx]# 
日志目录 : /var/log/nginx 
配置目录 : /etc/nginx/conf.d 
主配置文 : 件/etc/nginx/nginx.conf 
项目目录 : /usr/share/nginx/html nginx

二 将配置文件等目录挂载到本地

这个操作对于以后修改很方便,推荐这一种

  1. 首先在本地准备我们的挂载目录以及配置文件
[root@localhost nginx]# cd /home/hewcen/mynginx/nginx/
[root@localhost nginx]# ls
conf  conf.d  html  log
[root@localhost nginx]# 
  1. 建好目录后还需要二个配置文件
  • /home/hewcen/mynginx/nginx/conf nginx.conf
    小伙伴们可以直接复制也可以按照方法一进入到容器里复制自己的
vim /home/hewcen/mynginx/nginx/conf/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

  • /home/hewcen/mynginx/nginx/conf.d default.conf
vim /home/hewcen/mynginx/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

配置文件创建完成后我们在新建一个index.html
/home/hewcen/mynginx/nginx/html index.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>hello docker!</h1>
</body>
</html>

  1. 然后我们就可以在运行时指定挂载目录了
docker run --name docker_nginx -d -p 8080:80 \
-v /home/hewcen/mynginx/nginx/log:/var/log/nginx \
-v /home/hewcen/mynginx/nginx/conf.d:/etc/nginx/conf.d \
-v /home/hewcen/mynginx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/hewcen/mynginx/nginx/html:/usr/share/nginx/html nginx

   挂载日志目录
   挂载配置目录
   挂载主配置文件
   挂载项目目录
  • 注意如果你加上挂载目录运行报错
    ·open() “/etc/nginx/nginx.conf” failed (13: Permission denied)·
    查看错误日志方法是docker logs 运行容器的Id
    id查看命令是 docker ps -a
    显示所有运行的容器,或者运行失败的容器
    解决方法既然提示权限问题,那我们就给他root权限。
    --privileged=true \

三。 测试下

浏览器
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值