docker基本概念
Docker 是一個開源的應用容器引擎,讓開發(fā)者可以打包他們的應用以及依賴包到一個可移植的容器中,然后發(fā)布到任何流行的 Linux 機器上。
Docker是一個重新定義了程序開發(fā)測試、交付和部署過程的開放平臺,Docker則可以稱為構建一次,到處運行,這就是docker提出的“Build once,Run anywhere”
創(chuàng)建鏡像
創(chuàng)建鏡像的方法有三種:
基于已有的容器創(chuàng)建
基于本地模板導入
基于dockerfile
基于已有的容器創(chuàng)建
主要使用docker commit 命令,命令格式:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:tag]],主要包括:
-a ,--author="" 作者信息
-m,--message=""提交消息-p,--pause=true 提交時暫停容器例如:
# docker run -it centos /bin/bash[root@d7e7ac1cbca2 /]# touch test[root@d7e7ac1cbca2 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var# docker commit -m "add a file" -a "kafeikele" de6 centos_copy5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB
基于本地模板導入
推薦使用openVZ提供的模板來創(chuàng)建
https://openvz.org/Download/templates/precreated#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest
存出和導入鏡像
# docker imagescentos 7.1.1503 47a77536ad4c 8 weeks ago 212.1 MB# docker save -o centos_7.1.tar centos:7.1.1503 # docker load --input centos_7.1.tar# docker load < centos_7.1.tar
基于dockerfile
之后的內容詳細介紹
運行第一個docker容器
# docker run centos echo "hello world"Unable to find image 'centos:latest' locallylatest: Pulling from centos47d44cb6f252: Pull complete168a69b62202: Pull complete812e9d9d677f: Pull complete4234bfdd88f8: Pull completece20c473cd8a: Pull completecentos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to providesecurity.Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5Status: Downloaded newer image for centos:latesthello world
命令說明:
docker run :標準容器啟動命令
centos: 鏡像名稱,默認是latest
echo和后面的內容:容器啟動后執(zhí)行的命令
啟動一個交互式容器
docker run -it centos /bin/bash
*注:-t標示在心容器內指定一個偽終端或終端,-i標示允許我們對容器內的STDIN進行交互
以服務方式啟動一個docker容器
如果你實際測試,估計也發(fā)現了,第一個“hello world”容器啟動后執(zhí)行完echo命令就退出了,而第二個交互式的容器,只要用戶退出當前容器的bash,容器也退出了。這明顯不能滿足一個服務長時間運行的要求,好找docker run提供了‘-d'參數,可以實現將容器以守護進程方式啟動。
docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; <br>179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33
新聞熱點
疑難解答