docker部署nginx并且挂载文件夹和文件操作

这段时间在研究docker,在部署nginx时遇到了坑,最主要的问题是在挂载文件和文件夹的时候不知道怎么挂载,经过反复实验以及查看网上的教程,先总结如下:

1首先pull下载nginx镜像包

docker pull nginx

2(关键)查看nginx镜像里面配置文件、日志等文件的具体位置,只有找到镜像配置文件的路径,后面挂载文件和文件夹才能覆盖这些路径

以终端的方式打开镜像容器

[root@docker2 nginx]# docker run -i -t nginx /bin/bash
root@3b39da9212fe:/# ls -l
total 8
drwxr-xr-x  2 root root 4096 Apr 26 00:00 bin
drwxr-xr-x  2 root root  6 Feb 23 23:23 boot
drwxr-xr-x  5 root root 360 May 30 01:39 dev
drwxr-xr-x  1 root root  66 May 30 01:39 etc
drwxr-xr-x  2 root root  6 Feb 23 23:23 home
drwxr-xr-x  1 root root  45 Apr 26 00:00 lib
drwxr-xr-x  2 root root  34 Apr 26 00:00 lib64
drwxr-xr-x  2 root root  6 Apr 26 00:00 media
drwxr-xr-x  2 root root  6 Apr 26 00:00 mnt
drwxr-xr-x  2 root root  6 Apr 26 00:00 opt
dr-xr-xr-x 176 root root  0 May 30 01:39 proc
drwx------  2 root root  37 Apr 26 00:00 root
drwxr-xr-x  4 root root  43 Apr 26 00:00 run
drwxr-xr-x  2 root root 4096 Apr 26 00:00 sbin
drwxr-xr-x  2 root root  6 Apr 26 00:00 srv
dr-xr-xr-x 13 root root  0 May 25 06:07 sys
drwxrwxrwt  1 root root  6 Apr 30 13:55 tmp
drwxr-xr-x  1 root root  66 Apr 26 00:00 usr
drwxr-xr-x  1 root root  17 Apr 26 00:00 var

找到镜像中nginx.conf配置文件路径/etc/nginx/nginx.conf

root@3b39da9212fe:/etc/nginx# ls -l /etc/nginx/
total 36
drwxr-xr-x 2 root root  26 Apr 30 13:55 conf.d
-rw-r--r-- 1 root root 1007 Apr 9 16:01 fastcgi_params
-rw-r--r-- 1 root root 2837 Apr 9 16:01 koi-utf
-rw-r--r-- 1 root root 2223 Apr 9 16:01 koi-win
-rw-r--r-- 1 root root 5170 Apr 9 16:01 mime.types
lrwxrwxrwx 1 root root  22 Apr 9 16:01 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 643 Apr 9 16:01 nginx.conf
-rw-r--r-- 1 root root 636 Apr 9 16:01 scgi_params
-rw-r--r-- 1 root root 664 Apr 9 16:01 uwsgi_params
-rw-r--r-- 1 root root 3610 Apr 9 16:01 win-utf

找到default.conf配置文件的路径/etc/nginx/conf.d/default.conf

root@3b39da9212fe:/etc# ls -l /etc/nginx/conf.d/
total 4
-rw-r--r-- 1 root root 1093 Apr 9 16:01 default.conf

找到默认首页文件夹html路径/usr/share/nginx/html

root@3b39da9212fe:/etc# ls -l /usr/share/nginx/
total 0
drwxr-xr-x 2 root root 40 Apr 30 13:55 html

找到日志文件路径/var/log/nginx

ls -l /var/log/
total 96
drwxr-xr-x 1 root root  60 Apr 30 13:55 apt
-rw-rw---- 1 root utmp   0 Apr 26 00:00 btmp
-rw-r--r-- 1 root root 57602 Apr 30 13:55 dpkg.log
-rw-r--r-- 1 root root 3264 Apr 30 13:55 faillog
-rw-rw-r-- 1 root utmp 29784 Apr 30 13:55 lastlog
drwxr-xr-x 1 root root  41 Apr 30 13:55 nginx
-rw-rw-r-- 1 root utmp   0 Apr 26 00:00 wtmp

然后输入exit退出容器的终端

3用nginx镜像启动容器mynginx并且挂载文件夹和文件到容器中

这里说明一下为什么我要挂载配置文件和文件夹,如果你部署应用并且很轻易地修改nginx的配置文件,如果挂载了文件或者文件夹那么你只需要修改挂载源的文件或者文件夹里面的文件就可以了,而不用每次都要使用docker run -i -t nginx /bin/bash命令进入到镜像终端中去修改配置文件,下面我将演示修改自己的nginx首页,并且将其挂载上去容器中覆盖掉原来的默认的首页

