使用 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_copy 5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB
推薦使用 openVZ 提供的預建範本:OpenVZ templates
cat centos-7-x86_64-minimal.tar.gz | docker import - centos:latest
後續文章會詳細說明。
docker images docker save -o centos_7.1.tar centos:7.1.1503 docker load --input centos_7.1.tar docker load < centos_7.1.tar
# docker run centos echo "hello world" Unable to find image 'centos:latest' locally latest: Pulling from centos 47d44cb6f252: Pull complete 168a69b62202: Pull complete 812e9d9d677f: Pull complete 4234bfdd88f8: Pull complete ce20c473cd8a: Pull complete centos:latest: The image you are pulling has been verified. Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5 Status: Downloaded newer image for centos:latest hello world
說明:docker run 是標準的容器啟動指令,centos 是映像檔名稱(預設 tag 為 latest),echo 及其後內容是容器啟動後要執行的命令。
docker run -it centos /bin/bash
-t 指定偽終端,-i 允許對容器的 STDIN 做互動。
前面兩個例子,一個執行完 echo 就退出,一個在使用者離開 bash 後退出,都不適合長期運行的服務。docker run 的 -d 參數可以把容器以背景方式啟動:
docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; done" 179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33
這串長字串是容器 ID,為容器的唯一識別,可用來看日誌、停止或刪除容器:
dock logs 179fc7f17c358834364d
之所以用無窮迴圈,是因為容器唯一的程序若結束,容器也就停了。因此要在容器中跑服務,該服務本身必須以前台方式運行。
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] -d 以背景方式運行 -t 提供偽終端 -i 提供互動輸入(通常與 -t 合用,只給 -i 會無法退出) -v 映射 volume,如 -v /data/www:/var/www/html -p 映射埠,如 -p 8080:80
docker images 列出本地映像檔 docker search centos 搜尋映像檔 docker pull centos 下載映像檔 docker create -it ubuntu:latest 建立容器(處於停止狀態,需 docker start 啟動) docker ps -a 列出所有容器 docker run ubuntu /bin/echo "hello world"
docker run 等同先 create 再 start。
方法一:docker attach a54615a88787(容器名或 ID,退出後容器也退出,不常用)
方法二:docker exec -it a54615a88787 /bin/bash(推薦)
方法三:
yum -y install util-linux
docker inspect --format "{{.State.Pid}}" stupefied_cray
4899
nsenter --target 4899 --mount --uts --ipc --net --pid
可寫成腳本方便重複使用:
#!/bin/bash
CNAME=$1
CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)
nsenter --target $CPID --mount --uts --ipc --net --pid