容器的使用(一)

一. 运行第一个容器

1.  查看 docker版本

[root@c720120 ~]# docker -v
Docker version 18.05.0-ce, build f150324

 

2. 运行第一个容器

[root@c720120 ~]# docker container run alpine echo "Hello World"
Hello World

 

 

二. 开始,停止,移除容器

1.(1) 查看容器(以下命令是查看所有正在运行的容器)

root@c720120 ~]# docker container ls
CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS              PORTS               NAMES
486271eb3a34        0c60bcf89900                                              "/dashboard --insecu…"   4 days ago          Up 4 days                               k8s_kubernetes-dashboard_kubernetes-dashboard-7b44ff9b77-87vnz_kube-system_f4b7d3c3-6354-11e8-aec8-525400da66d3_3
d5e2a84188a3        f0fad859c909                                              "/opt/bin/flanneld -…"   4 days ago          Up 4 days                               k8s_kube-flannel_kube-flannel-ds-jpn69_kube-system_d511775a-6350-11e8-aec8-525400da66d3_2

 

(2) 查看容器(所有的容器,包括运行和没有运行的容器)

[root@c720120 ~]# docker container ps -a
CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS                      PORTS               NAMES
1abf431ab8e1        alpine                                                    "echo 'Hello World'"     15 minutes ago      Exited (0) 15 minutes ago                       xenodochial_lalande
52513c8d4a9d        alpine                                                    "echo 'Hello World'"     17 minutes ago      Exited (0) 17 minutes ago                       upbeat_roentge486271eb3a34        0c60bcf89900                                              "/dashboard --insecu…"   4 days ago          Up 4 days                                       k8s_kubernetes-dashboard_kubernetes-dashboard-7b44ff9b77-87vnz_kube-system_f4b7d3c3-6354-11e8-aec8-525400da66d3_3

 

 

(3)查看容器(只显示所 容器的ID)

[root@c720120 ~]# docker container ls -q
486271eb3a34
d5e2a84188a3
f5402aecff5d
d6e257bdf857
480c8322ffb8
1b854bcc2aab
d872474aef4d
2bf366ccc652
8a6d4a756fdf
8d31f95d2548
d3b6a739e42a
21c4b3486721
b924bad8ec4c
71cf291701e7
b2632fffbc1e
3850335e09a4

 

比如,可以执行以下命令删除所有的容器

$ docker container rm -f $(docker container ls -a -q)
 
如果要查看ls下的其它选项,请输入以下命令:

[root@c720120 ~]# docker container ls -h
Flag shorthand -h has been deprecated, please use --help

Usage:    docker container ls [OPTIONS]

List containers

Aliases:
   ls, ps, list

Options:
   -a, --all             Show all containers (default shows just running)
   -f, --filter filter   Filter output based on conditions provided
       --format string   Pretty-print containers using a Go template
   -n, --last int        Show n last created containers (includes all states) (default -1)
   -l, --latest          Show the latest created container (includes all states)
       --no-trunc        Don't truncate output
   -q, --quiet           Only display numeric IDs
   -s, --size            Display total file sizes

 
 

2. 停止和启动容器

(1)运行一个容器

[root@c720120 ~]# docker container run -d --name quotes alpine \
>    /bin/sh -c "while :; do wget -qO- https://talaikis.com/api/quotes/random; printf '\n'; sleep 5; done"
75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6

 

(2)停止在第一步运行的容器

[root@c720120 ~]# docker container stop quotes
quotes

 

或者执行以下命令:

export CONTAINER_ID = $(docker container ls | grep quotes | awk '{print $1}')
docker container stop $CONTAINER_ID
 

(3)启动容器

[root@c720120 ~]# docker container start quotes
quotes

 

3. 移除容器

(1)方式1

$ docker container rm <container ID>

 

(2)方式2

$ docker container rm <container name>

 

注意:有时候我们移除正在运行的容器时,会报错,如果要强行移除正在运行的容器,使用-f 或--force选项

posted @ 2018-06-05 16:31  大牛不会太遥远  阅读(264)  评论(0编辑  收藏  举报