三、docker容器的常用命令

root@node03 ~]# docker container

 

Usage:      docker container COMMAND

 

Manage containers

 

Commands:

  attach      Attach local standard input, output, and error streams to a running container

  commit      Create a new image from a container‘s changes

  cp          Copy files/folders between a container and the local filesystem

  create      Create a new container

  diff        Inspect changes to files or directories on a container‘s filesystem

  exec        Run a command in a running container

  export      Export a container‘s filesystem as a tar archive

  inspect     Display detailed information on one or more containers

  kill        Kill one or more running containers

  logs        Fetch the logs of a container

  ls          List containers

  pause       Pause all processes within one or more containers

  port        List port mappings or a specific mapping for the container

  prune       Remove all stopped containers

  rename      Rename a container

  restart     Restart one or more containers

  rm          Remove one or more containers

  run         Run a command in a new container

  start       Start one or more stopped containers

  stats       Display a live stream of container(s) resource usage statistics

  stop        Stop one or more running containers

  top         Display the running processes of a container

  unpause     Unpause all processes within one or more containers

  update      Update configuration of one or more containers

  wait        Block until one or more containers stop, then print their exit codes

1、运行容器docker run

$ docker run --name nginx-test -p 8081:80 -d nginx

参数说明:

  • --name nginx-test:容器名称。
  • -p 8081:80: 端口进行映射,将本地 8081 端口映射到容器内部的 80 端口。
  • -d nginx: 设置容器在在后台一直运行。

 

docker run -it –name centos6 centos:6.9 /bin/bash

-it      :分配交互式的终端

-name  :指定容器的名字

/bin/sh  : 覆盖容器的初始命令

 

运行容器指定名称

不指定名称容器名称是任意的

 三、docker容器的常用命令

 

 

2、查看容器的列表 docker ps

(1)只看正在运行的容器

