鏡像
獲取鏡像
可以使用 docker pull
命令來從倉庫獲取所需要的鏡像
e.g. sudo docker pull ubuntu:12.04
列出本地鏡像
sudo docker images
修改已有鏡像來創(chuàng)建新鏡像
sudo docker commit -m "change source list" -a "gevin" 0b2616b0e5a8 gevin/ubuntu:v1
-m 來指定提交的說明信息签财,跟我們使用的版本控制工具一樣间唉;-a 可以指定更新的用戶信息;之后是用來創(chuàng)建鏡像的容器的 ID施逾;最后指定目標(biāo)鏡像的倉庫名和 tag 信息敷矫。創(chuàng)建成功后會返回這個鏡像的 ID 信息
載入鏡像
使用 docker load
從導(dǎo)出的本地文件中再導(dǎo)入到本地鏡像庫例获,例如
$ sudo docker load --input ubuntu_14.04.tar
或
$ sudo docker load < ubuntu_14.04.tar
導(dǎo)出鏡像
使用 docker save
導(dǎo)出鏡像到本地,如:
docker save mynewimage_id > /tmp/mynewimage.tar
詳細(xì)的導(dǎo)出方法如下:
The current (and correct) behavior is as follows:
docker save repo
Saves all tagged images + parents in the repo, and creates a repositories file listing the tags
docker save repo:tag
Saves tagged image + parents in repo, and creates a repositories file listing the tag
docker save imageid
Saves image + parents, does not create repositories file. The save relates to the image only, and tags are left out by design and left as an exercise for the user to populate based on their own naming convention.
移除本地鏡像
如果要移除本地的鏡像曹仗,可以使用 docker rmi
命令榨汤。注意 docker rm 命令是移除容器。
$ sudo docker rmi training/sinatra
容器
啟動容器
新建并啟動
所需要的命令主要為 docker run
交互式的容器:
$ sudo docker run -t -i ubuntu:14.04 /bin/bash
root@af8bae53bdd3:/#
-t
選項讓Docker分配一個偽終端(pseudo-tty)并綁定到容器的標(biāo)準(zhǔn)輸入上怎茫, -i
則讓容器的標(biāo)準(zhǔn)輸入保持打開
守護態(tài)運行
讓 Docker 容器在后臺以守護態(tài)(Daemonized)形式運行收壕。此時,可以通過添加 -d 參數(shù)來實現(xiàn)轨蛤。
例如下面的命令會在后臺運行容器蜜宪。
$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
啟動已終止容器
可以利用 docker start 命令,直接將一個已經(jīng)終止的容器啟動運行祥山。
終止容器
可以使用 docker stop
來終止一個運行中的容器
查看容器信息
通過 docker ps
命令來查看容器信息圃验。
sudo docker ps
or
sudo docker ps -a
獲取容器的輸出信息
要獲取容器的輸出信息,可以通過 docker logs
命令
$ sudo docker logs container_name
hello world
hello world
hello world
. . .
進入容器
在使用 -d 參數(shù)時缝呕,容器啟動后會進入后臺澳窑。 某些時候需要進入容器進行操作,有很多種方法供常,包括使用 docker attach 命令或 nsenter 工具等摊聋。
attach 命令
docker attach 是Docker自帶的命令。下面示例如何使用該命令话侧。
$ sudo docker run -idt ubuntu
243c32535da7d142fb0e6df616a3c3ada0b8ab417937c853a9e1c251f499f550
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
243c32535da7 ubuntu:latest "/bin/bash" 18 seconds ago Up 17 seconds nostalgi_hypatia
$sudo docker attach nostalgic_hypatia
root@243c32535da7:/#
但是使用 attach 命令有時候并不方便。當(dāng)多個窗口同時 attach 到同一個容器的時候闯参,所有窗口都會同步顯示瞻鹏。當(dāng)某個窗口因命令阻塞時,其他窗口也無法執(zhí)行操作了。
nsenter 命令參考這里
導(dǎo)出容器
如果要導(dǎo)出本地某個容器鹿寨,可以使用 docker export 命令新博。
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7691a814370e ubuntu:14.04 "/bin/bash" 36 hours ago Exited (0) 21 hours ago test
$ sudo docker export 7691a814370e > ubuntu.tar
這樣將導(dǎo)出容器快照到本地文件。
導(dǎo)入容器快照
可以使用 docker import 從容器快照文件中再導(dǎo)入為鏡像脚草,例如
$ cat ubuntu.tar | sudo docker import - test/buntu:v1.0
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test/ubuntu v1.0 9d37a6082e97 About a minute ago 171.3 MB
此外赫悄,也可以通過指定 URL 或者某個目錄來導(dǎo)入,例如:
$sudo docker import http://example.com/exampleimage.tgz example/imagerepo