dockerfile

nginx容器html

修改符合需求的配置文件:
方式一:
    启动镜像
    exec container 
    vim nginx.conf
    reload
方式二:
    把配置文件作成存储卷
    坏处:在宿主机修改配置文件后,能不能直接生效?发现有些配置还要重启才能生效


方式三:
    制做镜像
    
    1、基于容器:配置文件仍是写死在了镜像里面
    2、

server.conf            /etc/nginx/conf.d/
{
    server_name $NGX_SERVER_NAME;
    listen    $NGX_IP:$NGINX_PORT;
    root    $DOC_ROOT;

    
}

 

在容器内的主进程启动以前,先启动一个别的程序(根据镜像中的配置文件,以及容器启动时传入的参数)
将用户传入的值替换配置文件中的变量,并保存,
再由主进程顶了当前进程(PID号一致),此进程关闭



临时主进程(预设环境的)



未来基于一个镜像,启动多个容器,让多个容器拥有不一样配置的作法就是向其传递变量,(从环境变量获取值)

 

dockerfile
一个 从文本文件 包含制做镜像支持的命令

Format
    # 注释
    INSTRUCTION   arguments
指令大小写不敏感,但易于区分使用大写
顺序执行 第一个指令必须是  'FROM'        # 基于某个镜像


dockerfile制做流程
1、专用目录,放置Dockerfile文件,首字母大写
2、Dockerfile引用的文件,都必须在这个目录下

3、还支持一个隐藏文件        dockerignore,    写在这里的文件路径,打包时都不会包含包括dockerignore文件

基于Dockerfile    使用docker build命令

 

 

 

docker   指令

FROM指令是最重的一个且必须为Dockerfile文件开篇的第一个非注释行,用于为影像文件构建过程指定基准镜像,后续的指令运行于此基准镜像所提供的运行环境中。
实践中,基准镜像能够是任何可用镜像文件,默认状况下,docker build会在docker主机上查找指定的镜像文件,再其不存在时,则会从Docker Hub Registry上拉取所须要的镜像文件(若是找不到指定的镜像文件,则会返回一个错误信息)
    语法:
        FROM <repository>[:<tag>]或
        FROM <resository>@<digest>        
            #     指定哈希码,安全;建议使用


MAINTANIER (depreacted)
    用于让Dockerfile制做者提供本人的详细信息
    Dockerfile 并不限制MAINTANINER指令可在出现的位置,但推荐将其放置于FROM指令以后
    语法:
        MAINTANIER "XXXXXXX<xxzc@asdasd.com>"
    

depreacted  提供更多信息
    key-value
    语法:
        LABEL <key>=<value><key>=<value><key>=<value>...


COPY
    用于从宿主机复制文件至建立的新镜像文件
    
    语法:
        COPY  <src> ... <dest> 或
        COPY  ["<src>", ... "<dest>"]
        src  通常相对路径
        dest 建议使用绝对路径,不然COPY指定则以当前WORKDIR为其起始路径
        注意:路径中有空白字符建议使用第二种
        
    文件复制准则
        <src>必须是build上下文中的路径,不能是其父目录中的文件
        若是<src>是目录,则其内部文件或目录会被递归复制,但<src>目录自身不会被复制
        若是指定了多个<src>,或在<src>中使用了通配符,则<dest>必须是一个目录,且必须以/结尾
        若是<dest>事先不存在,他将会被自动建立,这包括其父目录路径
ADD:
    ADD指令相似于COPY指令,ADD支持使用tar文件和url路径
    语法:
        ADD <src> ... <dest> 
        ADD ["<src>" ,... "<dest>"] 
         
    注意:
        若是src为url,且dest不以/结尾,则src指定的文件将被下载并直接被建立为dest;若是以/结果则文件名url指定的文件将被直接下载并保存为<dest>/<filename>
        若是src是一个本地系统上的压缩格式的tar文件,将会被展开为一个目录,其行为相似于tar -x 命令;然而,经过url获取到的tar文件将不会自动展开
        若是src有多个,或其间接或直接使用了通配符,则<dest>必须是一个以/结尾的目录路径;若是<dest>不以/结尾,则其被视为一个普通文件,<src>的内容被直接写入到<dest>;
    
 
 

 

 [root@localhost imgl]# docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -            
