docker镜像与容器(二)

docker镜像与容器linux

docker改变了什么?docker

面向产品:产品交付shell

面向开发:简化环境配置ubuntu

面向测试:多版本测试centos

面向运维:环境一致性bash

面向架构:自动化扩容(微服务)服务器

 

获取镜像网络

可使用 docker pull命令来从仓库获取所须要的镜像。架构

[root@localhost~]# docker pull centos#获取一个centos的镜像运维

[root@localhost~]# docker p_w_picpaths#查看docker的镜像

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker.io/centos    latest              a8493f5f50ff        12 days ago         192.5 MB

在列出信息中,能够看到几个字段信息,

来自于哪一个仓库,好比centos,

镜像的标记,好比lastest

它的 ID 号( 惟一)

建立时间

镜像大小

 

dockerrmi + ID#删除镜像,若是这个镜像建立了容器就不能删除

建立而且启动第一个容器

[root@localhost ~]#docker run centos /bin/echo "hehe"

hehe

全部容器都是使用这个参数dockerruncentos指的是镜像的名称,后面跟的是命令(执行命令)

能够用dockerrun --help查看参数

4.1 docker help

docker command

docker   # docker 命令帮助
Commands:
    attach    Attach to a running container                 # 当前 shell 下 attach链接指定运行镜像
    build     Build an p_w_picpath from a Dockerfile              # 经过 Dockerfile 定制镜像
    commit    Create a new p_w_picpath from a container'schanges # 提交当前容器为新的镜像
    cp        Copy files/folders from the containersfilesystem to the host path
              #从容器中拷贝指定文件或者目录到宿主机中
    create    Create a new container                        # 建立一个新的容器,同run,但不启动容器
    diff      Inspect changes on a container'sfilesystem   # 查看 docker 容器变化
    events    Get real time events from the server          # 从 docker 服务获取容器实时事件
    exec      Run a command in an existingcontainer        #在已存在的容器上运行命令
    export    Stream the contents of a container as a tararchive  
              # 导出容器的内容流做为一个 tar 归档文件[对应import ]
    history   Show the history of an p_w_picpath                  # 展现一个镜像造成历史
    p_w_picpaths    List p_w_picpaths                                   #列出系统当前镜像
    import    Create a new filesystem p_w_picpath from thecontents of a tarball 
              # 从tar包中的内容建立一个新的文件系统映像[对应export]
    info     Display system-wide information               # 显示系统相关信息
    inspect   Return low-level information on a container   # 查看容器详细信息
    kill      Kill a running container                      # kill 指定 docker 容器
    load      Load an p_w_picpath from a tar archive              # 从一个 tar 包中加载一个镜像[对应save]
    login     Register or Login to the docker registryserver  
              # 注册或者登录一个 docker源服务器
    logout    Log out from a Docker registry server         # 从当前 Docker registry 退出
    logs      Fetch the logs of a container                 # 输出当前容器日志信息
    port      Lookup the public-facing port which isNAT-ed to PRIVATE_PORT
              #查看映射端口对应的容器内部源端口
    pause     Pause all processes within a container        # 暂停容器
    ps        List containers                               # 列出容器列表
    pull      Pull an p_w_picpath or a repository from thedocker registry server
              #从docker镜像源服务器拉取指定镜像或者库镜像
    push      Push an p_w_picpath or a repository to thedocker registry server
              #推送指定镜像或者库镜像至docker源服务器
    restart   Restart a running container                   # 重启运行的容器
    rm        Remove one or more containers                 # 移除一个或者多个容器
    rmi       Remove one or more p_w_picpaths                
              #移除一个或多个镜像[无容器使用该镜像才可删除,不然需删除相关容器才可继续或 -f 强制删除]
    run      Run a command in a new container
              #建立一个新的容器并运行一个命令
    save      Save an p_w_picpath to a tar archive                # 保存一个镜像为一个 tar 包[对应load]
    search    Search for an p_w_picpath on the Docker Hub         # 在 docker hub 中搜索镜像
    start     Start a stopped containers                    # 启动容器
    stop      Stop a running containers                     # 中止容器
    tag       Tag an p_w_picpath into a repository                # 给源中镜像打标签
    top       Lookup the running processes of acontainer   # 查看容器中运行的进程信息
    unpause   Unpause a paused container                    # 取消暂停容器
    version   Show the docker version information           # 查看 docker 版本号
    wait      Block until a container stops, then printits exit code  
              # 截取容器中止时的退出状态值
Run 'docker COMMAND --help' for more information on a comman

查看启动的docker

[root@localhost ~]#docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES

6daf1bacfa9c        centos              "/bin/echo hehe"    15 minutes ago      Exited (0) 15 minutes ago                       cranky_darwin

参数介绍:

首先是一个容器的ID,而后是镜像,后面是命令(COMMAND),建立的时间,状态,端口,名称(docker有名称库,不指定名字的时候会自动帮咱们产生名称)

 

建立一个指定名字的容器

[root@localhost ~]#docker run --name mydocker -t -i centos /bin/bash

