docker 容器的基本操作
run
格式
docker run [选项] 镜像 [命令] [参数...]
选项
选项 | 解释 |
---|---|
-d | 后台运行 |
-i | 交互模式 |
-t | 分配一个伪终端 |
-p | 设置端口 |
--rm | 运行完命令后,删除容器 |
--name | 指定名称 |
--dns | 指定dns(默认dns与主机一致) |
实例
- 1.启动容器后自动终止容器
[root@hmm ~]# docker run --name deamo1 centos
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8204e6b2710f centos "/bin/bash" About a minute ago Exited (0) About a minute ago deamo1
8949ff144990 httpd "httpd-foreground" 2 hours ago Exited (255) 17 minutes ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb
[root@hmm ~]#
- 2.启动容器并允许用户交互
[root@hmm ~]# docker run -it --name demo2 centos /bin/bash
[root@921dd07c6b6c /]# //此时进入了容器,@符号后面为容器id
- 3.启动容器并让其以守护进程的方法在后台运行
[root@hmm ~]# docker run -d -p 80:80 --name test httpd
20bfaecb17b66d81a003f990d1b9aa961b160bbc573164d89e8a6769275ccfee
[root@hmm ~]# docker ps -a |grep test
20bfaecb17b6 httpd "httpd-foreground" 17 seconds ago Up 16 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp test
- 4.启动容器并运行指定命令后删除容器
[root@hmm ~]# docker run --rm centos echo 'bye bye'
bye bye
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20bfaecb17b6 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp test
921dd07c6b6c centos "/bin/bash" 6 minutes ago Exited (1) 3 minutes ago demo2
8204e6b2710f centos "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago deamo1
8949ff144990 httpd "httpd-foreground" 2 hours ago Exited (255) 24 minutes ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb //无新生成的容器
[root@hmm ~]#
create
格式
docker create [选项] 镜像 [命令] [参数...]
选项
语法,基本与run相同。只不过create 只是创建,并不启动。
实例
[root@hmm ~]# docker create --name new ubuntu
\ad3aca1015887c81d297b79bd0bc8ff091197423fa3c467240dc0c7bc0aeea9f
[root@hmm ~]# docker ps -a |grep new
ad3aca101588 ubuntu "/bin/bash" 10 seconds ago Created new
[root@hmm ~]#
start
格式
docker start [选项] 容器
实例
[root@hmm ~]# docker start ad3aca101588
ad3aca101588
stop
格式
docker stop [选项] 容器
实例
[root@hmm ~]# docker stop ad3aca101588
ad3aca101588
[root@hmm ~]#
-t:等多少秒后停止容器
下面案例中,执行命令后需等待3秒后输出容器id,完成容器停止。
[root@hmm ~]# docker stop -t 3 ad3aca101588
ad3aca101588
pause|unpause
格式
docker pause|unpause 容器]
实例
[root@hmm ~]# docker pause 20bfaecb17b6
20bfaecb17b6
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 6 minutes ago Exited (0) 3 minutes ago new
20bfaecb17b6 httpd "httpd-foreground" 14 minutes ago Up 14 minutes (Paused) //此处为挂起状态 0.0.0.0:80->80/tcp, :::80->80/tcp test
[root@hmm ~]# docker unpause 20bfaecb17b6
20bfaecb17b6
[root@hmm ~]# docker ps -a |grep httpd
20bfaecb17b6 httpd "httpd-foreground" 17 minutes ago Up 17 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
kill
格式
docker pause|unpause 容器
实例
[root@hmm ~]# docker kill ad3aca101588
Error response from daemon: Cannot kill container: ad3aca101588: container ad3aca1015887c81d297b79bd0bc8ff091197423fa3c467240dc0c7bc0aeea9f is not running
[root@hmm ~]# //强制关闭一个正在运行的容器
批量开启和批量停止容器
rename
格式
docker rename 容器 容器名称
实例
ps
格式
选项
选项 | 解释 |
---|---|
a | 显示全部容器 |
f | 条件过滤容器 |
n | 显示最近创建的n个容器 |
l | 显示最新创建的容器 |
q | 只显示id |
s | 显示总文件大小 |
实例
- -f 筛选
- id
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 36 minutes ago Exited (0) 32 minutes ago new
20bfaecb17b6 httpd "httpd-foreground" 44 minutes ago Up 44 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
921dd07c6b6c centos "/bin/bash" 48 minutes ago Exited (1) 46 minutes ago demo2
8204e6b2710f centos "/bin/bash" 51 minutes ago Exited (0) 51 minutes ago deamo1
8949ff144990 httpd "httpd-foreground" 3 hours ago Exited (255) About an hour ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb
[root@hmm ~]# docker ps -a -f id=a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 36 minutes ago Exited (0) 33 minutes ago new
[root@hmm ~]# docker ps -a -f id=8
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@hmm ~]# docker ps -a -f id=82
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8204e6b2710f centos "/bin/bash" 52 minutes ago Exited (0) 52 minutes ago deamo1
[root@hmm ~]#
- name
[root@hmm ~]# docker ps -a -f name=demo
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
921dd07c6b6c centos "/bin/bash" 52 minutes ago Exited (1) 49 minutes ago demo2
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 40 minutes ago Exited (0) 36 minutes ago new
20bfaecb17b6 httpd "httpd-foreground" 48 minutes ago Up 48 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
921dd07c6b6c centos "/bin/bash" 53 minutes ago Exited (1) 50 minutes ago demo2
8204e6b2710f centos "/bin/bash" 55 minutes ago Exited (0) 55 minutes ago deamo1
8949ff144990 httpd "httpd-foreground" 3 hours ago Exited (255) About an hour ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb
[root@hmm ~]#
- status
可用状态:created,restarting,running,removing,pausd,exited,dead
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 40 minutes ago Exited (0) 36 minutes ago new
20bfaecb17b6 httpd "httpd-foreground" 48 minutes ago Up 48 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
921dd07c6b6c centos "/bin/bash" 53 minutes ago Exited (1) 50 minutes ago demo2
8204e6b2710f centos "/bin/bash" 55 minutes ago Exited (0) 55 minutes ago deamo1
8949ff144990 httpd "httpd-foreground" 3 hours ago Exited (255) About an hour ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb
[root@hmm ~]# docker ps -a -f status=exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad3aca101588 ubuntu "/bin/bash" 41 minutes ago Exited (0) 38 minutes ago new
921dd07c6b6c centos "/bin/bash" 54 minutes ago Exited (1) 51 minutes ago demo2
8204e6b2710f centos "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago deamo1
8949ff144990 httpd "httpd-foreground" 3 hours ago Exited (255) About an hour ago 0.0.0.0:80->80/tcp, :::80->80/tcp testweb
[root@hmm ~]# docker ps -a -f status=running
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20bfaecb17b6 httpd "httpd-foreground" 54 minutes ago Up 54 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
[root@hmm ~]#
- label
[root@hmm ~]# docker ps -a -f label=hbkj
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a5a08615a61 ubuntu "/bin/bash" 41 seconds ago Up 40 seconds suspicious_darwin
[root@hmm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3160a3da6b8d ubuntu "/bin/bash" 32 seconds ago Up 31 seconds vigilant_ramanujan
9a5a08615a61 ubuntu "/bin/bash" 48 seconds ago Up 47 seconds suspicious_darwin
ad3aca101588 ubuntu "/bin/bash" 47 minutes ago Exited (0) 4 minutes ago new
20bfaecb17b6 httpd "httpd-foreground" 56 minutes ago Up 56 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp test
921dd07c6b6c centos "/bin/bash" About an hour ago Exited (1) 57 minutes ago demo2
8204e6b2710f centos "/bin/bash" About an hour ago Exited (0) About an hour ago deamo1
[root@hmm ~]# docker ps -a -f label=hb
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@hmm ~]# //标签必须完全匹配才能筛选出来
inspect
格式
docker inspect [选项]
选项
实例
1.-s
[root@hmm ~]# docker inspect 20bfaecb17b6 >test1
[root@hmm ~]# docker inspect -s 20bfaecb17b6 >sizetest
[root@hmm ~]# diff test1 sizetest
140a141,142
> "SizeRw": 2,
> "SizeRootFs": 167481474,
2.-f
[root@hmm ~]# docker container inspect 20bfaecb17b6 --format "{{.NetworkSettings}}"
{{ b44e5e255c9acf19f2236f113cc0cf74b449695205718ee2ab02bde9381e0f86 /var/run/docker/netns/b44e5e255c9a map[80/tcp:[{0.0.0.0 80} {:: 80}]] false 0 [] []} {3abde00f27a1324014ae1c9e48b74cba2a7db2f50ddb0f747f90d71c5d462d73 172.17.0.1 0 172.17.0.2 16 02:42:ac:11:00:02} map[bridge:0xc0003401c0]}
[root@hmm ~]# docker container inspect 20bfaecb17b6 --format "{{.NetworkSettings.Ports}}"
map[80/tcp:[{0.0.0.0 80} {:: 80}]]
[root@hmm ~]#
attach
格式
docker attach [选项] 容器
选项
选项 | 解释 |
---|---|
-detach-keys string | 覆盖后台运行容器的键顺序 |
--no-stdin | 不连接标准输入 |
--sig-proxy | 将接受的信息代理给(default true) |
实例
//注意 使用attach进入容器后,exit退出时会强制停止所有服务,关闭容器
1.连接到运行的容器中,并退出
[root@hmm ~]# docker run -d --name topdemo ubuntu /usr/bin/top -b
fa1894bcd4e50457cfc24ef3cab083fd3bc7ce3ad608f6760532272d4ef3e530
[root@hmm ~]# docker attach topemo
Error response from daemon: No such container: topemo
[root@hmm ~]# docker attach topdemo
top - 12:10:04 up 1:43, 0 users, load average: 0.00, 0.01, 0.05
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1982.6 total, 1198.0 free, 350.1 used, 434.5 buff/cache
MiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 1480.8 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 7168 1712 1260 R 0.0 0.1 0:00.07 top
top - 12:10:07 up 1:43, 0 users, load average: 0.00, 0.01, 0.05
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1982.6 total, 1197.9 free, 350.1 used, 434.5 buff/cache
MiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 1480.8 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 7168 1712 1260 R 0.0 0.1 0:00.07 top^C
[root@hmm ~]# docker ps -a|grep topdemo
fa1894bcd4e5 ubuntu "/usr/bin/top -b" 47 seconds ago Exited (0) 11 seconds ago topdemo
[root@hmm ~]#