docker Dockerfile

Dockerfile

dockerfile是用來構建docker鏡像的文件,命令和參數組成的腳本。docker

構建步驟

  1. 編寫一個dockerfile文件。名字能夠自定義,推薦「Dockerfile」
  2. docker build 構建鏡像
  3. docker run 構建容器
  4. docker push 發佈鏡像(DockerHub 阿里雲鏡像倉庫)

構建鏡像過程

  1. 每一個指令必須大寫
  2. 執行由上到下
  3. 表示註釋

  4. 每一個指令都是一個新的鏡像層
    dockerfile是面向開發人員的,docker鏡像成爲企業交付標準。

dockerfile經常使用命令

  • FROM 基礎鏡像好比操做系統
  • MAINTAINER 維護者信息姓名email smallForest<<1032817724@qq.com>>
  • RUN 鏡像構建時運行的命令
  • ADD 向鏡像添加文件,會自動解壓tar
  • WORKDIR 指定工做目錄
  • VOLUME 掛載目錄
  • EXPOSE 暴露端口
  • CMD 容器啓動時要運行的命令
  • ENTRYPOINT 容器啓動時要運行的命令,能夠追加命令
  • ONBUILD 當構建一個被繼承的docker,觸發該命令
  • COPY 相似ADD命令,將文件複製到鏡像
  • ENV 構建時設置環境變量

以前用別人的鏡像,之後本身也能夠寫鏡像。vim

實踐出真知

本身作一個鏡像centos

# 查看當前路徑
➜  file pwd
/Users/dada/Downloads/docker-centos/file
➜  file ls
# 建立文件
➜  file touch myDockerfie
# 編輯文件
➜  file vim myDockerfie
# 查看編輯的內容
➜  file cat myDockerfie 
FROM centos
MAINTAINER smallForest<1032817724@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash

# 進行構建鏡像,若是文件名字是Dockerfile,則能夠不用-f 指定文件路徑
➜  file docker build -f myDockerfie -t mycentos03:1.0 .
[+] Building 85.8s (8/8) FINISHED                                                                                                 
 => [internal] load build definition from myDockerfie                                                                        0.0s
 => => transferring dockerfile: 255B                                                                                         0.0s
 => [internal] load .dockerignore                                                                                            0.0s
 => => transferring context: 2B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/centos:latest                                                             0.0s
 => [1/4] FROM docker.io/library/centos                                                                                      0.0s
 => CACHED [2/4] WORKDIR /usr/local                                                                                          0.0s
 => [3/4] RUN yum -y install vim                                                                                            73.6s
 => [4/4] RUN yum -y install net-tools                                                                                      11.3s
 => exporting to image                                                                                                       0.7s 
 => => exporting layers                                                                                                      0.7s 
 => => writing image sha256:13fa3a8f9134f7b7b0e76e2b22ee68d07a09bc1e927365474a66fcc8a3666e6a                                 0.0s 
 => => naming to docker.io/library/mycentos03:1.0                            
# 查看結果
➜  file docker images|grep mycentos03
mycentos03                           1.0                                                     13fa3a8f9134   59 seconds ago   282MB

➜  file docker run -d -it --name mycentos03 mycentos03:1.0
bf6575057cbf0517eeb4ec70d98ef4e232c4246174e4dc15e4f2c1cbd4d917db
➜  file docker ps|grep mycentos03
bf6575057cbf   mycentos03:1.0         "/bin/sh -c /bin/bash"   13 seconds ago   Up 12 seconds   80/tcp                                                 mycentos03
➜  file docker exec -it bf6575057cbf /bin/bash
[root@bf6575057cbf local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src

# 查看工做目錄和設置的/usr/local是否一致
[root@bf6575057cbf local]# pwd
/usr/local
[root@bf6575057cbf local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.6  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:06  txqueuelen 0  (Ethernet)
        RX packets 9  bytes 726 (726.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@bf6575057cbf local]# vim a.sh
[root@bf6575057cbf local]# 
# 運行結果發現,vim ifconfig是能夠使用的,由於yum install進行了安裝
  • docker history 鏡像ID,能夠查看鏡像的構建歷史數組

    ➜  file docker history 13fa3a8f9134
    IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
    13fa3a8f9134   8 minutes ago   CMD ["/bin/sh" "-c" "/bin/bash"]                0B        buildkit.dockerfile.v0
    <missing>      8 minutes ago   CMD ["/bin/sh" "-c" "echo \"----end----\""]     0B        buildkit.dockerfile.v0
    <missing>      8 minutes ago   CMD ["/bin/sh" "-c" "echo $MYPATH"]             0B        buildkit.dockerfile.v0
    <missing>      8 minutes ago   EXPOSE map[80/tcp:{}]                           0B        buildkit.dockerfile.v0
    <missing>      8 minutes ago   RUN /bin/sh -c yum -y install net-tools # bu…   14.4MB    buildkit.dockerfile.v0
    <missing>      8 minutes ago   RUN /bin/sh -c yum -y install vim # buildkit    58.1MB    buildkit.dockerfile.v0
    <missing>      39 hours ago    WORKDIR /usr/local                              0B        buildkit.dockerfile.v0
    <missing>      39 hours ago    ENV MYPATH=/usr/local                           0B        buildkit.dockerfile.v0
    <missing>      39 hours ago    MAINTAINER smallForest<1032817724@qq.com>       0B        buildkit.dockerfile.v0
    <missing>      5 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
    <missing>      5 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
    <missing>      5 months ago    /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7…   209MB
相關文章
相關標籤/搜索