参数介绍:

--name是指定docker的名字,-t 是让系统分配一个伪终端tty-i表示让这个容器的标准输入保持打开的状态(打开以后就能够登进去)(-t-i要一块儿使用),最后是?bin/bash表示执行一个命令,这个命令是/bin/bash.-h指定容器系统的主机名

[root@94ba510e3127 /]# ps -ef#能够查看这个容器运行了什么进程

UID        PID PPID  C STIME TTY          TIME CMD

root         1    0  0 10:43 ?        00:00:00 /bin/bash(这个进行是我启动容器的时候让他执行的)

root        13    1  9 10:48 ?        00:00:00 ps -ef

[root@localhost yum]#docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

94ba510e3127       centos             "/bin/bash"         7minutes ago       Up 7 minutes                            mydocker

能够看到docker的主机名跟容器的ID是同样的

当咱们执行dockersrun的时候的流程:首先docker会检测本地有没有一个叫centos的镜像,若是没有他就会从公共的仓库(dockerhub)去下载,第二他就利用centos的镜像启动了一个容器

能够用exit推出这个容器

[root@localhost ~]#docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES

94ba510e3127        centos              "/bin/bash"         12 minutes ago      Exited (0) 7 seconds ago                        mydocker

6daf1bacfa9c        centos              "/bin/echo hehe"    32 minutes ago      Exited (0) 32 minutes ago                       cranky_darwin

 

镜像操做的几个命令:

搜索镜像:dockersearch

获取镜像:dockerpull

查看镜像:dockerp_w_picpaths

删除镜像:dockerrmi

启动容器:dockerrun --name -hhostname

中止容器:docker stopCONTAINER ID

查看容器:dockersps-a-L(加了-a会显示全部的容器进程)

进入容器:dockerexec|dockerattach ID

删除容器:dockerrm

中止后启动容器:dockerstart ID

[root@localhost ~]#docker start 94ba510e3127

94ba510e3127

[root@localhost ~]#docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

94ba510e3127        centos              "/bin/bash"         28 minutes ago      Up 11 seconds                           mydocker

[root@localhost~]# docker attach  94ba510e3127#进入容器

[root@94ba510e3127 /]#ps aux

USER       PID %CPU %MEM    VSZ  RSS TTY      STAT START   TIME COMMAND

root         1 0.0  0.1  11768 1880 ?        Ss   11:12  0:00 /bin/bash

root        13 0.0  0.1  47440 1668 ?        R+   11:13  0:00 ps aux

用sttach进入的话有问题:若是你再开个窗户再进去的话他的显示是同步的,并且exit退出后这个容器就停了(由于咱们退出了bash)

能够用nsenter来进入(不过须要知道进程的PID)(若是没有这个命令的话能够用yuminstall util-linux来安装)

查看PID

[root@localhost ~]# docker inspect --format"``.`State`.`Pid`" 94ba510e3127(ID或者容器名)

20979

[root@localhost~]# nsenter -t 20979 -u -i -n -p#进入容器

[root@94ba510e3127 ~]#

[root@localhost ~]#nsenter -t 20979 -u -i -n -p

[root@94ba510e3127 ~]#nsenter --help

 

用法:

 nsenter [options] <program>[<argument>...]

 

Run a program withnamespaces of other processes.

 

选项:

 -t, --target <pid>     要获取名字空间的目标进程

 -m, --mount[=<file>]   enter mount namespace

 -u, --uts[=<file>]     enter UTS namespace (hostname etc)

 -i, --ipc[=<file>]     enter System V IPC namespace

 -n, --net[=<file>]     enter network namespace

 -p, --pid[=<file>]     enter pid namespace

 -U, --user[=<file>]    enter user namespace

 -S, --setuid <uid>     set uid in entered namespace

 -G, --setgid <gid>     set gid in entered namespace

     --preserve-credentials do not touch uidsor gids

 -r, --root[=<dir>]     set the root directory

 -w, --wd[=<dir>]       set the working directory

 -F, --no-fork          执行 <程序> 前不 fork

 -Z, --follow-context   set SELinux context according to --targetPID

 

 -h, --help    显示此帮助并退出

 -V, --version 输出版本信息并退出

能够把查找PID和登陆容器的命令写成脚本

[root@localhost ~]# vins.sh

#!/bin/bash

PID=$(docker inspect--format "``.`State`.`Pid`" $1)

nsenter -t $PID -u -i-n -p

这样子就只要传入一个容器IP就能够登录了

[root@localhost ~]#./ns.sh 94ba510e3127

[root@94ba510e3127 ~]#

[root@94ba510e3127~]# docker run --rm centos /bin/echo "hehe"#执行容器而且删除容器

hehe

删除全部的容器

dockerkill $(docker ps -a -q)

[root@94ba510e3127 ~]# docker ps -a -q#只列出PID

94ba510e3127

6daf1bacfa9c


ubuntu容器的基本操做:

ubuntu@ubuntu-virtual-machine:~$ docker run ubuntu echo "hello word"

