Docker 基本命令

docker的基本命令

docker version :查看docker的版本號(hào),包括客戶(hù)端、服務(wù)端、依賴(lài)的Go等

[root@centos7 ~]# docker version
Client:
 Version:      1.8.2-el7.centos
 API version:  1.20
 Package Version: docker-1.8.2-10.el7.centos.x86_64
 Go version:   go1.4.2
 Git commit:   a01dc02/1.8.2
 Built:        
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.2-el7.centos
 API version:  1.20
 Package Version: 
 Go version:   go1.4.2
 Git commit:   a01dc02/1.8.2
 Built:        
 OS/Arch:      linux/amd64

docker info:查看系統(tǒng)(docker)層面信息,包括管理的images, containers數(shù)等

[root@centos7 ~]# docker info
Containers: 1
Images: 4
Storage Driver: devicemapper
 Pool Name: docker-8:3-36786088-pool
 Pool Blocksize: 65.54 kB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 2.059 GB
 Data Space Total: 107.4 GB
 Data Space Available: 12.93 GB
 Metadata Space Used: 1.765 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.146 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2015-10-14)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 1
Total Memory: 977.9 MiB
Name: centos7
ID: BUKD:MUW2:5X2D:G7BF:6Y7G:SKIH:LD6K:VUAC:3QA4:JY5C:S3DG:LFT2
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

search 搜索鏡像

[root@centos7 ~]# docker search ubuntu12.10
INDEX       NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/chug/ubuntu12.10x32         Ubuntu Quantal Quetzal 12.10 32bit  base i...   0                    
docker.io   docker.io/chug/ubuntu12.10x64         Ubuntu Quantal Quetzal 12.10 64bit  base i...   0                    
docker.io   docker.io/marcgibbons/ubuntu12.10                                                     0                    
docker.io   docker.io/mirolin/ubuntu12.10                                                         0                    
docker.io   docker.io/mirolin/ubuntu12.10_redis                                                   0         

pull 下載鏡像

[root@centos7 ~]# docker pull ubuntu

run 使用鏡像創(chuàng)建容器

[root@centos7 ~]# docker run ubuntu /bin/echo hello world

run 創(chuàng)建容器,并交互式的運(yùn)行
這里會(huì)創(chuàng)建一個(gè)新的容器。

[root@centos7 ~]# docker run -i -t ubuntu /bin/bash
root@c43c7d102baa:/# cat /etc/issue
Ubuntu 14.04.3 LTS \n \l
# -t 選項(xiàng)讓Docker分配一個(gè)偽終端(pseudo-tty)并綁定到容器的標(biāo)準(zhǔn)輸入上, -i 則讓容器的標(biāo)準(zhǔn)輸入保持打開(kāi)

當(dāng)利用 docker run 來(lái)創(chuàng)建容器時(shí),Docker 在后臺(tái)運(yùn)行的標(biāo)準(zhǔn)操作包括:

  • 檢查本地是否存在指定的鏡像,不存在就從公有倉(cāng)庫(kù)下載
  • 利用鏡像創(chuàng)建并啟動(dòng)一個(gè)容器
  • 分配一個(gè)文件系統(tǒng),并在只讀的鏡像層外面掛載一層可讀寫(xiě)層
  • 從宿主主機(jī)配置的網(wǎng)橋接口中橋接一個(gè)虛擬接口到容器中去
  • 從地址池配置一個(gè) ip 地址給容器
  • 執(zhí)行用戶(hù)指定的應(yīng)用程序
  • 執(zhí)行完畢后容器被終止

run -d 守護(hù)態(tài)運(yùn)行
更多的時(shí)候,需要讓 Docker 容器在后臺(tái)以守護(hù)態(tài)(Daemonized)形式運(yùn)行。此時(shí),可以通過(guò)添加 -d 參數(shù)來(lái)實(shí)現(xiàn)。
例如下面的命令會(huì)在后臺(tái)運(yùn)行容器。

[root@centos7 ~]# docker run -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"

logs 查看容器的運(yùn)行

以上個(gè)例子為前導(dǎo)。

[root@centos7 ~]# docker logs 4f34f95b6abc
hello world
hello world
hello world
hello world
hello world
hello world
hello world

ps 查看容器

[root@centos7 ~]# docker ps -h

Usage:  docker ps [OPTIONS]

List containers

  -a, --all=false       Show all containers (default shows just running)
  --before=             Show only container created before Id or Name
  -f, --filter=[]       Filter output based on conditions provided
  --format=             Pretty-print containers using a Go template
  --help=false          Print usage
  -l, --latest=false    Show the latest created container, include non-running
  -n=-1                 Show n last created containers, include non-running
  --no-trunc=false      Don't truncate output
  -q, --quiet=false     Only 
 numeric IDs
  -s, --size=false      Display total file sizes
  --since=              Show created since Id or Name, include non-running

attach 連接已經(jīng)啟動(dòng)的容器 / start -i 啟動(dòng)并連接容器

[root@centos7 ~]# docker ps -a  #查看容器ID
[root@centos7 ~]# docker start <CONTAINER ID>   #啟動(dòng)容器
[root@centos7 ~]# docker attach <CONTAINER ID>  #連接容器,該容器必須是啟動(dòng)狀態(tài)
或者
[root@centos7 ~]# docker start -i <CONTAINER ID>        #啟動(dòng)并連接容器

:但是使用 attach 命令有時(shí)候并不方便。當(dāng)多個(gè)窗口同時(shí) attach 到同一個(gè)容器的時(shí)候,所有窗口都會(huì)同步顯示。當(dāng)某個(gè)窗口因命令阻塞時(shí),其他窗口也無(wú)法執(zhí)行操作了。

commit 將容器的狀態(tài)保存為鏡像