在linux系统中创建挂载源文件和文件夹(我的是centos7)

mkdir -p /data/nginx/conf
mkdir -p /data/nginx/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs

然后创建在conf文件夹里面创建一个nginx.conf配置文件,并且输入一下内容,建议大家不要照抄我的配置,用我上面介绍的第一步的方法进入到nginx容器的终端中复制nginx.conf配置文件的内容到linux系统中这个新创建的nginx.conf文件中进行修改,这样子就保证了配置文件中的路径与镜像中配置文件的路径能保持一致

[root@docker2 /]# cd /data/nginx/conf
[root@docker2 conf]# more 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;
}

在conf.d里面创建一个default.conf文件,并且输入一下内容,同样这个内容也是我从镜像中default.conf默认的配置文件中复制过来修改的,同样建议大家不要照抄我的内容,因为涉及到路径那些可能会与你们nginx镜像中的路径不一致,这样子在启动镜像创建容器的时候就无法用挂载的方法覆盖掉容器中的路径

[root@docker2 conf]# more /data/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 1.html;
  }

  #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;
  #}
}

大家注意了,这里我修改了一下原来默认配置文件里面的内容,在上面的其中一个location中,我把nginx默认首页index改成了1.html,1.html是我自己创建的首页名

在html文件夹下创建1.html首页文件,并且编写属于自己的首页,这里我是用notepadd++在windows上面写好了1.html文件再通过工具拷过去linux系统里面的,注意有中文的可能要转换下编码,不然可能会乱码,例如我这里用的是ansi的编码

<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h1>
欢迎使用nginx!
</h1>
</body>
</html>

现在是创建容器并且挂载文件和文件夹了

[root@docker2 conf]# docker run --name mynginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/logs:/var/log/nginx nginx

记住挂载的目标目录或者文件路径要与镜像中的路径保持一致如/etc/nginx/nginx.conf,这个路径在第二步里面已经找出来了

docker ps 查看有没有启动成功

[root@docker2 conf]# docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
32ad171d34a2    nginx        "nginx -g 'daemon of…"  17 hours ago    Up 17 hours     0.0.0.0:80->80/tcp    mynginx

如果没有启动成功要先用docker ps -a查看失败的容器,并且用docker rm CONTAILNER ID删除容器ID,再查找问题,然后docker run再启动容器,如果在确保挂载的目录和文件没有问题还是不能启动的话,那么就是权限问题了,网上说的就是在docker run后面加个 --privileged=true参数

http://IP 打开网页看看效果把

