ITPub博客

首页 > 自动化运维 > DevOps > docker笔记9-容器命令

docker笔记9-容器命令

原创 DevOps 作者:czxin788 时间:2018-08-30 11:13:10 1 删除 编辑

     有镜像,才能创建容器,这是根本的前提。

     下面我们以下载一个centos镜像来做演示。

[root@t-docker chenzx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@t-docker chenzx]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Already exists 
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
[root@t-docker chenzx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              5182e96772bf        3 weeks ago         200MB

以交互式方式启动容器

    功能:新建并启动容器

    参数: 

 --name='容器新名字':为容器指定一个别名;
 -d:后台运行容器,并返回容器ID,也就是启动守护式容器;
 -i:以交互式模式运行容器,通常与-t同时使用;
 -t:tty,为容器重新分配一个伪终端,通常与-i同时使用;
 -P:随机端口映射;
 -p:指定端口映射,有以下四种格式:
     ip:hostPort:containerPort
     ip::containerPort
     hostPort:containerPort
     containerPort

    

[root@t-docker chenzx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              5182e96772bf        3 weeks ago         200MB
[root@t-docker chenzx]# 
[root@t-docker chenzx]# docker run -it 5182e96772bf  ##这句话就是说我以centos镜像为模板,生成一个23c6bc378d16这样的一个容器
[root@23c6bc378d16 /]# 
[root@23c6bc378d16 /]# ls ##这时已经进入容器的centos里面了
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

    再开另外一个窗口,在宿主机上执行如下命令,看到启动的容器:

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7da6063256a0        centos              "/bin/bash"         6 seconds ago       Up 5 seconds                            practical_ardinghelli

[root@t-docker chenzx]# docker run -it --name mycentos0115 centos

[root@ 0ecd4b9f8895 /]# #看到我们又生成了一个以centos镜像为模板,id为 0ecd4b9f8895的新容器。

画外音:一个镜像可以生成多个容器。

[root@t-docker chenzx]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

0ecd4b9f8895        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            mycentos0115

[root@t-docker chenzx]# 

docker ps

    功能:查看有哪些容器在跑,在宿主机上执行该命令。

    参数:

-a:列出当前所有正在运行的容器+历史上运行过的;
-l:last,显示最近创建的容器;
-n:显示最近n个创建的容器;
-q:静默模式,只显示容器编号
--no-trunc:不截断输出
[root@t-docker chenzx]# docker ps ##不加任何参数,表示当前正在运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7da6063256a0        centos              "/bin/bash"         6 seconds ago       Up 5 seconds                            practical_ardinghelli

退出容器

exit:容器停止退出

ctrl+P+Q:容器不停止退出

启动容器

先查出最近启动的容器,看到它们是关闭的状态:

[root@t-docker chenzx]# docker ps -n 2
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         7 minutes ago       Exited (137) 16 seconds ago                       mycentos0115
7da6063256a0        centos              "/bin/bash"         23 minutes ago      Exited (130) 4 minutes ago                        practical_ardinghelli

启动容器:

[root@t-docker chenzx]# docker start 0ecd4b9f8895  ##注意加的是上面ps查出的容器id
0ecd4b9f8895
[root@t-docker chenzx]# docker ps #查看当前正在运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         8 minutes ago       Up 5 seconds                            mycentos0115

重启容器

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         10 minutes ago      Up 2 minutes                            mycentos0115
[root@t-docker chenzx]# docker restart 0ecd4b9f8895
0ecd4b9f8895

停止容器

[root@t-docker chenzx]#  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         12 minutes ago      Up About a minute                       mycentos0115
[root@t-docker chenzx]# 
[root@t-docker chenzx]# docker stop 0ecd4b9f8895 
0ecd4b9f8895

强制停止容器

[root@t-docker chenzx]#  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         13 minutes ago      Up 2 seconds                            mycentos0115
[root@t-docker chenzx]# 
[root@t-docker chenzx]# docker kill 0ecd4b9f8895
0ecd4b9f8895

删除已经停止的容器

docker rm:删除容器

docker rmi: 删除镜像

[root@t-docker chenzx]#  docker ps -n 2
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
0ecd4b9f8895        centos              "/bin/bash"         15 minutes ago      Exited (137) About a minute ago                       mycentos0115
7da6063256a0        centos              "/bin/bash"         31 minutes ago      Exited (130) 11 minutes ago                           practical_ardinghelli
[root@t-docker chenzx]# docker rm 0ecd4b9f8895  删除已经关闭的容器
0ecd4b9f8895
[root@t-docker chenzx]# docker rm  -f 0ecd4b9f8895  强制删除正在运行中的容器
0ecd4b9f8895

一次性删除多个容器:

docker rm -rf $(docker ps -a -q)

docker ps -a -q |xargs docker rm

以守护式方式启动容器

    命令格式:docker run -d 容器名

    参数说明:

         -d:以守护式方式启动容器:

         -it:以交互式方式启动容器;

[root@t-docker chenzx]# docker run -d centos
fa80295c878acdf30531ecbf58be87b00427e4f941d10edf93b5d066ba40d000
[root@t-docker chenzx]# docker ps 没有输出,奇怪吗
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

    问题:上面我们看到通过docker run -d ceonts方式(即守护式启动容器)启动容器后,再通过docker ps -a进行查看,会发现容器已经退出。很重要的说明一点: Docker容器后台运行,就必须有一个前台进程。

    容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

    这个是docker机制的问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动相应的service即可。例如service nginx start。但是,这样做,nginx为后台模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事情可做了。所以, 最佳的解决方案是,将你要运行的程序以前台进程的形式运行。


[root@t-docker chenzx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              690cb3b9c7d1        7 days ago          463MB
centos              latest              5182e96772bf        3 weeks ago         200MB
[root@t-docker chenzx]# 
[root@t-docker chenzx]# docker run -it centos
[root@50fffecec77f /]# exit
exit
[root@t-docker chenzx]# docker run -it centos /bin/bash ##这个表示使用镜像centos:latest以交互式模式启动一个容器,并在容器内执行/bin/bash命令
[root@4eeeb44eef56 /]# exit
exit
[root@t-docker chenzx]# docker run -it centos /bin/bash


    上面例子是用-it以交互式启动的容器。

    那么你可能要问了,用-d以守护式启动容器有什么用呢。

查看容器日志

    命令格式:docker logs -f -t --tail 容器id

    参数说明:

        -t是加入时间戳

        -f是跟随最新的日志打印

        --tail是Linux命令,表示显示最后多少条

[root@t-docker chenzx]# docker run -d centos /bin/sh  -c "while true;do echo hello zzyy;sleep 2;done"
b118747a68bbfc6698f9ef9e5d817c037c6ecb9835b86964f6a8b90683dcd178
[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b118747a68bb        centos              "/bin/sh -c 'while t…"   5 seconds ago       Up 4 seconds                            elegant_euclid

    说明:上面命令的意思是用-d以后台方式运行容器centos,并在容器里面运行命令/bin/bash -c "while true;do echo hello zzyy;sleep 2;done"。最后我们通过docker ps看status是up状态,说明我们虽然以后台的方式启动了,但是这次容器并没有退出。

root@t-docker chenzx]#man sh
    -c string If the -c option is present, then commands are read from string.  If there are arguments after the string,  they
                 are assigned to the positional parameters, starting with $0.

    通过如下日志可以证明后台-d方式运行的容器没有停止:

[root@t-docker chenzx]# docker logs -t -f --tail 3 b118747a68bb
2018-08-30T01:47:55.198597337Z hello zzyy
2018-08-30T01:47:57.199661966Z hello zzyy
2018-08-30T01:47:59.200811444Z hello zzyy
2018-08-30T01:48:01.202114367Z hello zzyy
2018-08-30T01:48:03.203363568Z hello zzyy

查看容器内的进程

    命令格式:docker top 容器id

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b118747a68bb        centos              "/bin/sh -c 'while t…"   8 minutes ago       Up 8 minutes                            elegant_euclid
[root@t-docker chenzx]# docker top b118747a68bb
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2952                2934                0                   09:43               ?                   00:00:00            /bin/sh -c while true;do echo hello zzyy;sleep 2;done
root                3410                2952                0                   09:52               ?                   00:00:00            sleep 2

查看容器内的细节

    命令格式:docker inspect 容器id

    该命令会以json形式返回。

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b118747a68bb        centos              "/bin/sh -c 'while t…"   13 minutes ago      Up 13 minutes                           elegant_euclid
[root@t-docker chenzx]# docker inspect b118747a68bb
[
    {
        "Id": "b118747a68bbfc6698f9ef9e5d817c037c6ecb9835b86964f6a8b90683dcd178",
        "Created": "2018-08-30T01:43:24.433793568Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo hello zzyy;sleep 2;done"
   .....

进入正在运行的容器并以命令行交互

    重新进入容器的两个命令:

        docker exec -it 容器id bash shell

        docker attach 容器id

    上面两个命令的区别:

        attach:直接进入容器启动命令的终端,不会启动新的进程;

        exec:是在容器中打开新的终端,并且可以启动新的进程。

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@t-docker chenzx]# docker run -it centos
[root@3d6cf42834fa /]# 启动一个容器实例并进入容器里面
[root@3d6cf42834fa /]# pwd
/
[root@3d6cf42834fa /]# exit
exit
[root@t-docker chenzx]# docker run -it centos /bin/bash
[root@3cb3babb797f /]# pwd
/

    说明:docker run -it centos和docker run -it centos /bin/bash是一样的。也就是说docker run -it centos,就等价于

docker run -it centos /bin/bash。

    继续我们的实验:

[root@t-docker chenzx]# docker run -it centos
[root@04b568f7ee84 /]# ctrl+P+Q:容器不停止退出
[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
04b568f7ee84        centos              "/bin/bash"         29 seconds ago      Up 28 seconds              
[root@t-docker chenzx]# docker attach 04b568f7ee84
[root@04b568f7ee84 /]# ls /tmp
ks-script-Lu6hIQ  yum.log
[root@t-docker chenzx]# docker exec -it f188fac12d4f ls /tmp
ks-script-Lu6hIQ  yum.log
说明: -t表示--tty

    上面我们的实验,可以看到以attch方式进入运行中的容器,是需要在容器里面的命令行里面执行ls /tmp操作;而exec方式进入运行中的容器,是直接在exec命令后面写ls /tmp的操作,并把结果返回到宿主机的界面上。不好意思,表达的可能有点绕,看上面例子就会明白。

    不过,exec也有进入容器执行命令的功能(和attach功能一样),如下:

[root@t-docker chenzx]# docker exec -it f188fac12d4f /bin/bash
[root@f188fac12d4f /]# ls /tmp
ks-script-Lu6hIQ  yum.log

    所以,exec的功能比attach功能强大,建议只学exec的方式进入容器。

从容器内拷贝文件到宿主机上

    应用场景:当我们以前台交互式方式运行容器后,如果在容器里面执行exit操作,容器就会停止,此时容器中运行的数据也就没有了。这时我们就需要把运行中的容器数据拷贝到宿主机上来保留。

    命令格式:docker cp 容器id或容器名:容器内路径 目的宿主机路径

[root@t-docker tmp]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f188fac12d4f        centos              "/bin/bash"         25 minutes ago      Up 25 minutes                           heuristic_nightingale
[root@t-docker tmp]# docker cp f188fac12d4f:/tmp/yum.log /tmp/
[root@t-docker tmp]# ls
yum.log

从宿主机上拷贝文件到容器内

    命令格式:docker cp 宿主机路径  容器id或容器名:容器内路径 

容器的导入和导出

1
2
3
[root@#localhost docker]# docker run -it  ubuntu:update /bin/bash
root@cbe 3 cb 7799 ed:/# ls
bin  boot  dev  etc  home  lib  lib 64    media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  update.txt  usr  var


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@#localhost docker]# docker ps -a
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS                        PORTS                                   NAMES
cbe 3 cb 7799 ed        ubuntu:update                   "/bin/bash"                47   seconds ago      Exited ( 1 )  6   seconds ago                                              adoring_kare
812 a 997 f 614 a        ubuntu: 14.04                     "/bin/bash"                16   minutes ago      Exited ( )  8   minutes ago                                              zealous_euler
69304 dea 46 c 7          gitlab/gitlab-ce:latest         "/assets/wrapper"          2   hours ago         Exited ( 127 )  53   minutes ago                                           competent_minsky
67 ba 866 e 21 b          gitlab/gitlab-ce:latest         "/assets/wrapper"          2   hours ago         Exited ( 137 )  2   hours ago                                              hungry_hoover
2 a 3 d 08 a a 2 ff        twang 2218 /gitlab-ce-zh: 9.0 . 3     "/assets/wrapper"          2   hours ago         Exited ( 137 )  2   hours ago                                              nervous_wozniak
6 db 49540 be 99          twang 2218 /gitlab-ce-zh: 9.0 . 3     "/assets/wrapper"          2   hours ago         Exited ( 255 )  2   hours ago       22 /tcp,  443 /tcp,  0.0 . 0.0: 3000 -> 80 /tcp   romantic_elion
b 08 a 6 d 6 ed 716          gitlab/gitlab-ce:latest         "/assets/wrapper"          2   hours ago         Exited ( 255 )  2   hours ago       22 /tcp,  443 /tcp,  0.0 . 0.0: 8080 -> 80 /tcp   competent_brahmagupta
33 fd b 1 ebd 27          gitlab/gitlab-ce:latest         "/assets/wrapper"          3   hours ago         Exited ( 127 )  2   hours ago                                              loving_brattain
6 f 53620 a 930 c        twang 2218 /gitlab-ce-zh: 9.0 . 3     "/assets/wrapper"          3   hours ago         Exited ( 127 )  2   hours ago                                              brave_galileo
88 df 78 f 77 c 4 e        ubuntu: 14.04                     "sleep 360"                4   days ago          Exited ( 137 )  4   days ago                                               testcopy
81 a 879 a 36 bd 3          wordpress                       "docker-entrypoint..."     4   days ago          Exited ( )  4   days ago                                                 wordpress
a 57 a 3 cc 492 b 7          mysql                           "docker-entrypoint..."     4   days ago          Exited ( )  4   days ago                                                 mysqlwp

# 将镜像导出到文件

1
[root@#localhost docker]# docker export cbe 3 cb 7799 ed > update.tar

# 创建一个新镜像从导出的文件导入

1
2
[root@#localhost /]# docker import - update < update.tar 
sha 256: fd 00 d 520 a 43 eb 5 dc 6 cca 8717 fe ca 04 cfdc 53 b 02 cad 2 fb 5 b 50 d 877 b 8 e 6 d 6 c 3 bc
1
2
3
4
5
6
7
8
9
10
[root@#localhost /]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
update                      latest              fd 00 d 520 a 43 e         13   seconds ago       165   MB
ubuntu                      update               317 f 10258460          11   minutes ago       188   MB
twang 2218 /gitlab-ce-zh       9.0 . 3                 36172 b 5 fefab         44   hours ago         1.19   GB
gitlab/gitlab-ce            latest               5 eff 2 e 44957 c         2   days ago           1.11   GB
mysql                       latest               9546 ca 122 d 3 a         8   days ago           407   MB
wordpress                   latest               4 ad 41 adc 2794          2   weeks ago          401   MB
ubuntu                       14.04                 7 c 09 e 61 e 9035          5   weeks ago          188   MB
daocloud.io/library/nginx    1.7 . 1                 e 3 e 043 d 3 ed 2 f         2   years ago          499   MB


其他容器命令

    还有其他一些docker容器常用命令,比如diff等等,平时基本不用,就先不学了。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28916011/viewspace-2213013/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-06-03

  • 博文量
    233
  • 访问量
    858429