Docker学习次日-容器

Docker 容器

容器是 Docker 又一核心概念。linux

简单的说,容器是独立运行的一个或一组应用,以及它们的运行态环境。对应的,虚拟机能够理解为模拟运行的一整套操做系统(提供了运行态环境和其余系统环境)和跑在上面的应用。docker

本片文章将具体介绍围绕容器的重要操做,包括建立一个容器、启动容器、终止一个容器、进入容器内执行操做、删除容器和经过导入导出容器来实现容器的迁移。ubuntu

启动容器

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另一个是将在终止状态(stopped)的容器从新启动。centos

由于 Docker 的容器实在过轻量级了,不少时候用户都是随时删除和新建立容器。bash

新建并启动

所须要的命令主要为 docker runapp

例如,下面的命令输出一个 “Hello World”,以后终止容器。curl

[root@MSJTVL-MJSP-A35 docker]# docker run unbutn:02.09 /bin/echo 'Hello World'
Hello World
[root@MSJTVL-MJSP-A35 docker]# 

这跟本地直接执行/bin/echo 'Hello World'几乎感受没有任何区别。工具

下面的命令则启动一个bash终端,容许用户进行交互。url

[root@MSJTVL-MJSP-A35 docker]# docker run -t -i unbutn:02.09 /bin/bash
root@797bce48e2ac:/# 

其中,-t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。spa

在交互模式下,用户能够经过所建立的终端输入命令,例如

root@797bce48e2ac:/# pwd
/
root@797bce48e2ac:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var
root@797bce48e2ac:/# cd /etc/       

当利用 docker run 来建立容器时,Docker 在后台运行的标准操做包括:

  • 检查本地是否存在指定的镜像,不存在就从公有仓库下载
  • 利用镜像建立并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止    

启动已终止容器

能够利用 docker start 命令,直接将一个已经终止的容器启动运行。

容器的核心为所执行的应用程

root@797bce48e2ac:~# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
   17 ?        00:00:00 ps
root@797bce48e2ac:~# 

可见,容器中仅运行了指定的bash应用。这种特色使得Docker对资源的利用率极高,是货真价实的轻量级虚拟化。

 

守护态运行

更多的时候,须要让Docker容器在后台以守护(Daemonized)形式运行。此时,能够经过添加 -d参数来实现。

例以下面的命令会在后台运行容器。

[root@MSJTVL-MJSP-A35 docker]# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done"
dd94131ba7cce93cffd1987095082160da1be8b13205ebb418782b89c88ec21a

容器启动以后会返回一个惟一的ID,也能够经过docker ps 命令来查看容器的信息。

