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能夠下載鏡像
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息