hello word

 

启动交互式容器:

docker run -i -t IMAGE /bin/bash

-i--interactive=true |fasle 默认是fasle

-t --tty=true|fasle 默认是fasle

 

查看容器:

docker ps [-a] [-l]

-a 列出全部的容器,-l列出最新建立的容器

docker inspect 能够接容器的ID也能够是名字

会对容器详细的检查,返回配置的信息(包括名称,命令,网络配置等)

 

自定义容器命名:

docker run --name=container01 -I -t ubuntu /bin/bash

 

从新启动中止的容器:

docker start [-i] 容器名

 

删除已经中止的容器(不能删除运行中的)

docker rm ID

 

守护试容器:

什么是守护试容器:在大多数状况下,咱们须要一个可以长期运行的容器来提供服务。

可以长期运行

没有交互式会话

适合运行应用程序和服务

以守护形式运行容器:

1docker run -i-t IMAGE /bin/bash

Ctrl+p Ctrl +q结束的容器,这样子容器就会在后天运行

首先建立一个容器

buntu@ubuntu-virtual-machine:~$ docker run -i -t ubuntu /bin/bash

root@f2d00398e2c4:/# ubuntu@ubuntu-virtual-machine:~$ #Ctrl+p Ctrl +q结束的容器

而后

ubuntu@ubuntu-virtual-machine:~$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

f2d00398e2c4        ubuntu:latest       "/bin/bash"         37 seconds ago      Up 31 seconds                           cocky_lalande      

发现还在后台运行

 

怎么结束守护运行容器?

能够附加到运行中的容器:

docker attach 容器名

 

ubuntu@ubuntu-virtual-machine:~$ docker attach f2d00398e2c4#从新进入容器

root@f2d00398e2c4:/#

root@f2d00398e2c4:/# exit#exit退出

exit

ubuntu@ubuntu-virtual-machine:~$ docker ps#容器已经再也不运行

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

启动守护式容器2

docker run -d 镜像名 COMMAND[ARG…]#-d会告诉机器在启动容器时在后台的模式运行,只是之后台的形式运行,在命令结束后依旧会中止

ubuntu@ubuntu-virtual-machine:~$ docker run --name dc1 -d ubuntu /bin/bash -c "while true; do echo hello word;sleep 1;done"

00cf672ea018961f73e0ec16f9dbc98750894340190575e1913cf5263576423d

ubuntu@ubuntu-virtual-machine:~$ docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

00cf672ea018        ubuntu:latest       "/bin/bash -c 'while   12 seconds ago      Up 10 seconds 

查看容器内部运行的状况:能够用容器的logs日志命令来查看容器内部运行的状况

 查看容器日志:

docker los [-f] [-t] [--tail] 容器名

-f --follow=true|false 默认为false#告诉logs命令一直跟踪日志的变化并返回结果

-t --timestamps=true|false 默认为false#是在返回的结果上加上时间戳

--tail=all#选择结尾处多少数量的日志,不指定的话就会返回全部的日志

 

查看容器内的进程:

能够用:docker top 容器名查看运行中容器运行状况

ubuntu@ubuntu-virtual-machine:~$ docker top dc1

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                8342                7138                0                   15:02               ?                   00:00:14            /bin/bash -c while true; do echo hello word;sleep 1;done

 

在运行的容器中启动新的进程:

docker exec [-d] [-i] [-t] 容器名 COMMAND[ARG](跟run命令类似)

ubuntu@ubuntu-virtual-machine:~$ docker exec -i -t dc1 /bin/bash

root@00cf672ea018:/# ubuntu@ubuntu-virtual-machine:~$ #使用Ctrl+p Ctrl +q

ubuntu@ubuntu-virtual-machine:~$ docker top

docker: "top" requires a minimum of 1 argument. See 'docker top --help'.

ubuntu@ubuntu-virtual-machine:~$ docker top dc1

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                8342                7138                0                   15:02               ?                   00:00:18            /bin/bash -c while true; do echo hello word;sleep 1;done

root                12826               7138                0                   16:11               pts/1               00:00:00            /bin/bash

root                12889               8342                0                   16:12               ?                   00:00:00            sleep 1

 

中止守护式容器:

1docker stop 容器名#是发送一个信号给容器,等待容器中止

2docker kill 容器名#直接中止

ubuntu@ubuntu-virtual-machine:~$ docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

5d3123ba6245        ubuntu:latest       "/bin/bash -c 'while   6 seconds ago       Up 5 seconds                            dc2                

00cf672ea018        ubuntu:latest       "/bin/bash -c 'while   13 hours ago        Up 13 hours                             dc1                

ubuntu@ubuntu-virtual-machine:~$ docker stop dc2#中止的时候会等待,成功中止后会返回容器的名字

dc2

ubuntu@ubuntu-virtual-machine:~$ docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

00cf672ea018        ubuntu:latest       "/bin/bash -c 'while   13 hours ago        Up 13 hours                             dc1                

ubuntu@ubuntu-virtual-machine:~$ docker kill dc1

dc1