[root@node01 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES

24defbb5755b        nginx               "nginx -g ‘daemon of…"         19 hours ago          Up 19 hours         0.0.0.0:8081->80/tcp      nginx-test1

 

字段解释:

CONTAINER ID :容器id

IMAGE:镜像名称

COMMAND:启动容器时启动的命令

CREATED:创建了多长时间

STATUS:最后一次启动后运行了多久

PORTS:端口占用和映射情况

NAMES:容器名称

 

(2)查看所有的容器

[root@node01 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES

24defbb5755b        nginx               "nginx -g ‘daemon of…"         19 hours ago        Up 19 hours          0.0.0.0:8081->80/tcp      nginx-test1

6d28826ceacc        nginx               "nginx -g ‘daemon of…"         19 hours ago        Created                                     nginx-test

 

查看最后一个运行的容器

[root@node03 ~]# docker ps -a -l

3、启动容器进入容器

[root@node03 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES

9dc5421ea021        nginx:latest        "nginx -g ‘daemon of…"   7 weeks ago         Exited (0) 7 weeks ago                       reverent_lamport

7cb788123876        ubuntu:15.10        "/bin/bash"              2 months ago        Exited (0) 8 weeks ago                       blissful_lewin

[root@node03 ~]# docker start 7cb788123876

7cb788123876

[root@node03 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

7cb788123876        ubuntu:15.10        "/bin/bash"         2 months ago        Up 9 seconds                            blissful_lewin

[root@node03 ~]# docker run -it --name ubuntu_yc ubuntu:15.10

root@cde36937797f:/# ls

bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

 

例子:拉取centos镜像运行

[root@node03 ~]# docker pull centos:6.9

[root@node03 ~]# docker run -it --name centos_yc centos:6.9

[root@cb2089a95653 /]# ifconfig

eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03 

          inet addr:172.17.0.3  Bcast:172.17.255.255  Mask:255.255.0.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:8 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:648 (648.0 b)  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)

 

[root@cb2089a95653 /]# ps -ef

UID         PID   PPID  C STIME TTY          TIME CMD

root          1      0  0 12:25 pts/0    00:00:00 /bin/bash

root         13      1  0 12:25 pts/0    00:00:00 ps -ef

[root@cb2089a95653 /]# exit

[root@node03 ~]#

 

4、容器启动后面命令

命令是容器在启动的时候要执行的命令,不跟命令启动默认的命令

 

例如如果跟了ps -ef 那么执行完容器就运行完毕

[root@node03 ~]# docker run -it  ubuntu:15.10 ps -ef

UID         PID   PPID  C STIME TTY          TIME CMD

root          1      0  0 12:47 pts/0    00:00:00 ps -ef

root@node03 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES

a7837d2a610f        ubuntu:15.10          "ps -ef"                     49 seconds ago         Exited (0) 48 seconds ago                         agitated_rhodes

5、进入一个正在运行的容器

(1)attach进入容器

[root@node03 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

cb2089a95653        centos:6.9          "/bin/bash"         11 minutes ago      Up 4 seconds                            centos_yc

7cb788123876        ubuntu:15.10        "/bin/bash"         2 months ago        Up 48 minutes                           blissful_lewin

[root@node03 ~]# docker attach cb2089a95653

[root@cb2089a95653 /]#

注:使用attach进入容器如果有多个客户端,客户端会同步所有操作,有任何一个窗口退出ctrl+c退出容器,容器就死了。如下图所示

 三、docker容器的常用命令

(2)exec进入容器

画面不同步

 三、docker容器的常用命令

进入容器后推出的时候ctrl +p 然后ctrl+q退出后容器不会死

 三、docker容器的常用命令

6、停止一个容器

root@node03 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

cb2089a95653        centos:6.9          "/bin/bash"         26 minutes ago      Up 14 minutes                           centos_yc

7cb788123876        ubuntu:15.10        "/bin/bash"         2 months ago        Up About an hour                        blissful_lewin

 

停止容器

[root@node03 ~]# docker stop cb2089a95653

cb2089a95653

[root@node03 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

7cb788123876        ubuntu:15.10        "/bin/bash"         2 months ago        Up About an hour                        blissful_lewin

 

杀死容器

[root@node03 ~]# docker kill 7cb788123876

7cb788123876

[root@node03 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

7、删除容器

[root@node03 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES

a7837d2a610f        ubuntu:15.10        "ps -ef"                 8 minutes ago       Exited (0) 8 minutes ago                          agitated_rhodes

cb2089a95653        centos:6.9          "/bin/bash"              31 minutes ago      Exited (0) 4 minutes ago                          centos_yc

cde36937797f        ubuntu:15.10        "/bin/bash"              About an hour ago   Exited (127) 47 minutes ago                       ubuntu_yc

9dc5421ea021        nginx:latest        "nginx -g ‘daemon of…"   7 weeks ago         Exited (0) 7 weeks ago                            reverent_lamport

7cb788123876        ubuntu:15.10        "/bin/bash"              2 months ago        Exited (137) 4 minutes ago                        blissful_lewin

[root@node03 ~]# docker rm 7cb788123876

 

强制删除一个正在运行的容器

[root@node03 ~]# docker rm -f 7cb788123876

 

一次删除所有容器

 三、docker容器的常用命令

 

 

删除所有容器

docker rm `docker ps -a -q`

强制删除所有容器

docker rm -f `docker ps -a -q`

 

8、修改容器中文件(容器里没有vi等工具)

[root@node03 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES

9dc5421ea021        nginx:latest        "nginx -g ‘daemon of…"   7 weeks ago         Exited (0) 7 weeks ago                               reverent_lamport

[root@node03 ~]# docker cp 9dc5421ea021:/etc/nginx/nginx.conf /root

[root@node03 ~]# ls

anaconda-ks.cfg  initial-setup-ks.cfg  nginx.conf  openstack-swift-object  ubuntu:v1.tar.gz

[root@node03 ~]# docker cp  /root/nginx.conf  9dc5421ea021:/etc/nginx/nginx.conf

 

总结:

docker容器内的第一个进程必须一直处于前台运行的状态(必须夯住),否则这个容器就会处于退出状态。

三、docker容器的常用命令

上一篇:linux下rocksdb的编译安装


下一篇:20155302 2016-2017-2 《Java程序设计》第九周学习总结