# docker --help
Usage: docker [OPTIONS] COMMAND [arg...]
docker daemon [ --help | ... ]
docker [ -h | --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files
-D, --debug=false Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help=false Print usage
-l, --log-level=info Set the logging level
--tls=false Use TLS; implied by --tlsverify
--tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
--tlscert=~/.docker/cert.pem Path to TLS certificate file
--tlskey=~/.docker/key.pem Path to TLS key file
--tlsverify=false Use TLS and verify the remote
-v, --version=false Print version information and quit
Commands:
attach Attach to a running container
--将终端依附到容器上
1> 运行一个交互型容器
[root@localhost ~]# docker run -i -t centos /bin/bash
[root@f0a02b473067 /]#
2> 在另一个窗口上查看该容器的状态
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 5 seconds ago Up 5 seconds cranky_mahavira
3> 退出第一步中运行的容器
[root@d4a75f165ce6 /]# exit
exit
4> 查看该容器的状态
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 2 minutes ago Exited (0) 23 seconds ago cranky_mahavira
可见此时容器的状态是Exited,那么,如何再次运行这个容器呢?可以使用docker start命令
5> 再次运行该容器
[root@localhost ~]# docker start cranky_mahavira
cranky_mahavira
6> 再次查看该容器的状态
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 6 minutes ago Up 29 seconds cranky_mahavira
因为该容器是交互型的,但此刻我们发现没有具体的终端可以与之交互,这时可使用attach命令。
7> 通过attach命令进行交互
[root@localhost ~]# docker attach cranky_mahavira
[root@d4a75f165ce6 /]#
build Build an image from a Dockerfile
--通过Dockerfile创建镜像
commit Create a new image from a container's changes
--通过容器创建本地镜像
注意:如果是要push到docker hub中,注意生成镜像的命名
[root@localhost ~]# docker commit centos_v1 centos:v1
68ad49c999496cff25fdda58f0521530a143d3884e61bce7ada09bdc22337638
[root@localhost ~]# docker push centos:v1
You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: <user>/centos)
用centos:v1就不行,因为它push到docker hub中时,是推送到相应用户下,必须指定用户名。譬如我的用户名是ivictor,则新生成的本地镜像命名为:
docker push victor/centos:v1,其中v1是tag,可不写,默认是latest
cp Copy files/folders from a container to a HOSTDIR or to STDOUT
--在宿主机和容器之间相互COPY文件
cp的用法如下:
Usage: docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|-
docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH
如:容器mysql中/usr/local/bin/存在docker-entrypoint.sh文件,可如下方式copy到宿主机
# docker cp mysql:/usr/local/bin/docker-entrypoint.sh /root
修改完毕后,将该文件重新copy回容器
# docker cp /root/docker-entrypoint.sh mysql:/usr/local/bin/
create Create a new container
--创建一个新的容器,注意,此时,容器的status只是Created
diff Inspect changes on a container's filesystem
--查看容器内发生改变的文件,以我的mysql容器为例
[root@localhost ~]# docker diff mysqldb
C /root
A /root/.bash_history
A /test1.txt
A /test.tar
A /test.txt
C /run
C /run/mysqld
A /run/mysqld/mysqld.pid
A /run/mysqld/mysqld.sock
不难看出,C对应的均是目录,A对应的均是文件
events Get real time events from the server
--实时输出Docker服务器端的事件,包括容器的创建,启动,关闭等。
譬如:
[root@localhost ~]# docker events
2015-09-08T17:40:13.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) create
2015-09-08T17:40:14.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) die
2015-09-08T17:42:10.000000000+08:00 839866a338db6dd626fa8eabeef53a839e4d2e2eb16ebd89679aa722c4caa5f7: (from mysql) start
exec Run a command in a running container
--用于容器启动之后,执行其它的任务
通过exec命令可以创建两种任务:后台型任务和交互型任务
后台型任务:docker exec -d cc touch 123 其中cc是容器名
交互型任务:
[root@localhost ~]# docker exec -i -t cc /bin/bash
root@1e5bb46d801b:/# ls
123 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
export Export a container's filesystem as a tar archive
--将容器的文件系统打包成tar文件
有两种方式:
docker export -o mysqldb1.tar mysqldb
docker export mysqldb > mysqldb.tar
history Show the history of an image
--显示镜像制作的过程,相当于dockfile
images List images
--列出本机的所有镜像
import Import the contents from a tarball to create a filesystem image
--根据tar文件的内容新建一个镜像,与之前的export命令相对应
[root@localhost ~]# docker import mysqldb.tar mysql:v1
eb81de183cd94fd6f0231de4ff29969db822afd3a25841d2dc9cf3562d135a10
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mysql v1 eb81de183cd9 21 seconds ago 281.9 MB
info Display system-wide information
--查看docker的系统信息
[root@localhost ~]# docker info
Containers: 3 --当前有3个容器
Images: 298
Storage Driver: devicemapper
Pool Name: docker-253:0-34402623-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 8.677 GB --对应的是下面Data loop file大小
Data Space Total: 107.4 GB
Data Space Available: 5.737 GB
Metadata Space Used: 13.4 MB --对应的是下面Metadata loop file大小
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.134 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.93-RHEL7 (2015-01-28)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.10.0-229.el7.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 2
Total Memory: 979.7 MiB
Name: localhost.localdomain
ID: TFVB:BXGQ:VVOC:K2DJ:LECE:2HNK:23B2:LEVF:P3IQ:L7D5:NG2V:UKNL
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
inspect Return low-level information on a container or image
--用于查看容器的配置信息,包含容器名、环境变量、运行命令、主机配置、网络配置和数据卷配置等。
kill Kill a running container
--强制终止容器
关于stop和kill的区别,docker stop命令给容器中的进程发送SIGTERM信号,默认行为是会导致容器退出,当然,
容器内程序可以捕获该信号并自行处理,例如可以选择忽略。而docker kill则是给容器的进程发送SIGKILL信号,该信号将会使容器必然退出。
load Load an image from a tar archive or STDIN
--与下面的save命令相对应,将下面sava命令打包的镜像通过load命令导入
login Register or log in to a Docker registry
--登录到自己的Docker register,需有Docker Hub的注册账号
[root@localhost ~]# docker login
Username: ivictor
Password:
Email: xxxx@foxmail.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
logout Log out from a Docker registry
--退出登录
[root@localhost ~]# docker logout
Remove login credentials for https://index.docker.io/v1/