0%

Docker 基本命令(hello world)

Docker registry拉取imagebusybox到本地系统

1
2
3
4
5
xiaomao@jp-vps:~$docker pull busybox
xiaomao@jp-vps:~$
xiaomao@jp-vps:~$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 020584afccce 12 days ago 1.22MB

基于busybox image运行一个container

1
2
xiaomao@jp-vps:~$docker run busybox
xiaomao@jp-vps:~$

感觉没有任何响应或输出:是因为没有执行任何命令,container起来后又自动退出

执行echo命令测试

1
2
3
xiaomao@jp-vps:~$docker run busybox echo "hello from busybox"
hello from busybox
xiaomao@jp-vps:~$

container查看

ps 正在运行的container
ps -a 所有运行过的container

1
2
3
4
5
6
xiaomao@jp-vps:~$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xiaomao@jp-vps:~$
xiaomao@jp-vps:~$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbb8c274f214 busybox "echo 'hello from bu…" 32 seconds ago Exited (0) 31 seconds ago youthful_napier

运行一个可交互的container

1
2
3
4
5
xiaomao@jp-vps:~$docker run -it busybox sh
/ # id
uid=0(root) gid=0(root) groups=10(wheel)
/ # hostname
e990a36fbbe4

删除container

1
2
3
4
5
6
7
8
9
10
xiaomao@jp-vps:~$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e990a36fbbe4 busybox "sh" 7 minutes ago Exited (0) 5 minutes ago friendly_liskov
bbb8c274f214 busybox "echo 'hello from bu…" 17 minutes ago Exited (0) 17 minutes ago youthful_napier
xiaomao@jp-vps:~$docker rm e990a36fbbe4 bbb8c274f214
e990a36fbbe4
bbb8c274f214
xiaomao@jp-vps:~$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xiaomao@jp-vps:~$

or

1
2
3
4
5
xiaomao@jp-vps:~$docker run busybox echo hello
hello
xiaomao@jp-vps:~$docker rm $(docker ps -a -q -f status=exited)
50a78121bce9
xiaomao@jp-vps:~$

or

1
2
3
4
5
6
7
xiaomao@jp-vps:~$docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
b47229664c70532b5c5d651cde2e289ca73058add7040603f91b08b340e7cb19

Total reclaimed space: 0B

rmi删除image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
xiaomao@jp-vps:~$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 11 days ago 64.2MB
busybox latest 020584afccce 12 days ago 1.22MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
xiaomao@jp-vps:~$docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
xiaomao@jp-vps:~$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 11 days ago 64.2MB
busybox latest 020584afccce 12 days ago 1.22MB

更多信息学会查看--help

TIPS

container退出时自动删除,执行时加--rm

1
2
3
4
xiaomao@jp-vps:~$docker run --rm busybox echo hello
hello
xiaomao@jp-vps:~$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES