dog1888 发表于 2017-12-6 19:43:05

Docker学习第二天-容器

Docker 容器
  容器是 Docker 又一核心概念。
  简单的说,容器是独立运行的一个或一组应用,以及它们的运行态环境。对应的,虚拟机可以理解为模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面的应用。
  本片文章将具体介绍围绕容器的重要操作,包括创建一个容器、启动容器、终止一个容器、进入容器内执行操作、删除容器和通过导入导出容器来实现容器的迁移。

启动容器
  启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。
  因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器。

新建并启动
  所需要的命令主要为 docker run。
  例如,下面的命令输出一个 “Hello World”,之后终止容器。



# docker run unbutn:02.09 /bin/echo 'Hello World'
Hello World
#
  这跟本地直接执行/bin/echo 'Hello World'几乎感觉没有任何区别。
  下面的命令则启动一个bash终端,允许用户进行交互。



# docker run -t -i unbutn:02.09 /bin/bash
root@797bce48e2ac:/#
  其中,-t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。
  在交互模式下,用户可以通过所创建的终端输入命令,例如



root@797bce48e2ac:/# pwd
/
root@797bce48e2ac:/# ls
binbootdevetchomeliblib64mediamntoptprocrootrunsbinselinuxsrvsystmpusrvar
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参数来实现。
  例如下面的命令会在后台运行容器。



# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done"
dd94131ba7cce93cffd1987095082160da1be8b13205ebb418782b89c88ec21a
  容器启动之后会返回一个唯一的ID,也可以通过docker ps 命令来查看容器的信息。



# 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命令。



# 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 来终止一个运行中的容器。



# 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         
# docker stop happy_morse
happy_morse
# docker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
#
  此外,当docker容器中指定的应用终结时,容器也自动终止。例如对于上面启动了一个终端的容器,用户通过exit命令或是ctrl + d 来退出终端时,所创建的容器会立刻终止。
  终止状态的容器也用docker ps -a 命令看到。例如



# 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         
#
  处于终止状态的容器,可以通过 docker start 命令来重新启动。
  此外,docker restart 命令会将一个运行态的容器终止,然后再重新启动它。

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

attach命令
  docker attach 是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         
# docker attach happy_morse
hello world
hello world
hello world
hello world
hello world
···
  但是使用attach命令有时候并不方便。当多个窗口同时attach到同一个容器的时候,所有窗口都会同步显示。当某个窗口因为命令阻塞时,其他窗口也无法执行操作了。

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



# 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         
# docker start 0f627067205b
0f627067205b
# docker exec -ti 0f627067205b /bin/bash
#
nsenter命令
  安装
  nsenter 工具在util-linux包2.23版本后包含。如果系统中没有util-linux包没有该命令,可以按照如下的方式从源码安装。



# 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 % XferdAverage Speed   Time    Time   TimeCurrent
DloadUpload   Total   Spent    LeftSpeed
100 7451k100 7451k    0   027704      00:04:350:04:35 --:--:-- 29805
# ./configure --without-ncurses
# 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
  下面一个完整的例子:



# docker run -idt ubuntu:12.04
ef321bb21bb77b6dc9cf9c1069c400552631d776b607cb8beb07e0db81f65bdf
# 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      
-MJSP-A35 bin]# PID=$(docker-pid ef321bb21bb7)
-bash: docker-pid: command not found
  尝试这种方式好多次一直没有执行处理,具体操作如下,大家要是发现哪有问题还请多多指点,谢谢。
  鉴于上面的方式不能查出容器的ID,那么咱换一直方式:



# 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登录



# nsenter --target 26205 --mount --uts --ipc --net --pid
root@ef321bb21bb7:/#
  感觉好像登录成功。
  退出之后查看容器还在,个人感觉跟attach相比就是退出之后容器不会关闭,大家有什么其他发现可以给我留言,我会不定期的更新。



root@ef321bb21bb7:/# logout
# 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
  查看所有的容器如下所示:



# 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         
#
  分别导出ef321bb21bb7和dd94131ba7cc容器到test_for_run.tar文件和test_for_stop.tar文件:



# docker export gloomy_ardinghelli >test_for_run.tar
# docker export happy_morse >test_for_stop.tar
# ll
total 487568
-rw-r--r-- 1 root root 210291971 Feb9 13:40 centos-6-x86-minimal.tar.gz
-rw-r--r-- 1 root root89469440 Feb9 20:02 test_for_run.tar
-rw-r--r-- 1 root root89468416 Feb9 20:03 test_for_stop.tar
-rw-r--r-- 1 root root 109515264 Feb9 14:00 ubuntu_12.04.tar
#
  可以将这些文件传输到其他机器上,在其他机器上通过导入命令实现容器的迁移。

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



# cat test_for_run.tar |docker import - test/ubuntu:v1.0
2c9f598772e33b68a2b58b05afc020b03cb289348257538bc62e9a3ae27b829b
# 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 CONTAINER。
  支持的选项包括:
  ·-f,--force=false强行终止并删除一个运行中的容器。
  ·-l,--link=false删除容器连接,但保留容器。
  ·-v,--volumes=false删除容器挂载的数据卷。
  例如,查看处于终止状态的容器并删除如下表示:



# 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         
# docker rm 6ef0c8e046d9
6ef0c8e046d9
# 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         
#
  如果删除一个运行中的容器,可以添加-f参数。Docker会发送SIGKILL信号给容器,终止其中的应用



# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done"
d677c7bb29203e770c23a745b7a5ac4dbb3891c467908fd1ab999e4b54e8a25e
# 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         
# 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:
# docker rm -f backstabbing_mayer
backstabbing_mayer
# 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         
#
页: [1]
查看完整版本: Docker学习第二天-容器