Docker Commands
docker: 查看docker命令和选项
[bruce@rhel6 ~]$ sudo docker
Usage: docker [OPTIONS] COMMAND [arg...]
-H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use
A self-sufficient runtime for linux containers.
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders from a container's filesystem to the host path
diff Inspect changes on a container's filesystem
events Get real time events from the server
export Stream the contents of a container as a tar archive
history Show the history of an image
images List images
import Create a new filesystem image from the contents of a tarball
info Display system-wide information
inspect Return low-level information on a container
kill Kill a running container
load Load an image from a tar archive
login Register or log in to the Docker registry server
logs Fetch the logs of a container
port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
pause Pause all processes within a container
ps List containers
pull Pull an image or a repository from a Docker registry server
push Push an image or a repository to a Docker registry server
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image to a tar archive
search Search for an image on the Docker Hub
start Start a stopped container
stop Stop a running container
tag Tag an image into a repository
top Lookup the running processes of a container
unpause Unpause a paused container
version Show the Docker version information
wait Block until a container stops, then print its exit code
[bruce@rhel6 ~]$ sudo docker -h
Usage of docker:
--api-enable-cors=false Enable CORS headers in the remote API
-b, --bridge="" Attach containers to a pre-existing network bridge
use 'none' to disable container networking
--bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b
-D, --debug=false Enable debug mode
-d, --daemon=false Enable daemon mode
--dns=[] Force Docker to use specific DNS servers
--dns-search=[] Force Docker to use specific DNS search domains
-e, --exec-driver="native" Force the Docker runtime to use a specific exec driver
-G, --group="docker" Group to assign the unix socket specified by -H when running in daemon mode
use '' (the empty string) to disable setting of a group
-g, --graph="/var/lib/docker" Path to use as the root of the Docker runtime
-H, --host=[] The socket(s) to bind to in daemon mode
specified using one or more tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd.
--icc=true Enable inter-container communication
--ip="0.0.0.0" Default IP address to use when binding container ports
--ip-forward=true Enable net.ipv4.ip_forward
--iptables=true Enable Docker's addition of iptables rules
--mtu=0 Set the containers network MTU
if no value is provided: default to the default route MTU or 1500 if no default route is available
-p, --pidfile="/var/run/docker.pid" Path to use for daemon PID file
-r, --restart=true Restart previously running containers
-s, --storage-driver="" Force the Docker runtime to use a specific storage driver
--selinux-enabled=false Enable selinux support
--storage-opt=[] Set storage driver options
--tls=false Use TLS; implied by tls-verify flags
--tlscacert="/root/.docker/ca.pem" Trust only remotes providing a certificate signed by the CA given here
--tlscert="/root/.docker/cert.pem" Path to TLS certificate file
--tlskey="/root/.docker/key.pem" Path to TLS key file
--tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon)
-v, --version=false Print version information and quit
[bruce@rhel6 ~]$
docker version: 查看docker的版本号
[bruce@rhel6 /]$ sudo docker version
Client version: 1.1.2
Client API version: 1.13
Go version (client): go1.2.2
Git commit (client): d84a070/1.1.2
Server version: 1.1.2
Server API version: 1.13
Go version (server): go1.2.2
Git commit (server): d84a070/1.1.2
docker info: 查看docker的信息,里面可以看到container数量和images数量等
[bruce@rhel6 /]$ sudo docker info
Containers: 9
Images: 1
Storage Driver: devicemapper
Pool Name: docker-8:2-2625258-pool
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 638.7 Mb
Data Space Total: 102400.0 Mb
Metadata Space Used: 1.2 Mb
Metadata Space Total: 2048.0 Mb
Execution Driver: lxc-1.0.6
Kernel Version: 2.6.32-431.el6.x86_64
docker images: 罗列images
docker images -a: 罗列所有images(包括中间历史的)
docker images --tree : 显示镜像的所有层(layer)
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64 latest 6b4a87d0b323 19 hours ago 305.3 MB
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64 latest 6b4a87d0b323 19 hours ago 305.3 MB
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
└─574037ac4007 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64_demo:v1
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker run -i -t rhel6.5_x86_64 /bin/bash
bash-4.1#
bash-4.1# ifconfig
eth0 Link encap:Ethernet HWaddr 66:6F:CA:B1:11:F9
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::646f:caff:feb1:11f9/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:468 (468.0 b) TX bytes:468 (468.0 b)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
bash-4.1#
bash-4.1# ping www.sina.com.cn
PING taurus.sina.com.cn (61.172.201.15) 56(84) bytes of data.
64 bytes from 61.172.201.15: icmp_seq=1 ttl=61 time=21.3 ms
64 bytes from 61.172.201.15: icmp_seq=2 ttl=61 time=8.36 ms
64 bytes from 61.172.201.15: icmp_seq=3 ttl=61 time=8.49 ms
^C
--- taurus.sina.com.cn ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2464ms
rtt min/avg/max/mdev = 8.360/12.740/21.366/6.099 ms
bash-4.1#
>>> run 'docker ps' on another terminal
docker ps : 列出当前所有正在运行的container
docker ps -l : 列出最近一次启动的container
docker ps -a : 列出所有的container(包含历史,即运行过的container)
docker ps -q : 列出最近一次运行的container ID
>>>
[bruce@rhel6 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 22 seconds ago Up 21 seconds nostalgic_galileo
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 2 minutes ago Up 2 minutes nostalgic_galileo
[bruce@rhel6 ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 24 seconds ago Up 24 seconds nostalgic_galileo
44281a01687e rhel6.5_x86_64:latest /bin/bash 26 minutes ago goofy_euclid
e02d7b6061aa rhel6.5_x86_64:latest /bin/bash 18 hours ago Exited (0) 18 hours ago loving_einstein
1130b0c2c518 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 18 hours ago sleepy_yonath
01ba78d585d4 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago sick_feynman
050fc7a8ed16 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago furious_davinci
1332cb105337 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago condescending_ptolemy
31c3c567937a rhel6.5_x86_64:latest /bin/bash 19 hours ago pensive_hopper
b4504647422c rhel6.5_x86_64:latest /bin/bash 19 hours ago desperate_leakey
e482d39c642b rhel6.5_x86_64:latest /bin/ping www.baidu. 19 hours ago sad_almeida
[bruce@rhel6 ~]$ sudo docker ps -q
34e0bab838e2
bash-4.1# exit
exit
[bruce@rhel6 /]$
docker start/stop/restart <container> : 启动/停止/重启container
docker start [container_id] : 启动某个container (可以是历史container)
docker attach [container_id] : 连接某个正在运行的container(container必须已经start状态)
docker start -i <container> : 以交互模式启动container(等同于先start,后attach)
[bruce@rhel6 ~]$ sudo docker start 34e0bab838e2
34e0bab838e2
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 7 minutes ago Up 7 seconds nostalgic_galileo
[bruce@rhel6 ~]$ sudo docker attach 34e0bab838e2
bash-4.1#
bash-4.1# ls
bin boot dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var
bash-4.1# exit
exit
[bruce@rhel6 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[bruce@rhel6 ~]$ sudo docker start 34e0bab838e2
34e0bab838e2
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$ sudo docker restart 34e0bab838e2
34e0bab838e2
[bruce@rhel6 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 8 minutes ago Up 2 seconds nostalgic_galileo
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$ sudo docker start -i 34e0bab838e2
34e0bab838e2
bash-4.1#
bash-4.1#
bash-4.1# chkconfig --list
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
bash-4.1# exit
exit
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$
docker rm <container...> : 删除1个或者多个container
docker rm `docker ps -a -q` : 方式一: 删除所有的container
docker ps -a -q | xargs docker rm : 方式二: 删除所有的container
[bruce@rhel6 ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 12 minutes ago Exited (0) 52 seconds ago nostalgic_galileo
44281a01687e rhel6.5_x86_64:latest /bin/bash 38 minutes ago goofy_euclid
e02d7b6061aa rhel6.5_x86_64:latest /bin/bash 18 hours ago Exited (0) 18 hours ago loving_einstein
1130b0c2c518 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago sleepy_yonath
01ba78d585d4 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago sick_feynman
050fc7a8ed16 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago furious_davinci
1332cb105337 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago condescending_ptolemy
31c3c567937a rhel6.5_x86_64:latest /bin/bash 20 hours ago pensive_hopper
b4504647422c rhel6.5_x86_64:latest /bin/bash 20 hours ago desperate_leakey
e482d39c642b rhel6.5_x86_64:latest /bin/ping www.baidu. 20 hours ago sad_almeida
[bruce@rhel6 ~]$ sudo docker rm e482d39c642b
e482d39c642b
[bruce@rhel6 ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 13 minutes ago Exited (0) About a minute ago nostalgic_galileo
44281a01687e rhel6.5_x86_64:latest /bin/bash 39 minutes ago goofy_euclid
e02d7b6061aa rhel6.5_x86_64:latest /bin/bash 18 hours ago Exited (0) 18 hours ago loving_einstein
1130b0c2c518 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago sleepy_yonath
01ba78d585d4 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago sick_feynman
050fc7a8ed16 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago furious_davinci
1332cb105337 rhel6.5_x86_64:latest /bin/bash 19 hours ago Exited (0) 19 hours ago condescending_ptolemy
31c3c567937a rhel6.5_x86_64:latest /bin/bash 20 hours ago pensive_hopper
b4504647422c rhel6.5_x86_64:latest /bin/bash 20 hours ago desperate_leakey
[bruce@rhel6 ~]$
[bruce@rhel6 ~]$ sudo docker rm
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
-f, --force=false Force removal of running container
-l, --link=false Remove the specified link and not the underlying container
-v, --volumes=false Remove the volumes associated with the container
[bruce@rhel6 ~]$ sudo docker rm b4504647422c 31c3c567937a 1332cb105337 050fc7a8ed16 01ba78d585d4 1130b0c2c518 e02d7b6061aa 44281a01687e
b4504647422c
31c3c567937a
1332cb105337
050fc7a8ed16
01ba78d585d4
1130b0c2c518
e02d7b6061aa
44281a01687e
[bruce@rhel6 ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 16 minutes ago Exited (0) 4 minutes ago nostalgic_galileo
[bruce@rhel6 ~]$
docker commit -m "comments" -a "author" <container-id> [repository image name:tag] : 将一个container固化为一个新的image
[bruce@rhel6 /]$ sudo docker commit -h
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
-a, --author="" Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-m, --message="" Commit message
-p, --pause=true Pause container during commit
[bruce@rhel6 ~]$ sudo docker commit -m "Demo generating image from container" -a "Bruce" 34e0bab838e2 rhel6.5_x86_64_demo:v1
574037ac4007eb8a21c5dec52571325374838db401a02695a5ff4401c4296cc6
[bruce@rhel6 ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 2 hours ago Exited (0) 2 minutes ago nostalgic_galileo
[bruce@rhel6 ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_demo v1 574037ac4007 12 seconds ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 22 hours ago 305.3 MB
docker export: 导出container到文件
docker import: 导入container
[bruce@rhel6 /]$ sudo docker export -h
Usage: docker export CONTAINER
Export the contents of a filesystem as a tar archive to STDOUT
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 4 hours ago Exited (0) 2 hours ago nostalgic_galileo
[bruce@rhel6 /]$
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker export 34e0bab838e2 > /tmp/rhel6.5_x86_64_container_exp_demo.tar
[bruce@rhel6 /]$
[bruce@rhel6 /]$ ls /tmp/rh* -al
-rw-rw-r-- 1 bruce bruce 328040448 Oct 21 15:19 /tmp/rhel6.5_x86_64_container_exp_demo.tar
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e0bab838e2 rhel6.5_x86_64:latest /bin/bash 4 hours ago Exited (0) 2 hours ago nostalgic_galileo
[bruce@rhel6 /]$ sudo docker rm 34e0bab838e2
34e0bab838e2
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[bruce@rhel6 /]$ sudo docker import -h
Usage: docker import URL|- [REPOSITORY[:TAG]]
Create an empty filesystem image and import the contents of the tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then optionally tag it.
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo cat /tmp/rhel6.5_x86_64_container_exp_demo.tar | sudo docker import - rhel6.5_x86_64_container_exp_demo:latest
3fd239b0ca2842b5cdda0e9ddf7c1ed8fcc57e82f889fd55c173d8111556b82c
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_container_exp_demo latest 3fd239b0ca28 26 seconds ago 318.5 MB
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─3fd239b0ca28 Virtual Size: 318.5 MB Tags: rhel6.5_x86_64_container_exp_demo:latest
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
└─574037ac4007 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64_demo:v1
[bruce@rhel6 /]$
docker save: 保存image
docker load: 加载image
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_container_exp_demo latest 3fd239b0ca28 6 minutes ago 318.5 MB
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker save -h
Usage: docker save IMAGE
Save an image to a tar archive (streamed to STDOUT by default)
-o, --output="" Write to an file, instead of STDOUT
[bruce@rhel6 /]$ sudo docker save 3fd239b0ca28 > /tmp/rhel6.5_x86_64_container_save_demo.tar
[bruce@rhel6 /]$ ls /tmp/rh* -al
-rw-rw-r-- 1 bruce bruce 328040448 Oct 21 15:19 /tmp/rhel6.5_x86_64_container_exp_demo.tar
-rw-rw-r-- 1 bruce bruce 328045568 Oct 21 15:33 /tmp/rhel6.5_x86_64_container_save_demo.tar
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_container_exp_demo latest 3fd239b0ca28 21 minutes ago 318.5 MB
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker rmi 3fd239b0ca28
Untagged: rhel6.5_x86_64_container_exp_demo:latest
Deleted: 3fd239b0ca2842b5cdda0e9ddf7c1ed8fcc57e82f889fd55c173d8111556b82c
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker load < /tmp/rhel6.5_x86_64_container_save_demo.tar
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
<none> <none> 3fd239b0ca28 23 minutes ago 318.5 MB
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker tag 3fd239b0ca28 rhel6.5_x86_64_container_load_demo:latest
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_container_load_demo latest 3fd239b0ca28 24 minutes ago 318.5 MB
rhel6.5_x86_64_demo v1 574037ac4007 2 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 24 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─3fd239b0ca28 Virtual Size: 318.5 MB Tags: rhel6.5_x86_64_container_load_demo:latest
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
└─574037ac4007 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64_demo:v1
[bruce@rhel6 /]$
Difference between export/import and save/load:
[Refers to http://tuhrig.de/difference-between-save-and-export-in-docker/]
导出后再导入(exported-imported)的镜像会丢失所有的历史,而保存后再加载(saved-loaded)的镜像没有丢失历史和层(layer)。
这意味着使用导出后再导入的方式,你将无法找到到之前的层(layer)。
>>> 总之: 如果要将某个image从一个docker‘拷贝’到另一个docker,那么使用“save/load”方式。
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_demo2 v2 47314df268ef 2 minutes ago 305.3 MB
rhel6.5_x86_64_demo v1 574037ac4007 3 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
├─47314df268ef Virtual Size: 305.3 MB Tags: rhel6.5_x86_64_demo2:v2
└─574037ac4007 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64_demo:v1
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfd3763a5d80 rhel6.5_x86_64_demo2:v2 ifconfig 2 minutes ago Exited (0) 2 minutes ago loving_poincare
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 11 minutes ago Exited (0) 11 minutes ago pensive_lalande
4534a13209b7 rhel6.5_x86_64_demo:v1 /bin/bash 13 minutes ago Exited (0) 12 minutes ago backstabbing_fermat
[bruce@rhel6 /]$ sudo docker export 4534a13209b7 > /tmp/rhel6.5_demo_export.tar
[bruce@rhel6 /]$ sudo docker save 47314df268ef > /tmp/rhel6.5_demo_save.tar
[bruce@rhel6 /]$ sudo ps -a
PID TTY TIME CMD
3529 pts/0 00:00:20 docker
6779 pts/2 00:00:00 sudo
6780 pts/2 00:00:00 ps
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfd3763a5d80 rhel6.5_x86_64_demo2:v2 ifconfig 5 minutes ago Exited (0) 5 minutes ago loving_poincare
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 14 minutes ago Exited (0) 14 minutes ago pensive_lalande
4534a13209b7 rhel6.5_x86_64_demo:v1 /bin/bash 16 minutes ago Exited (0) 15 minutes ago backstabbing_fermat
[bruce@rhel6 /]$ sudo docker rm 4534a13209b7
4534a13209b7
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfd3763a5d80 rhel6.5_x86_64_demo2:v2 ifconfig 5 minutes ago Exited (0) 5 minutes ago loving_poincare
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 15 minutes ago Exited (0) 15 minutes ago pensive_lalande
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_demo2 v2 47314df268ef 6 minutes ago 305.3 MB
rhel6.5_x86_64_demo v1 574037ac4007 3 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker rm dfd3763a5d80
dfd3763a5d80
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 16 minutes ago Exited (0) 16 minutes ago pensive_lalande
[bruce@rhel6 /]$ sudo docker rmi 47314df268ef
Deleted: 47314df268ef4e815d97e73e3450e6d7c0c15f76db33f2fbec5301bc26546f6e
[bruce@rhel6 /]$ sudo docker rmi 574037ac4007
Deleted: f396ed50d1c263f0f7ddf3c54c0751216cb0f783a85b8653440cc30fec1ef219
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 16 minutes ago Exited (0) 16 minutes ago pensive_lalande
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$
[bruce@rhel6 /]$
[bruce@rhel6 /]$
[bruce@rhel6 /]$ cat /tmp/rhel6.5_demo_
rhel6.5_demo_export.tar rhel6.5_demo_save.tar
[bruce@rhel6 /]$ sudo cat /tmp/rhel6.5_demo_export.tar | sudo docker import - demo1:import
bfefb4ca5804fc8b894d2560197ab22603657115f42b4282671c09aa519d678f
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo1 import bfefb4ca5804 20 seconds ago 318.5 MB
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─bfefb4ca5804 Virtual Size: 318.5 MB Tags: demo1:import
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
[bruce@rhel6 /]$ sudo docker load < /tmp/rhel6.5_demo_save.tar
[bruce@rhel6 /]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo1 import bfefb4ca5804 About a minute ago 318.5 MB
<none> <none> 47314df268ef 10 minutes ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker tag 47314df268ef demo2:load
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo1 import bfefb4ca5804 19 seconds ago 318.5 MB
demo2 load 47314df268ef 18 minutes ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 25 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─bfefb4ca5804 Virtual Size: 318.5 MB Tags: demo1:import
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest
└─47314df268ef Virtual Size: 305.3 MB Tags: demo2:load
[bruce@rhel6 /]$
docker inspect: 查看container或者image底层信息
[bruce@rhel6 /]$ sudo docker inspect -h
Usage: docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]
Return low-level information on a container or image
-f, --format="" Format the output using the given go template.
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 44 minutes ago Exited (0) 43 minutes ago pensive_lalande
[bruce@rhel6 /]$ sudo docker inspect 28723f7f42ec
[{
"Args": [
"www.sina.com.cn"
],
"Config": {
"AttachStderr": true,
"AttachStdin": true,
"AttachStdout": true,
"Cmd": [
"ping",
"www.sina.com.cn"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [],
"ExposedPorts": {},
"Hostname": "28723f7f42ec",
"Image": "6b4a87d0b323",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": true,
"PortSpecs": null,
"StdinOnce": true,
"Tty": true,
"User": "",
"Volumes": {},
"WorkingDir": ""
},
"Created": "2014-10-21T08:11:40.019264321Z",
"Driver": "devicemapper",
"ExecDriver": "lxc-1.0.6",
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"Dns": null,
"DnsSearch": null,
"Links": null,
"LxcConf": [],
"NetworkMode": "bridge",
"PortBindings": {},
"Privileged": false,
"PublishAllPorts": false,
"VolumesFrom": null
},
"HostnamePath": "/var/lib/docker/containers/28723f7f42ec5503af15366717786615c85167e207d0a9241ea40a79b1982590/hostname",
"HostsPath": "/var/lib/docker/containers/28723f7f42ec5503af15366717786615c85167e207d0a9241ea40a79b1982590/hosts",
"Id": "28723f7f42ec5503af15366717786615c85167e207d0a9241ea40a79b1982590",
"Image": "6b4a87d0b32383bc7f461f530718cc483fcedcbc2562661a9a4e688eafa8ef61",
"MountLabel": "",
"Name": "/pensive_lalande",
"NetworkSettings": {
"Bridge": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"PortMapping": null,
"Ports": null
},
"Path": "ping",
"ProcessLabel": "",
"ResolvConfPath": "/etc/resolv.conf",
"State": {
"ExitCode": 0,
"FinishedAt": "2014-10-21T08:11:42.076795737Z",
"Paused": false,
"Pid": 0,
"Running": false,
"StartedAt": "2014-10-21T08:11:40.294816708Z"
},
"Volumes": {},
"VolumesRW": {}
}
]
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker inspect 6b4a87d0b323
[{
"Architecture": "amd64",
"Author": "",
"Comment": "Imported from -",
"Config": null,
"Container": "",
"ContainerConfig": {
"AttachStderr": false,
"AttachStdin": false,
"AttachStdout": false,
"Cmd": null,
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": null,
"ExposedPorts": null,
"Hostname": "",
"Image": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": false,
"PortSpecs": null,
"StdinOnce": false,
"Tty": false,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2014-10-20T07:04:34.642564972Z",
"DockerVersion": "1.1.2",
"Id": "6b4a87d0b32383bc7f461f530718cc483fcedcbc2562661a9a4e688eafa8ef61",
"Os": "linux",
"Parent": "",
"Size": 305267275
}
]
[bruce@rhel6 /]$
docker logs: 查看docker运行日志
[bruce@rhel6 /]$ sudo docker logs -h
Usage: docker logs CONTAINER
Fetch the logs of a container
-f, --follow=false Follow log output
-t, --timestamps=false Show timestamps
--tail="all" Output the specified number of lines at the end of logs (defaults to all logs)
[bruce@rhel6 /]$ sudo docker logs 28723f7f42ec
PING taurus.sina.com.cn (61.172.201.26) 56(84) bytes of data.
64 bytes from 61.172.201.26: icmp_seq=1 ttl=61 time=24.5 ms
64 bytes from 61.172.201.26: icmp_seq=2 ttl=61 time=6.32 ms
^C
--- taurus.sina.com.cn ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1678ms
rtt min/avg/max/mdev = 6.321/15.449/24.577/9.128 ms
docker cp: 从container拷贝文件到host
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo1 import bfefb4ca5804 39 minutes ago 318.5 MB
demo2 load 47314df268ef 57 minutes ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 26 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn About an hour ago Exited (0) About an hour ago pensive_lalande
[bruce@rhel6 /]$ sudo docker cp -h
Usage: docker cp CONTAINER:PATH HOSTPATH
Copy files/folders from the PATH to the HOSTPATH
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn About an hour ago Exited (0) About an hour ago pensive_lalande
[bruce@rhel6 /]$ sudo docker run -i -t bfefb4ca5804 /bin/bash
bash-4.1# ls
bin boot dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var
bash-4.1# cd /root
bash-4.1# ls
1.txt
bash-4.1# cat 1.txt
#Demo Text File for Docker
bash-4.1# exit
exit
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ce41b7a637f demo1:import /bin/bash 16 seconds ago Exited (0) 4 seconds ago tender_feynman
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn About an hour ago Exited (0) About an hour ago pensive_lalande
[bruce@rhel6 /]$ sudo docker cp -h
Usage: docker cp CONTAINER:PATH HOSTPATH
Copy files/folders from the PATH to the HOSTPATH
[bruce@rhel6 /]$ sudo docker cp 7ce41b7a637f:/root/1.txt .
[bruce@rhel6 /]$ cat 1.txt
#Demo Text File for Docker
[bruce@rhel6 /]$
/var/lib/docker: docker存放文件的地方
[bruce@rhel6 /]$ sudo ls -al /var/lib/docker/
total 44
drwx------. 8 root root 4096 Oct 21 17:20 .
drwxr-xr-x. 47 root root 4096 Oct 20 11:23 ..
drwx------. 4 root root 4096 Oct 21 17:20 containers
drwx------. 5 root root 4096 Oct 20 15:04 devicemapper
drwx------. 3 root root 4096 Oct 20 11:29 execdriver
drwx------. 6 root root 4096 Oct 21 16:38 graph
drwx------. 2 root root 4096 Oct 20 11:29 init
-rw-r--r--. 1 root root 5120 Oct 21 17:20 linkgraph.db
lrwxrwxrwx 1 root root 18 Oct 21 10:53 lxc-start-unconfined -> /usr/bin/lxc-start
-rw-------. 1 root root 283 Oct 21 16:38 repositories-devicemapper
drwx------. 2 root root 4096 Oct 20 11:29 volumes