Docker Tips

Gevin發表於2015-11-18

映象

獲取映象

可以使用 docker pull 命令來從倉庫獲取所需要的映象

e.g. sudo docker pull ubuntu:12.04

列出本地映象

sudo docker images

修改已有映象來建立新映象

sudo docker commit -m "change source list" -a "gevin" 0b2616b0e5a8 gevin/ubuntu:v1

-m 來指定提交的說明資訊,跟我們使用的版本控制工具一樣;-a 可以指定更新的使用者資訊;之後是用來建立映象的容器的 ID;最後指定目標映象的倉庫名和 tag 資訊。建立成功後會返回這個映象的 ID 資訊

載入映象

使用 docker load 從匯出的本地檔案中再匯入到本地映象庫,例如

$ sudo docker load --input ubuntu_14.04.tar
或
$ sudo docker load < ubuntu_14.04.tar

匯出映象

使用 docker save 匯出映象到本地,如:

docker save mynewimage_id > /tmp/mynewimage.tar

詳細的匯出方法如下:

The current (and correct) behavior is as follows:

1.

docker save repo

Saves all tagged images + parents in the repo, and creates a repositories file listing the tags

2.

docker save repo:tag

Saves tagged image + parents in repo, and creates a repositories file listing the tag

3.

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)並繫結到容器的標準輸入上, -i 則讓容器的標準輸入保持開啟

守護態執行

讓 Docker 容器在後臺以守護態(Daemonized)形式執行。此時,可以通過新增 -d 引數來實現。

例如下面的命令會在後臺執行容器。

$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

啟動已終止容器

可以利用 docker start 命令,直接將一個已經終止的容器啟動執行。

終止容器

可以使用 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 引數時,容器啟動後會進入後臺。 某些時候需要進入容器進行操作,有很多種方法,包括使用 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 命令有時候並不方便。當多個視窗同時 attach 到同一個容器的時候,所有視窗都會同步顯示。當某個視窗因命令阻塞時,其他視窗也無法執行操作了。

nsenter 命令參考這裡

匯出容器

如果要匯出本地某個容器,可以使用 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

這樣將匯出容器快照到本地檔案。

匯入容器快照

可以使用 docker import 從容器快照檔案中再匯入為映象,例如:

$ 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 或者某個目錄來匯入,例如:

$sudo docker import http://example.com/exampleimage.tgz example/imagerepo

注:轉載本文,請與Gevin聯絡




如果您覺得Gevin的文章有價值,就請Gevin喝杯茶吧!

|

歡迎關注我的微信公眾賬號

Docker Tips