[root@centos7 ~]# docker commit c43c7d102baa ubhttp
d47bbf8e50bace073de2b256b0360cfab029c11881f0d361fce7ae7464aa40ff
[root@centos7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubhttp              latest              d47bbf8e50ba        54 seconds ago      248 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
## 更為標(biāo)準(zhǔn)點(diǎn)的如下:
$ sudo docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2
其中,-m 來(lái)指定提交的說(shuō)明信息,跟我們使用的版本控制工具一樣;-a 可以指定更新的用戶(hù)信息;之后是用來(lái)創(chuàng)建鏡像的容器的 ID;最后指定目標(biāo)鏡像的倉(cāng)庫(kù)名和 tag 信息。創(chuàng)建成功后會(huì)返回這個(gè)鏡像的 ID 信息。

diff 命令查看容器內(nèi)的文件變化

它可以列出容器內(nèi)發(fā)生變化的文件和目錄。這些變化包括添加(A-add)、刪除(D-delete)、修改(C-change)

[root@centos7 ~]# docker diff c43c7d102baa

cp 命令拷貝文件

#從docker中往本地拷貝文件
[root@centos7 ~]# docker cp c43c7d102baa:/var/www/html/index.html /opt/   
[root@centos7 ~]# ls /opt/
index.html  rh
# 從本地往docker中拷貝文件
[root@centos7 ~]# docker cp aa c43c7d102baa:/var
[root@centos7 ~]# docker start -i c43c7d102baa
root@c43c7d102baa:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@c43c7d102baa:/# ls var/
aa  backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp  www

inspect 收集有關(guān)容器和鏡像的底層信息

Docker inspect命令可以收集有關(guān)容器和鏡像的底層信息。這些信息包括:

  • 容器實(shí)例的IP地址
  • 端口綁定列表
  • 特定端口映射的搜索
  • 收集配置的詳細(xì)信息

語(yǔ)法:

docker inspect container/image

kill 命令發(fā)送sigkill信號(hào)停止容器的主進(jìn)程

語(yǔ)法:

docker kill [options] <container_id>

rmi 移除一個(gè)或多個(gè)鏡像

docker rmi <image_id>
#注意:在刪除鏡像之前要先用 docker rm 刪掉依賴(lài)于這個(gè)鏡像的所有容器

wait 阻塞對(duì)指定容器的其它調(diào)用方法,直到容器停止后退出阻塞

docker wait <container_id>

tag 修改鏡像的標(biāo)簽

[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              f59c7e5b1817        18 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
[root@centos7 ~]# docker tag f59c7e5b1817 zwx/ub_mv:127 
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zwx/ub_mv           127                 f59c7e5b1817        18 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB

docker的導(dǎo)入導(dǎo)出操作

save 保存鏡像為tar文件并發(fā)送到STDOUT:

[root@node2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zwx_ub              latest              f59c7e5b1817        7 seconds ago       192 MB
ubuntu              latest              8693db7e8a00        6 days ago          187.9 MB
[root@node2 ~]# docker save f59c7e5b1817 >zwx_ub.tar
# 我將zwx_ub這個(gè)鏡像導(dǎo)出成tar包,并拷貝到centos7的測(cè)試機(jī)中導(dǎo)入,導(dǎo)入過(guò)程在下邊。

load 從tar文件中載入鏡像或倉(cāng)庫(kù)到STDIN:

[root@centos7 ~]# docker load -i zwx_ub.tar 
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubhttp              latest              d47bbf8e50ba        About an hour ago   248 MB
<none>              <none>              f59c7e5b1817        16 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
[root@centos7 ~]# docker run -it f59c7e5b1817
root@e17558664f8d:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@e17558664f8d:/# ls /mnt/
zwx
# 可以看出,我導(dǎo)入zwx_ub這個(gè)鏡像后,鏡像ID并沒(méi)有變化,我創(chuàng)建個(gè)容器并進(jìn)入,發(fā)現(xiàn)打包前我創(chuàng)建的文件都在。

import 從本地文件系統(tǒng)導(dǎo)入一個(gè)鏡像

比如,先下載了一個(gè) ubuntu-14.04 的鏡像,之后使用以下命令導(dǎo)入
tar.gz的鏡像可以在http://openvz.org/Download/template/precreated下載。

[root@centos7 ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz  |docker import - ubuntu:zwx
23997a971195cdd826f16a50573e480e1be1679729636178146425cdd46d1b52
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              zwx                 23997a971195        28 seconds ago      214.9 MB

export 容器的導(dǎo)出

[root@centos7 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
16f568766019        ubuntu              "/bin/bash"         52 minutes ago      Up 45 minutes                           elegant_mcclintock
[root@centos7 ~]# docker export 16f568766019 >ubuntu.tar

import 容器的導(dǎo)入

可以將容器的tar文件再導(dǎo)入為鏡像

$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
test/ubuntu         v1.0                9d37a6082e97        About a minute ago   171.3 MB

此外,也可以通過(guò)指定 URL 或者某個(gè)目錄來(lái)導(dǎo)入,例如

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

:用戶(hù)既可以使用 docker load 來(lái)導(dǎo)入鏡像存儲(chǔ)文件到本地鏡像庫(kù),也可以使用 docker import 來(lái)導(dǎo)入一個(gè)容器快照到本地鏡像庫(kù)。這兩者的區(qū)別在于容器快照文件將丟棄所有的歷史記錄和元數(shù)據(jù)信息(即僅保存容器當(dāng)時(shí)的快照狀態(tài)),而鏡像存儲(chǔ)文件將保存完整記錄,體積也要大。此外,從容器快照文件導(dǎo)入時(shí)可以重新指定標(biāo)簽等元數(shù)據(jù)信息。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容