#查看本地容器进程
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
528ea123a806 hello-world "/hello" 13 hours ago Exited (0) 13 hours ago angry_dirac
#查看镜像
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lihanbing/alpine v3.10.3 a187dde48cd2 12 days ago 5.6MB
alpine latest a187dde48cd2 12 days ago 5.6MB
hello-world latest fce289e99eb9 15 months ago 1.84kB
#启动容器
~]# docker run -it alpine:latest /bin/sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:07:04:01
inet addr:172.7.4.1 Bcast:172.7.4.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16 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:1296 (1.2 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
#非交互式启动容器
~]# docker run --rm alpine:latest /bin/echo hello #启动完会自动退出
#非交互式启动后台容器
~]# docker run -d --name myalpine alpine:latest #会自动退出
0a642f280bc4576f28e5d28baeda7dc2d7e4d4edb6dc61b6746e5b69e95c6dae
~]# docker run -d --name myalpine1 alpine:latest /bin/sleep 300 #会停留300s
6397daba92df89b904744fea12e6e4ccbf2d78ed2f44f80c486c5aa2c9b53735
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6397daba92df alpine:latest "/bin/sleep 300" 37 seconds ago Up 37 seconds myalpine1
#查看宿主机进程
~]# ps aux | grep sleep | grep -v grep
root 14190 0.0 0.0 1560 244 ? Ss 11:53 0:00 /bin/sleep 300
#进入处于UP状态的容器
~]# docker exec -it 118557f79e43 /bin/sh
#停止容器
~]# docker stop 118557f79e43
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
118557f79e43 alpine:latest "/bin/sleep 300" 5 minutes ago Exited (137) About a minute ago
#再次启动
~]# docker start 118557f79e43
118557f79e43
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
118557f79e43 alpine:latest "/bin/sleep 300" 6 minutes ago Up 20 seconds myalpine2
#restart容器
~]# docker restart 118557f79e43
#这些操作可以用容器的ID,也可以用容器的名字
#删除容器
~]# docker rm myalpine
myalpine #删除已经停止的容器
#删除正在运行的容器
~]# docker rm -f myalpine2
#删除所有退出的容器
~]# for i in `docker ps -a | grep -i exit | awk '{print $1}'`;do docker rm -f $i;done