列出主机上的容器
列出正在运行的容器: docker ps
列出所有容器: docker ps -a
列出最近使用的容器,包括没有运行的: docker ps -l
仅列出容器的ID,不包括没有运行的: docker ps q
创建容器
参数:
--name 指定容器的名字
--rm 容器运行完毕会自动删除
-i -t 创建一个提供交互式shell的容器。
-d 在后台运行容器,并且打印出容器的ID。
创建交互式容器
[root@wls12c ~]$ docker run --name weblogic -i -t centos /bin/bash [root@35e3b0fa9dea /]# exit exit
对于交互式容器,当退出shell后,容器会关闭。 后面可以通过命令重新启动容器。
创建守护式容器
这种容器指容器可以长期一直运行,没有交互式会话,非常适合容器中运行后台应用程序和服务(如数据库服务、web服务器等)。
[root@wls12c ~]$ docker run -d centos /bin/bash -c "while true;do echo hello docker;sleep 1;done" af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c
启动和停止容器
docker start/stop/restart continer id/name
docker -t 可以指定时间停止容器,默认是10s
强制停止容器
docker kill continer id/name
附着到一个容器上
docker attach continer id/name
当重新启动容器时,会沿用创建容器(docker run)命令时指定的参数来运行,可能需要按回车才进入。
这时就已经相当于在容器内部了的shell操作了。如果操作过程中,退出了shell。容器也会随之停止。
[root@wls12c ~]$ docker run -i -t centos /bin/bash[root@cf9b70252ba5 /]# exitexit[root@wls12c ~]$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScf9b70252ba5 centos "/bin/bash" 22 seconds ago Exited (0) 11 seconds ago happy_bohr [root@wls12c ~]$ docker start cf9cf9[root@wls12c ~]$ docker attach cf9[root@cf9b70252ba5 /]#
在运行的容器中执行命令
[root@wls12c ~]$ docker exec -t -i af2 /bin/bash[root@af2e15753ec8 /]# ps -efUID PID PPID C STIME TTY TIME CMDroot 1 0 0 03:27 ? 00:00:00 /bin/bash -c while true;do echo hello docker;sleeproot 33 0 0 03:28 ? 00:00:00 /bin/bashroot 54 33 10 03:28 ? 00:00:00 ps -efroot 55 1 0 03:28 ? 00:00:00 sleep 1
可以通过docker exec命令在容器内部额外启动新进程,退出容器,并不会停止容器。
查看容器的详细信息
查看容器的运行时后台输出信息
docker logs continer id/name
[root@wls12c ~]$ docker logs cf9[root@cf9b70252ba5 /]# exitexit[root@cf9b70252ba5 /]# exitexit[root@cf9b70252ba5 /]# [root@cf9b70252ba5 /]# exitexit[root@wls12c ~]$ docker logs af2hello dockerhello dockerhello docker
利用docker inspect 命令可以查看容器更多的信息。 如ip地址等,这对守护容器还是很有意义的。
[root@wls12c ~]$ docker inspect af2[{ "Id": "af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c", "Created": "2016-08-24T02:58:02.527234216Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo hello docker;sleep 1;done" ], "State": { "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2123, "ExitCode": 0, "Error": "", "StartedAt": "2016-08-24T03:30:45.230597838Z", "FinishedAt": "2016-08-24T03:29:50.395642183Z" }, "Image": "d83a55af4e75f89d589940a008e1f8d87329fed8e88b17519b07bafee796e0c7", "NetworkSettings": { "Bridge": "", "EndpointID": "fc332cd88cc00e1323367ca18d47ef59a2a50c3a29fa484539d5aaa68595f098", "Gateway": "172.17.42.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "HairpinMode": false, "IPAddress": "172.17.0.46", "IPPrefixLen": 16, "IPv6Gateway": "", "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:2e", "NetworkID": "e4ea4632d4b308c9bcecb43729de781cc873e30e4d5eef893cc7bf731b696900", "PortMapping": null, "Ports": {}, "SandboxKey": "/var/run/docker/netns/af2e15753ec8", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null }, "ResolvConfPath": "/var/lib/docker/containers/af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c/resolv.conf", "HostnamePath": "/var/lib/docker/containers/af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c/hostname", "HostsPath": "/var/lib/docker/containers/af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c/hosts", "LogPath": "/var/lib/docker/containers/af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c/af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c-json.log", "Name": "/determined_brattain", "RestartCount": 0, "Driver": "aufs", "ExecDriver": "native-0.2", "MountLabel": "", "ProcessLabel": "", "Volumes": {}, "VolumesRW": {}, "AppArmorProfile": "", "ExecIDs": [ "12fca2b28a81afb4a1b7a09eb40acec5c452e9359fcc385929d315943d0555c6", "0d47716bf0c2c455384e6825f7824f25cb5bfdc2bb5eebba062531763e9b3633" ], "HostConfig": { "Binds": null, "ContainerIDFile": "", "LxcConf": [], "Memory": 0, "MemorySwap": 0, "CpuShares": 0, "CpuPeriod": 0, "CpusetCpus": "", "CpusetMems": "", "CpuQuota": 0, "BlkioWeight": 0, "OomKillDisable": false, "Privileged": false, "PortBindings": {}, "Links": null, "PublishAllPorts": false, "Dns": null, "DnsSearch": null, "ExtraHosts": null, "VolumesFrom": null, "Devices": [], "NetworkMode": "bridge", "IpcMode": "", "PidMode": "", "UTSMode": "", "CapAdd": null, "CapDrop": null, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "SecurityOpt": null, "ReadonlyRootfs": false, "Ulimits": null, "LogConfig": { "Type": "json-file", "Config": {} }, "CgroupParent": "" }, "Config": { "Hostname": "af2e15753ec8", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "PortSpecs": null, "ExposedPorts": null, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash", "-c", "while true;do echo hello docker;sleep 1;done" ], "Image": "centos", "Volumes": null, "VolumeDriver": "", "WorkingDir": "", "Entrypoint": null, "NetworkDisabled": false, "MacAddress": "", "OnBuild": null, "Labels": { "build-date": "20160729", "license": "GPLv2", "name": "CentOS Base Image", "vendor": "CentOS" } }}]
删除已经停止运行的容器
[root@wls12c ~]$ docker stop af2af2[root@wls12c ~]$ docker rm af2af2
一次删除所有停止的容器
[root@wls12c ~]$ docker rm $(docker ps -a -q)
查看容器内的改变信息
创建一个容器,会在容器的对应的镜像上增加一个可写层,镜像部分是只读的。通过 diff命令可以看出改变的信息。如:
[root@wls12c ~]$ docker run -i -t centos /bin/bash[root@224de7986c5f /]# touch demo.ext[root@224de7986c5f /]# echo hello docker >demo.ext [root@224de7986c5f /]# rm -rf anaconda-post.log [root@224de7986c5f /]# exitexit[root@wls12c ~]$ docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES224de7986c5f centos "/bin/bash" 40 seconds ago Exited (0) 3 seconds ago desperate_curie [root@wls12c ~]$ docker diff 224dD /anaconda-post.logA /demo.extC /rootA /root/.bash_history
说明:每行代表一个变动的文件或目录。其中 A 表示新增、C表示被修改、D表示被删除
主机和容器之间的文件拷贝
容器--->主机
[root@wls12c ~]$ docker exec -t -i 9f bin/bash[root@9f49397623ad /]# cat demo.txt hello[root@9f49397623ad /]# exitexit [root@wls12c ~]$ docker cp 9f:/demo.txt /test[root@wls12c ~]$ cat /test/demo.txt hello
主机-->容器
[root@wls12c ~]$ cp ~/1.txt /var/lib/docker/aufs/mnt/9f49397623ade7dfd2beb4d84454cbdb9878a4b22a2bab2e8b5db72bcffe60a0/test[root@wls12c ~]$ docker exec -t -i 9f /bin/bash[root@9f49397623ad /]# ls /test1.txt
重命名容器
[root@wls12c ~]$ docker rename stoic_meitner demo