以上这篇docker部署nginx并且挂载文件夹和文件操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Docker部署nginx实现过程图文详解

    1.下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB [root@localhost my.Shells]# docker pull nginx //下载nginx Using default tag: latest Trying to pull reposit

  • docker nginx实现一个主机部署多个站点操作

    在某站租赁的虚拟机快到期了,续费得花200多,想到在阿里云新买的服务器,不如把这个也转移过去.域名我就用真实的吧,大家别黑我网站就好了,谢谢各位了. 阿里云里面已经用部署了一个站点 用域名 www.dcssn.com 就能直接访问,我的想法是再用 www.xhxf119.com 指向这个主机,根据域名的不同去访问不同的服务. 首先 域名解析都要指向这个主机的ip 然后 www.dcssn.com的服务开启8080端口,docker run -p 8080:80 weian www.xhxf119

  • Docker容器和本机之间的文件传输方法

    主机和容器之间传输文件的话需要用到容器的ID全称. 获取方法如下: 1.先拿到容器的短ID或者指定的name. 2.然后根据这两项的任意一项拿到ID全称. 有了这个长长的ID的话,本机和容器之间的文件传输就简单了. docker cp 本地文件路径 ID全称:容器路径 进入容器之后就能够看到刚才上传进来的文件了. 如果是容器传输文件到本地的话,反过来就好了: docker cp ID全称:容器文件路径 本地路径 进行挂载的话可以参考这篇:利用Volume在主机和Docker容器文件传输. 以上就

  • docker利用单个镜像映射到多个端口操作

    需求: 官网的资源服务器肯定不能使用一个实例, 需要多个一起,但是如果继续复制太慢了,我就想能不能直接使用docker镜像,多制造几个相同的容器实例 前后两个docker-compose.yml version: '3' services: micro-hcnet-website-13: image: 172.18.0.1:5000/hcnet-website-12:0.0.1-SNAPSHOT restart: on-failure ports: - 8311:8211 ~ version:

  • docker部署nginx并且挂载文件夹和文件操作

    这段时间在研究docker,在部署nginx时遇到了坑,最主要的问题是在挂载文件和文件夹的时候不知道怎么挂载,经过反复实验以及查看网上的教程,先总结如下: 1首先pull下载nginx镜像包 docker pull nginx 2(关键)查看nginx镜像里面配置文件.日志等文件的具体位置,只有找到镜像配置文件的路径,后面挂载文件和文件夹才能覆盖这些路径 以终端的方式打开镜像容器 [root@docker2 nginx]# docker run -i -t nginx /bin/bash roo

  • Docker部署Nginx并修改配置文件的两种方式

    目录 一.创建容器 二.修改配置文件 1. 进入容器内部修改配置文件 2.容器加载外部配置文件 总结 一.创建容器 # 搜索Nginx镜像 docker search nginx # 拉取Nginx镜像 docker pull nginx # 查看镜像名为nginx的镜像 docker images nginx # 运行镜像,生成容器 # 命令解读: # -d:以后台守护线程运行 # --name:容器命名 # -p 80:80 : 映射端口,容器内部80端口映射到服务器80端口 # nginx

  • Docker部署Nginx并配置反向代理

    准备工作 在docker内部署任何应用,都需要先下载对应的镜像:下载镜像之前,需要先搜索镜像来确认该镜像是否存在: docker search nginx 从列表可以看到,docker已经有了nginx的镜像,名称是“nginx”,接下来下载镜像: docker pull nginx 下载完成后,查看一下本地镜像: 如果在列表中看到nginx,镜像下载就已经成功了. 容器设置 在docker中,真正运行的是容器,镜像在我理解中是一种环境.我们在指定的镜像中运行某个容器,然后编辑和配置这个容器,从

  • PHP创建/删除/复制文件夹、文件

    学习了PHP的文件编程,其中PHP自身提供了复制文件的函数(copy).自己也写了一个功能差不多的复制图片的函数,以此在这里记录一下. 在说该函数之前,先介绍一下使用PHP创建/删除文件夹.文件等知识. 1.创建文件夹 <?php //使用 file_exists("d:/mydir") 或 is_dir("d:/mydir")判断该文件夹是否存在 if(!file_exists("d:/mydir")) { if(mkdir("

  • Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案

    1.配置web.config <system.web> <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="

  • js/jq仿window文件夹框选操作插件

    0.先给大家看看效果: 1.创建一个index.html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> ul{list-style: none} li{width:200px;margin:10px;float:left;height: 100

  • asp.net编程实现删除文件夹及文件夹下文件的方法

    本文实例讲述了asp.net编程实现删除文件夹及文件夹下文件的方法.分享给大家供大家参考,具体如下: //获取文件夹 string path = Server.MapPath("Image"); //获取文件夹中所有图片 if (Directory.GetFileSystemEntries(path).Length > 0) { //遍历文件夹中所有文件 foreach (string file in Directory.GetFiles(path)) { //文件己存在 if

  • Asp.net获取服务器指定文件夹目录文件并提供下载的方法

    本文实例讲述了Asp.net获取服务器指定文件夹目录文件并提供下载的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: string dirPath = HttpContext.Current.Server.MapPath("uploads/"); if (Directory.Exists(dirPath)) {        //获得目录信息        DirectoryInfo dir = new DirectoryInfo(dirPath);       

  • php定时删除文件夹下文件(清理缓存文件)

    那么有没有方法自动清理临时文件夹呢? 以下代码就是一个简单定时清理文件夹下文件的php代码. ps:这个代码如果不重启网站会一直执行下去,所以只用作于本地环境测试,请别在网站上测试. 复制代码 代码如下: <?php ignore_user_abort(); //客户端断开时,可以让脚本继续在后台执行 set_time_limit(0); //忽略php.ini设置的脚本运行时间限制 $interval = 5*60; //设置执行周期,单位为秒,5分钟为 5*60=300 do{ $dir =

  • PHP遍历文件夹与文件类及处理类用法实例

    本文实例讲述了PHP遍历文件夹与文件类及处理类用法,非常具有实用价值.分享给大家供大家参考.具体方法如下: FindFile.class.php类文件用于遍历目录文件,具体代码如下: <?php /** 遍历文件夹及文件类 * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */ class FindFile{ public $files = array(); // 存储遍历的文件 protected $maxdepth; // 搜寻深度,0表示没有

随机推荐