# PATH默认当前路径
Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair
                                Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair
                                Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is
                                'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap:
                                '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN
                                instructions during build (default
                                "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version
                                of the image
  -q, --quiet                   Suppress the build output and print
                                image ID on success
      --rm                      Remove intermediate containers after a
                                successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the
                                'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])


[root@localhost imgl]# tree ./
./
├── Dockerfile
└── index.html

0 directories, 2 files

[root@localhost imgl]# cat Dockerfile
# Description  : test img
FROM busybox:latest
MAINTAINER "ace <1214972346@qq.com>"
# LABEL  MAINTAINER="ace <1214972346@qq.com>"
COPY index.html /data/web/html/

[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# docker build -t first_image:v0.0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/3 : MAINTAINER "ace <1214972346@qq.com>"
 ---> Running in f73d4aec0494
Removing intermediate container f73d4aec0494
 ---> 3827a178aa69
Step 3/3 : COPY index.html /data/web/html/
 ---> e4a919420266
Successfully built e4a919420266
Successfully tagged first_image:v0.0.1
[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
first_image         v0.0.1              e4a919420266        19 seconds ago      1.15MB
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# docker build -t first_image:v0.0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/3 : MAINTAINER "ace <1214972346@qq.com>"
 ---> Running in f73d4aec0494
Removing intermediate container f73d4aec0494
 ---> 3827a178aa69
Step 3/3 : COPY index.html /data/web/html/
 ---> e4a919420266
Successfully built e4a919420266
Successfully tagged first_image:v0.0.1
[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
first_image         v0.0.1              e4a919420266        19 seconds ago      1.15MB
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
[root@localhost imgl]#

[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# ls
Dockerfile  index.html
[root@localhost imgl]# docker build -t first_image:v0.0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 59788edf1f3e
Step 2/3 : MAINTAINER "ace <1214972346@qq.com>"
 ---> Running in f73d4aec0494
Removing intermediate container f73d4aec0494
 ---> 3827a178aa69
Step 3/3 : COPY index.html /data/web/html/
 ---> e4a919420266
Successfully built e4a919420266
Successfully tagged first_image:v0.0.1
[root@localhost imgl]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
first_image         v0.0.1              e4a919420266        19 seconds ago      1.15MB
redis               latest              ce25c7293564        10 days ago         94.9MB
nginx               latest              568c4670fa80        3 weeks ago         109MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB
 
 

[root@localhost imgl]# docker run --rm --name t1 first_image:v0.0.1 cat /data/web/html/index.html
<h1>hello world </h1>
    
VOLUME

    用于在image中建立一个挂载点目录,以挂载Docker host 上的卷或其余容器上的卷
    语法:
        VOLUME <mountpint>或
        VOLUME ["<mountpint>"]
        若是挂载点目录路径下此前在文件存在,docker run命令会在此卷挂载完后将此前的全部文件复制到新挂载的卷中
        
    只能是docker管理的卷,不能是宿主机的目录
    

EXPOSE
    用于为容器打开指定监听的端口以实现与外部通讯
    语法:
        EXPOSE <port>[/<protocol>][<port>[/protcol]...]
        EXPOSE 11211/tcp    11211/udp    
    用户与标明这个镜像中的应用将会真挺某个端口,而且新但愿将这个端口映射到主机的网络界面上,可是为了安全,docker run 若是没有带上响应的端口映射参数,Docker并不会讲端口映射出去
    此外expose端口是能够在多个容器之间通讯用(links),经过--links参数可让多个容器经过端口链接在一块儿






 
ENV
    用于为镜像定义所须要的环境变量,并可被Dockerfile文件中的位于其后的其余指令如(ENV、ADD、COPY)所调用;相似于linux的export命令
    调用格式 
        $variable_name
        ${variable_name} 
        ${variable:-word}                #  未设置或为空设置默认值word (字符串)
        ${variable:+word}                #  有值显现word   没值返回空
    语法:
        ENV <key> <value>或
        ENV <key>=<value> \
            <key>=<value> \
            <key>=<value>
        
    docker run  和 docker build  均可以穿变量
    这是两个不一样的过程
    在dockerfile的值在 build 时已经作成镜像了,已经成为事实了,
    在run的时候,只是显示为当前的,并不会从新配置
        经过 -e WEB_SERVER_PACKAGE="nginx-1.15.1"   传参

RUN    
    用于指定docker build过程当中运行的程序,其能够是任何命令
    会在shell或exec环境中执行命令
    语法:
        RUN <COMMAND>              # shell
        RUN ["<executable>","<param1>","<param2>"]            # exec    
        
    第一种格式中,command 一般是一个shell命令,且以‘、bin/sh -c ’来运行它,这意味着此进程在容器中的PID不为1,不能接受Unix信号,所以当使用docker stop <container>来中止容器时,此进程接收不到SIGTERM信号;
    第二种语法格式中的参数是一个json格式的数组,其中<executable>为要运行的命令,后面的<param>为传递给命令的选项或参数,然而,此种格式指定的命令不会以’/bin/sh -c‘ 来发起,所以常见的shell操做如变量替换以及通配符等将不会进行;不过若是要运行的命令依赖于此shell特性的话,能够将其替换为相似于下面的格式
    exec不会触发shell,因此$HOME这样的环境变量没法使用,可是他能够在没有bash的镜像中执行,并且能够避免错误的解析命令行字符串
    例子:RUN ["/bin/bash","-c","<executable>","<param1>"]
        







    
    
CMD

    相似于run命令,cmd指令也可用于运行任何命令或应用程序,不过,两者的运行时间不一样
        run指令运行于镜像文件的构建过程当中,而cmd运行于基于Dockerfile构建出的新镜像文件启动一个容器时
        cmd指令的首要目的在于启动的容器指定默认要运行的程序,且其运行结束后,容器也将终止;不过cmd指令的命令其能够被docker run的命令行选项所覆盖
    语法:
        CMD <command>或
        CMD ["<executable>","<param1>","<param2>"]或
        CMD ["<param1>","<param2>"]
        前面两种语法格式的意义同RUN
        第三种则用于为ENTRYPOINT指令提供默认参数

    注意:
        docker run 命令能够覆盖CMD命令,它与entrypoint功能相似,
        区别;
            1、若是docker run 后面出现与cmd指定的相同的命令,那么cmd会被覆盖;而entrypoint会把容器名后面的全部内容都当成参数传递给其指定的命令(不会覆盖)
            2、cmd指令还能够单独做为entrypoint指令的可选参数,共同完成一条命令
    
FROM centos
RUN yum -y install epel-release && yum makecache && yum install nginx && yum clean

 

  

 

 

CMD以默认参数传给ENTRYPOINT
只有--entrypoint 定义,才会覆盖Entrypoint

 

 

 

一、灵活的传参,被shell解析
二、容器接受配置,须要环境变量
Vim Dockerfile
FROM nginx:1.14-alpine
LABEL maintainer=”xxx<d@qq.com>”
ENV  NGX_DOC_ROOT=’/data/web/html/’
ADD  entrypoint.sh   /bin/
ADD  index.html  ${NGX_DOC_ROOT}
EXPOSE  80/tcp
HEALTHCHECK  --start-period=3s    CMD wget -o  -  -q  http://${IP:-0.0.0.0}:${PORT}/ 
CMD  [‘/usr/sbin/nginx’, ’-g’, ‘daemon off;’ ]
ENTRYPOINT [‘/bin/entrypoint.sh’,]
Vim  entrypoint.sh
#!/bin/sh
cat  >   /etc/nginx/conf.d/www.conf << EOF
Server {
server_name  ${}HOSTNAME};
listen ${IP:-0.0.0.0}:${PORT:-80};
root ${NGX_DOC_ROOT:-/usr/share/nginx/html};
}
EOF
exec “$@”
#nginx 再也不是子进程,它的pid为1
docker  build  -t  myweb:v0.1  ./
docker run --name myweb1 --rm  -P -e  “port=8080”  IPmyweb:v0.1

Nginx可以接受变量生成配置文件,能在启动容器的时候进行传递

  

FROM scratch #制做base image 基础镜像,尽可能使用官方的image做为base image


FROM centos #使用base image

FROM ubuntu:14.04 #带有tag的base image


#至关于代码注释,告诉别人,你的镜像文件的信息是什么
LABEL version=“1.0” #容器元信息,帮助信息,Metadata,相似于代码注释
#定义一个dockerfile的做者信息
LABEL maintainer="wupeiqidsb"


#开始定制本身的镜像需求
#对于复杂的RUN命令,避免无用的分层,多条命令用反斜线换行,合成一条命令!
RUN yum update && yum install -y vim \
    Python-dev #反斜线换行
#RUN指令,至关于在centos中执行了一堆命令
RUN hostnamectl set-hostname mydocker
RUN yum install redis -y 
	
#写上3条就会执行者3条


WORKDIR /root 					#至关于linux的cd命令,改变目录,尽可能使用绝对路径!!!不要用RUN cd
WORKDIR /test						 #若是没有就自动建立
WORKDIR demo						 #再进入demo文件夹
WORKDIR s14
WORKDIR /opt 

RUN pwd      /opt 


#ADD和COPY
#宿主机linux有本身的磁盘,文件夹
#容器空间 也有本身的文件夹

#咱们使用docker必定是想将宿主机的文件,添加到容器中

#ADD就是添加宿主机的文件,到容器当中
#ADD还有一个解压缩的功能

# /opt 
ADD and COPY 
#把宿主机的hello文件,放入到容器的 / 根目录下
# 这个hello文件的相对路径,是以Dockerfile文件所在目录为相对路径

ADD hello /  #把本地文件添加到镜像中,吧本地的hello可执行文件拷贝到镜像的/目录

#把与dockerfile同级的一个test.tar.gz压缩文件,拷贝添加到容器的 根 / 目录中,而且解压缩
#  远程传输 而且  tar -zxvf  
ADD test.tar.gz /  #添加到根目录并解压  
 

WORKDIR /root  #切换工做目录到 /root 

#把dockerfile同级的那个hello文件 拷贝到容器的/root/test/hello 

ADD hello test/ #进入/root/ 添加hello可执行命令到test目录下,也就是/root/test/hello 一个绝对路径

COPY hello test/  #等同于上述ADD效果



ADD与COPY
    -ADD除了COPY功能还有解压功能
	
添加远程文件/目录使用curl或wget



ENV #环境变量,尽量使用ENV增长可维护性
ENV MYSQL_VERSION 5.6 #设置一个mysql常量
RUN yum install -y mysql-server=“${MYSQL_VERSION}” 


RUN ./cofigure  --prefix=/opt/
RUN  make&& make install 

dockerfile实验

 

from flask import Flask
app=Flask(__name__)
@app.route('/')
def hello():
    return "hello docker,i am  sbwueiqi, i am in s14 "
if __name__=="__main__":
    app.run(host='0.0.0.0',port=8080)
1.准备一个flask代码,准备python代码
[root@node1 /data/mydocker 10:33:53]#ls
CentOS-Base.repo  Dockerfile  epel.repo  myflask.py

cat Dockerfile
    FROM centos        
    LABEL maintainer="Chao Yu<yc_uuu@163.com>"
    ADD CentOS-Base.repo /etc/yum.repos.d/
    ADD epel.repo /etc/yum.repos.d/
    RUN yum clean all
    RUN yum install python-pip -y
    RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
    COPY myflask.py /app/
    WORKDIR /app
    EXPOSE 8080
    CMD ["python","myflask.py"]
2.准备Dockerfile,准备好全部须要的文件
docker build -t yuchao163/s14-flask-docker .

#构建当前目录的Dcokerfile,而后构建出一个名为yuchao163/s14-flask-docker 这个的镜像文件
-t  tag参数,给镜像加上标记名
dockerhub帐户名:yuchao163

dockerhub帐户名/镜像名   #是为了后面讲docker镜像,推送到dockerhub
3.构建docker镜像文件
docker images 
4.查看镜像是否构建完成
docker run -p 9000:8080   -d 43d
-p  映射9000端口到容器的8080
-d 后台运行 
43d  镜像id
5.运行这个镜像文件,产生容器实例
docker ps 
6.查看已经运行的docker实例
1.我能够先下载其余人的docker镜像

docker pull yuchao163/hello-world-docker

2.上传本身的docker镜像 
    docker login  #登陆本身的docker hub帐号 ,输入密码 
    #docker就会有你本身的dockerhub帐号信息  yuchao163
    
3.更改docker镜像的名字,也就是加上一个tag标记
    docker tag s14/centos-vim  yuchao163/s14-centos-vim
    docker tag  现有镜像名字     dockerhub帐户名/新的镜像名
4.登陆后能够推送镜像文件,此时推送给的是本身的yuchao163帐号仓库下
    docker push yuchao163/s14-hello-docker 
    
5.登陆https://hub.docker.com/查看本身推送的公网镜像
7.学习dockerhub,发布本身的docker镜像
1.下载docker官方提供的私有仓库镜像
docker pull  registry
2.查看镜像
docker images 
3.启动一个私有仓库容器
    docker run -d \
        -p 5000:5000 \
        -v /opt/data/registry:/var/lib/registry \
        registry
4.此时能够检查容器进程
docker ps 
5.此时私有仓库就打开了5000端口,经过端口映射,访问宿主机的5000端口,查看是否通讯
yum install telnet -y 

telnet 127.0.0.1 5000   #检测5000端口是否通讯

6.修改本地镜像的tag标签,标注我要往哪push镜像
docker tag docker.io/hello-world  192.168.12.96:5000/s14-hello

7.修改docker配置,容许非安全的传输方式

    1.vim /etc/docker/daemon.json,写入信息,私有仓库地址,都得改为本身的
        {"registry-mirrors": ["http://95822026.m.daocloud.io"],
        "insecure-registries":["192.168.12.96:5000"]
        }
    2.修改docker.server 
        vim /lib/systemd/system/docker.service
        #写入以下信息,请在[service]中写入        
        [Service]
        EnvironmentFile=/etc/docker/daemon.json
    
    
    
8.重启docker服务,使得生效
systemctl daemon-reload  #从新加载docker配置文件

systemctl restart docker #重启docker服务 

9. #重启docker服务,会中止容器,所以要再次启动

    docker ps -a 
    docker start b23bcfe42e80  #启动这个私有仓库容器

10.推送本地镜像到 私有仓库 192.168.12.96:5000
    docker push 192.168.12.96:5000/s14-hello

11.此时访问api接口,查看私有仓库的数据
http://192.168.12.96:5000/v2/_catalog
8.构建公司私有docker仓库,其实就是搭建一个api服务器,经过api能够下载镜像
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息