Docker入门与应用系列(三)容器管理

一、启动容器

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

1.1 新建并启动

主要命令为  docker run
下面的命令输出一个”Hello,world!”,之后终止容器

[bigberg@localhost ~]$ docker run centos:7 /bin/echo "hello,world"
hello,world

  启动一个bash终端,允许用户进行交互

  # docker run -it centos /bin/bash

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

  在交互模式下,用户可以所创建的终端来输入命令:  

[bigberg@localhost ~]$ docker run -it centos:7 /bin/bash
[root@8e390ccc6898 /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
[root@8e390ccc6898 /]# uname -a
Linux 8e390ccc6898 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

  

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

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

1.2 启动已经终止的容器

  利用 docker start [container_id/name] 可以启动一个已经终止的容器。 

[bigberg@localhost ~]$ docker start 8e390ccc6898 
8e390ccc6898

注意! 容器不是一个虚拟机,因为他就是一个进程,如果我们退出,这个进程就退出了。

二、守护状态运行

  更多的时候,需要让 Docker在后台运行而不是直接把执行命令的结果输出在当前宿主机下。此时,可以通过添加 -d 参数实现。  

[bigberg@localhost ~]$ docker run --name mytest -it -d centos:7 /bin/bash
0cd0fd089ea0bc27c6e25bdbf7a4dafde11769aeeffec3a0f4a21ce0d0bbdcd8
[bigberg@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS 
0cd0fd089ea0        centos:7            "/bin/bash"         15 seconds ago      Up 14 seconds

  --name : 未容器命名

  -d : 运行在后台

三、停止容器  

在宿主机可以使用 docker stop 来终止一个容器

在Docker容器中,可以使用 exit 或者 ctl+d 来退出终端,并终止容器。

[bigberg@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0cd0fd089ea0        centos:7            "/bin/bash"         10 minutes ago      Up 7 minutes                            mytest
8e390ccc6898        centos:7            "/bin/bash"         2 hours ago         Up 14 minutes                           loving_engelbart
[bigberg@localhost ~]$ docker stop mytest
mytest

  查看所有容器信息

[bigberg@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS               
0cd0fd089ea0        centos:7            "/bin/bash"              11 minutes ago      Exited (137) 37 secon
8e390ccc6898        centos:7            "/bin/bash"              2 hours ago         Up 15 minutes 

  Exited 表示已经终止的容器

  此外,我们可以用docker restat 命令来重启一个容器。

四、进入容器

  当我们需要进入容器进行操作时,有很多种方法,包括使用docker attach命令或 nsenter工具等。

  4.1 attach 命令

  docker attach 命令是Docker自带的命令。  

[bigberg@localhost ~]$ docker attach mytest
[root@0cd0fd089ea0 /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr

  但是使用 attach 命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了。

  4.2 nsenter 命令 

[root@localhost ~]# yum install util-linux -y 
Centos7默认最小化已经安装
我们通过nsenter就可以进入容器,但是nsenter是通过pid进入容器里,所以我们需要知道容器的pid。我们可以通过docker inspect来获取到pid

  

  为了连接到容器,你还需要找到容器的第一个进程的 PID,可以通过下面的命令获取。

# PID=$(docker inspect --format "{{.State.Pid}}" dtest)
# nsenter --target $PID --mount --uts --ipc --net –pid

  

  退出 Ctl+ddocker 容器会保持运行状态

  4.3 .bashrc_docker

   更简单的,建议大家下载 .bashrc_docker,并将内容放到 .bashrc 中。  

# Some useful commands to use docker.
# Author: yeasy@github
# Created:2014-09-25

alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'"
alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"

#the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
function docker-enter() {
    #if [ -e $(dirname "$0")/nsenter ]; then
    #Change for centos bash running
    if [ -e $(dirname '$0')/nsenter ]; then
        # with boot2docker, nsenter is not in the PATH but it is in the same folder
        NSENTER=$(dirname "$0")/nsenter
    else
        # if nsenter has already been installed with path notified, here will be clarified
        NSENTER=$(which nsenter)
        #NSENTER=nsenter
    fi
    [ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return

    if [ -z "$1" ]; then
        echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
        echo ""
        echo "Enters the Docker CONTAINER and executes the specified COMMAND."
        echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
    else
        PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1")
        if [ -z "$PID" ]; then
            echo "WARN Cannot find the given container"
            return
        fi
        shift

        OPTS="--target $PID --mount --uts --ipc --net --pid"

        if [ -z "$1" ]; then
            # No command given.
            # Use su to clear all host environment variables except for TERM,
            # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
            # and start a login shell.
            #sudo $NSENTER "$OPTS" su - root
            sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
        else
            # Use env to clear all host environment variables.
            sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
        fi
    fi
}

  

echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc

  这个文件中定义了很多方便使用 Docker 的命令,例如 docker-pid 可以获取某个容器的 PID;而 docker-enter 可以进入容器或直接在容器内执行命令。 

[bigberg@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0cd0fd089ea0        centos:7            "/bin/bash"         About an hour ago   Up About a minute                       mytest
8e390ccc6898        centos:7            "/bin/bash"         3 hours ago         Up About an hour                        loving_engelbart
[bigberg@localhost ~]$ docker-pid mytest
6641
[bigberg@localhost ~]$ docker-enter mytest
Last login: Sat Feb 24 06:50:02 UTC 2018
[root@0cd0fd089ea0 ~]# ls
anaconda-ks.cfg
[root@0cd0fd089ea0 ~]# ifconfig

  

五、导出和导入容器

5.1 导出容器

  如果要导出本地某个容器,可以使用docker export 命令 

[bigberg@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS 
0cd0fd089ea0        centos:7            "/bin/bash"         2 hours ago         Up About an hour          
8e390ccc6898        centos:7            "/bin/bash"         5 hours ago         Up 3 hours                
[bigberg@localhost ~]$ docker export mytest > centos7.tar.gz
[bigberg@localhost ~]$ ll
total 606476
-rw-rw-r-- 1 bigberg bigberg 292853760 Feb 24 16:12 centos7.tar.gz

5.2 导入容器

[bigberg@localhost ~]$ cat centos7.tar.gz | docker import - test/centos:v1.0
sha256:1c2b77ab34f25c54b0d0f835c7dea6cf57876ace264c5dc0c7e886e1b524e604
[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/centos         v1.0                1c2b77ab34f2        6 seconds ago       284MB
nginx               latest              e548f1a579cf        3 days ago          109MB
centos              7                   ff426288ea90        6 weeks ago         207MB
centos              latest              ff426288ea90        6 weeks ago         207MB

六、删除容器  

  可以使用 docker rm 来删除一个处于终止状态的容器

[bigberg@localhost ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
0cd0fd089ea0        centos:7            "/bin/bash"              3 hours ago         Up 2 hours                                        mytest
8e390ccc6898        centos:7            "/bin/bash"              5 hours ago         Exited (137) 19 seconds ago                       loving_engelbart
7b34e5fd77f1        centos:7            "/bin/echo hello,wor…"   5 hours ago         Exited (0) 5 hours ago                            heuristic_lovelace
26b037aa1a09        centos              "/bin/echo hello,wor…"   5 hours ago         Exited (0) 5 hours ago                            distracted_mirzakhani
[bigberg@localhost ~]$ docker rm 7b34e5fd77f1
7b34e5fd77f1

七、容器重命名 

[bigberg@localhost ~]$ docker rename mytest my-test
[bigberg@localhost ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0cd0fd089ea0        centos:7            "/bin/bash"         3 hours ago         Up 2 hours                              my-test

  

八、查看容器运行

docker ps #查看已经运行的容器
docker ps -a #查看所有容器(运行的和不运行的)
docker ps -l #查看最新创建的容器
docker ps -q #只显示所有容器的PID
docker ps -s #显示所有容器的大小

  

九、查看容器的信息

docker inspect my-test

  

十、查看容器端口映射

docker port my-test

  

十一、复制文件到容器中

###复制宿主机文件到容器中
[bigberg@localhost ~]$ docker cp test.txt my-test:/tmp/
[bigberg@localhost ~]$ docker exec -it my-test ls /tmp
ks-script-Rl3Umm  test.txt  yum.log


### 复制容器中文件到宿主机

[bigberg@localhost ~]$ sudo docker cp my-test:/tmp/test.txt /home
[bigberg@localhost ~]$ ll /home/test.txt 
-rw-rw-r-- 1 root root 0 Feb 24 16:52 /home/test.txt

  

十二、查看日志

docker logs my-test

  

原文地址:https://www.cnblogs.com/bigberg/p/8465033.html