Docker数据管理 手动构建镜像

数据卷

  1. docker run -it --name volume-test1 -h nginx -v /data centos
    说明: -i 终端保持打开 -h 指定主机名为nginx   , --name: 指定容器名称 -v 指定 数据卷名称 /data 


    挂载了一个 /data 目录
    Docker 直接将 /data 目录挂载到了物理主机上
  2. 查看 volume-test1 容器信息,挂载到物理机的什么位置上
    方式一 : docker inspect -f {{.Volumes}} volume-test1 ,改成 .Config.Volumes 依旧查询不到。

    直接看容器的所有信息 再grep
  3. 物理机上创建文件,可以映射到 容器中。
  4.  指定一个目录,挂载到容器里面
    docker run -it --name volume-test2 -h nginx -v /opt:/opt centos 将物理主机上的 /opt 挂载到 容器上的 /opt目录

数据卷容器

  1. 启动一个容器数据卷来源于另外的一个容器 (源容器为volume-test1 目标容器为 volume-test4 )
    docker run -it --name volume-test4 --volumes-from volume-test1 centos  
      
    这里启动的volume-test4里就有之前在 volume-test1 容器里使用物理映射创建的 haha文件。

Docker容器手动构建 nginx

  1.  首先需要启动一个centos的镜像容器
    docker run --name nginx-man -it centos

  2. 在centos镜像中,安装常用工具  yum install -y wget gcc gcc-c++ make openssl-devel

  3. 安装nginx   wget http://nginx.org/download/nginx-1.18.0.tar.gz

  4. 安装pcre  wget ftp://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz (以命令这个为准。)

  5. 创建www用户,/sbin/nologin 不登录 , -M 不创建目录  useradd -s /sbin/nologin -M www

  6. 安装 配置nginx  ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42

  7. 编译 nginx目录下执行 make

  8. 安装 make install

  9. 安装成功后,将nginx放入到 /etc/rc.local 下,使得容器每次启动,nginx自动起来 , 配置  /usr/local/nginx/sbin/nginx

  10. 指定nginx配置,后台启动  /usr/local/nginx/conf/nginx.conf 加上 daemon off; 

  11. docker commit -m "my nginx" 98fe081a457e xiaoxu/my-nginx:v2 生成镜像

  12. docker run -d -p 8088:80 xiaoxu/my-nginx:v2 /usr/local/nginx/sbin/nginx   启动镜像并指定nginx位置 物理主机 端口映射

  13.  效果图