[root@MSJTVL-MJSP-A35 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   23 seconds ago      Up 21 seconds                           happy_morse         

要获取容器的输出信息,能够经过docker logs命令。

[root@MSJTVL-MJSP-A35 docker]# docker logs happy_morse
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
······

终止容器

可使用docker stop 来终止一个运行中的容器。

[root@MSJTVL-MJSP-A35 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   About an hour ago   Up 2 seconds                            happy_morse         
[root@MSJTVL-MJSP-A35 ~]# docker stop happy_morse
happy_morse
[root@MSJTVL-MJSP-A35 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@MSJTVL-MJSP-A35 ~]# 

此外,当docker容器中指定的应用终结时,容器也自动终止。例如对于上面启动了一个终端的容器,用户经过exit命令或是ctrl + d 来退出终端时,所建立的容器会马上终止。

终止状态的容器也用docker ps -a 命令看到。例如

[root@MSJTVL-MJSP-A35 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   10 minutes ago      Up 10 minutes                                   happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            13 minutes ago      Exited (0) 11 minutes ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            40 minutes ago      Exited (0) 14 minutes ago                       stupefied_mclean       
6ef0c8e046d9        unbutn:02.09        "/bin/echo 'Hello Wo   43 minutes ago      Exited (0) 43 minutes ago                       boring_elion           
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   43 minutes ago      Exited (0) 43 minutes ago                       berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            5 hours ago         Exited (0) 5 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            5 hours ago         Exited (127) 5 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            6 hours ago         Exited (0) 6 hours ago                          high_lovelace          
[root@MSJTVL-MJSP-A35 docker]# 

处于终止状态的容器,能够经过 docker start 命令来从新启动。

此外,docker restart 命令会将一个运行态的容器终止,而后再从新启动它。

 

进入容器

在使用-d 参数时,容器启动后会进入后台。某些时候须要进入容器进行操做,有不少种方式,包括使用docker attach命令或nsenter 工具等。

attach命令

docker attach 是Docker自带的命令。下面示例如何使用该命令。

[root@MSJTVL-MJSP-A35 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   About an hour ago   Up About an hour                        happy_morse         
[root@MSJTVL-MJSP-A35 docker]# docker attach happy_morse
hello world
hello world
hello world
hello world
hello world
···

可是使用attach命令有时候并不方便。当多个窗口同时attach到同一个容器的时候,全部窗口都会同步显示。当某个窗口由于命令阻塞时,其余窗口也没法执行操做了。

exec命令

Docker自1.3版本起,提供了一个更加方便的工具exec,能够直接在容器内运行命令。例如进入到刚建立的容器中,并启动一个bash:

[root@MSJTVL-MJSP-A35 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                         PORTS               NAMES
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   About an hour ago   Exited (137) 9 minutes ago                         happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            About an hour ago   Exited (0) About an hour ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            2 hours ago         Exited (0) About an hour ago                       stupefied_mclean       
6ef0c8e046d9        unbutn:02.09        "/bin/echo 'Hello Wo   2 hours ago         Exited (0) 2 hours ago                             boring_elion           
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   2 hours ago         Exited (0) 2 hours ago                             berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            6 hours ago         Exited (0) 6 hours ago                             cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            7 hours ago         Exited (127) 7 hours ago                           compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            7 hours ago         Exited (0) 7 hours ago                             high_lovelace          
[root@MSJTVL-MJSP-A35 ~]# docker start 0f627067205b
0f627067205b
[root@MSJTVL-MJSP-A35 ~]# docker exec -ti 0f627067205b /bin/bash
[root@0f627067205b /]# 

nsenter命令

安装

nsenter 工具在util-linux包2.23版本后包含。若是系统中没有util-linux包没有该命令,能够按照以下的方式从源码安装。

[root@MSJTVL-MJSP-A35 tmp]# cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24;
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7451k  100 7451k    0     0  27704      0  0:04:35  0:04:35 --:--:-- 29805
[root@MSJTVL-MJSP-A35 util-linux-2.24]# ./configure --without-ncurses
[root@MSJTVL-MJSP-A35 util-linux-2.24]# make nsenter && cp nsenter /usr/local/bin

 使用

nsenter能够访问另外一个进程的名字空间。nsenter要正常工做须要有root权限。

为了使用nsenter链接到容器,还须要找到容器进程的PID,能够经过下面的命令获取:

PID=$(docker inspect --format "{{ .State.Pid }}" <container>)

经过这个PID能够链接到这个容器:

$ nsenter --target $PID --mount --uts --ipc --net --pid

下面一个完整的例子:

[root@MSJTVL-MJSP-A35 bin]# docker run -idt ubuntu:12.04
ef321bb21bb77b6dc9cf9c1069c400552631d776b607cb8beb07e0db81f65bdf
[root@MSJTVL-MJSP-A35 bin]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"         15 seconds ago      Up 14 seconds                           gloomy_ardinghelli   
0f627067205b        centos:14.04        "/bin/bash"         2 hours ago         Up 52 minutes                           serene_poitras       
[root@MSJTVL-MJSP-A35 bin]# PID=$(docker-pid ef321bb21bb7) -bash: docker-pid: command not found

尝试这种方式好屡次一直没有执行处理,具体操做以下,你们要是发现哪有问题还请多多指点,谢谢。

鉴于上面的方式不能查出容器的ID,那么咱换一直方式:

 

[root@MSJTVL-MJSP-A35 ~]# docker top ef321bb21bb7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                26205               6841                0                   19:02               pts/4               00:00:00            /bin/bash

 

查出容器的ID是26205,接下来用nsenter登陆

[root@MSJTVL-MJSP-A35 ~]# nsenter --target 26205 --mount --uts --ipc --net --pid
root@ef321bb21bb7:/# 

感受好像登陆成功。

退出以后查看容器还在,我的感受跟attach相比就是退出以后容器不会关闭,你们有什么其余发现能够给我留言,我会不按期的更新。

root@ef321bb21bb7:/# logout
[root@MSJTVL-MJSP-A35 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"         34 minutes ago      Up 34 minutes                           gloomy_ardinghelli   
0f627067205b        centos:14.04        "/bin/bash"         3 hours ago         Up About an hour                        serene_poitras       

 导出和导入容器

导出容器

导出容器是指导出一个已经建立的容器到一个文件,无论此时这个容器是否处于运行状态,可使用docker export 命令,该命令格式为docker export CONTAINER

查看全部的容器以下所示:

 

[root@MSJTVL-MJSP-A35 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"            56 minutes ago      Up 8 seconds                                    gloomy_ardinghelli     
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   3 hours ago         Exited (137) 2 hours ago                        happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            3 hours ago         Exited (1) 32 seconds ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            4 hours ago         Exited (0) 3 hours ago                          stupefied_mclean       
6ef0c8e046d9        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 hours ago                          boring_elion           
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 hours ago                          berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            8 hours ago         Exited (0) 8 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            8 hours ago         Exited (127) 8 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (0) 9 hours ago                          high_lovelace          
[root@MSJTVL-MJSP-A35 ~]# 

分别导出ef321bb21bb7和dd94131ba7cc容器到test_for_run.tar文件和test_for_stop.tar文件:

[root@MSJTVL-MJSP-A35 docker]# docker export gloomy_ardinghelli >test_for_run.tar
[root@MSJTVL-MJSP-A35 docker]# docker export happy_morse >test_for_stop.tar
[root@MSJTVL-MJSP-A35 docker]# ll
total 487568
-rw-r--r-- 1 root root 210291971 Feb  9 13:40 centos-6-x86-minimal.tar.gz
-rw-r--r-- 1 root root  89469440 Feb  9 20:02 test_for_run.tar
-rw-r--r-- 1 root root  89468416 Feb  9 20:03 test_for_stop.tar
-rw-r--r-- 1 root root 109515264 Feb  9 14:00 ubuntu_12.04.tar
[root@MSJTVL-MJSP-A35 docker]# 

能够将这些文件传输到其余机器上,在其余机器上经过导入命令实现容器的迁移。

导入容器

导出的文件又可使用docker import 命令导入,成为镜像,例如:

[root@MSJTVL-MJSP-A35 docker]# cat test_for_run.tar |docker import - test/ubuntu:v1.0
2c9f598772e33b68a2b58b05afc020b03cb289348257538bc62e9a3ae27b829b
[root@MSJTVL-MJSP-A35 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
test/ubuntu         v1.0                2c9f598772e3        23 seconds ago      83.56 MB
centos              14.04               2197ed1032f7        6 hours ago         512.1 MB
test                latest              1a67c55ea394        8 hours ago         103.6 MB
ubuntu              latest              6aa0b6d7eb90        2 weeks ago         129.5 MB
ubuntu              12.04               8ea067ad7a27        7 weeks ago         103.6 MB
unbutn              02.09               8ea067ad7a27        7 weeks ago         103.6 MB

你们可能会记得,在以前的章节曾介绍过使用docker load命令来导入一个镜像文件。

实际上,既可使用docker load命令来导入镜像存储文件到本地的镜像库,又可使用docker import命令来导入一个容器快照到本地镜像库。这二者的区别在于容器快照文件将丢弃全部的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。此外,从容器快照文件导入时能够从新指定标签等元数据信息。

 

删除容器

可使用docker rm命令删除处于终止状态的容器,命令格式为docker rm[OPTIONS] CONTAINER[CONTAINER...]。

支持的选项包括:

·-f,--force=false强行终止并删除一个运行中的容器。

·-l,--link=false删除容器链接,但保留容器。

·-v,--volumes=false删除容器挂载的数据卷。

例如,查看处于终止状态的容器并删除以下表示:

[root@MSJTVL-MJSP-A35 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"            About an hour ago   Up 24 minutes                                   gloomy_ardinghelli     
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   3 hours ago         Exited (137) 2 hours ago                        happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            4 hours ago         Exited (1) 25 minutes ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            4 hours ago         Exited (0) 4 hours ago                          stupefied_mclean       
6ef0c8e046d9        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 hours ago                          boring_elion           
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 hours ago                          berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            8 hours ago         Exited (0) 8 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (127) 9 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (0) 9 hours ago                          high_lovelace          

[root@MSJTVL-MJSP-A35 docker]# docker rm 6ef0c8e046d9
6ef0c8e046d9
[root@MSJTVL-MJSP-A35 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"            About an hour ago   Up 25 minutes                                   gloomy_ardinghelli     
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   3 hours ago         Exited (137) 2 hours ago                        happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            4 hours ago         Exited (1) 25 minutes ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            4 hours ago         Exited (0) 4 hours ago                          stupefied_mclean       
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 hours ago                          berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            8 hours ago         Exited (0) 8 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (127) 9 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            10 hours ago        Exited (0) 9 hours ago                          high_lovelace          
[root@MSJTVL-MJSP-A35 docker]# 

若是删除一个运行中的容器,能够添加-f参数。Docker会发送SIGKILL信号给容器,终止其中的应用

[root@MSJTVL-MJSP-A35 docker]# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done"
d677c7bb29203e770c23a745b7a5ac4dbb3891c467908fd1ab999e4b54e8a25e
[root@MSJTVL-MJSP-A35 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
d677c7bb2920        ubuntu:12.04        "/bin/sh -c 'while t   5 seconds ago       Up 3 seconds                                    backstabbing_mayer     
ef321bb21bb7        ubuntu:12.04        "/bin/bash"            About an hour ago   Up 32 minutes                                   gloomy_ardinghelli     
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   4 hours ago         Exited (137) 2 hours ago                        happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            4 hours ago         Exited (1) 32 minutes ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            4 hours ago         Exited (0) 4 hours ago                          stupefied_mclean       
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 3 minutes ago                        berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (0) 9 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (127) 9 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            10 hours ago        Exited (0) 10 hours ago                         high_lovelace          
[root@MSJTVL-MJSP-A35 docker]# docker rm backstabbing_mayer
Error response from daemon: Cannot destroy container backstabbing_mayer: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error: failed to remove containers: [backstabbing_mayer]
[root@MSJTVL-MJSP-A35 docker]# docker rm -f backstabbing_mayer
backstabbing_mayer
[root@MSJTVL-MJSP-A35 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
ef321bb21bb7        ubuntu:12.04        "/bin/bash"            About an hour ago   Up 33 minutes                                   gloomy_ardinghelli     
dd94131ba7cc        ubuntu:12.04        "/bin/sh -c 'while t   4 hours ago         Exited (137) 2 hours ago                        happy_morse            
0f627067205b        centos:14.04        "/bin/bash"            4 hours ago         Exited (1) 33 minutes ago                       serene_poitras         
797bce48e2ac        unbutn:02.09        "/bin/bash"            4 hours ago         Exited (0) 4 hours ago                          stupefied_mclean       
64641a698106        unbutn:02.09        "/bin/echo 'Hello Wo   4 hours ago         Exited (0) 4 minutes ago                        berserk_feynman        
5b098fe766ca        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (0) 9 hours ago                          cranky_einstein        
c8c413078420        ubuntu:12.04        "/bin/bash"            9 hours ago         Exited (127) 9 hours ago                        compassionate_colden   
d67d32057f1c        ubuntu:12.04        "/bin/bash"            10 hours ago        Exited (0) 10 hours ago                         high_lovelace          
[root@MSJTVL-MJSP-A35 docker]# 
相关文章
相关标签/搜索