Docker之基础入门2

1.11Docker企业实战linux

系统环境nginx

[root@docker~]# cat /etc/redhat-release  #查看版本号

CentOS Linux release 7.1.1503 (Core)

[root@docker ~]# uname -r  #查看Linux内核

c3.10.0-229.el7.x86_64

 [root@docker ~]#yum install -y docker  #安装docker(CentOS7系统CentOS-Extras库中已带Docker)

[root@docker ~]#systemctl start docker  #启动docker

[root@docker ~]#systemctl enable docker  #加入开机自启动

[root@docker ~]# docker version  #查看docker版本信息

Client:  #docker客户端版本信息

 Version:1.8.2-el7.centos

 API version:1.20

 Package Version: docker-1.8.2-10.el7.centos.x86_64

 Go version:go1.4.2

 Git commit:a01dc02/1.8.2

 Built: OS/Arch:linux/amd64

 

Server:  #docker服务端版本信息

 Version:1.8.2-el7.centos

 API version:1.20

 Package Version:

 Go version:go1.4.2

 Git commit:a01dc02/1.8.2

 Built: OS/Arch:linux/amd64

1.12Docker基础命令git

[root@docker ~]# docker --help

Usage:
docker [OPTIONS] COMMAND [arg...]

       docker daemon [ --help | ... ]

       docker [ --help | -v | --version ]

 

A
self-sufficient runtime for containers.

 

Options:

  --config=~/.docker              Location of client config files  #客户端配置文件的位置

  -D, --debug=false               Enable debug mode  #启用Debug调试模式

  -H, --host=[]                   Daemon socket(s) to connect to  #守护进程的套接字(Socket)链接

  -h, --help=false                Print usage  #打印使用

  -l, --log-level=info            Set the logging level  #设置日志级别

  --tls=false                     Use TLS; implied by--tlsverify  #

  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA  #信任证书签名CA

  --tlscert=~/.docker/cert.pem    Path to TLS certificate file  #TLS证书文件路径

  --tlskey=~/.docker/key.pem      Path to TLS key file  #TLS密钥文件路径

  --tlsverify=false               Use TLS and verify the remote  #使用TLS验证远程

  -v, --version=false             Print version information and quit  #打印版本信息并退出

 Commands:

    attach    Attach to a running container  #当前shell下attach链接指定运行镜像

    build     Build an image from a Dockerfile  #经过Dockerfile定制镜像

    commit    Create a new image from a container's changes  #提交当前容器为新的镜像

    cp    Copy files/folders from a container to a HOSTDIR or to STDOUT  #从容器中拷贝指定文件或者目录到宿主机中

    create    Create a new container  #建立一个新的容器,同run 但不启动容器

    diff    Inspect changes on a container's filesystem  #查看docker容器变化

    events    Get real time events from the server#从docker服务获取容器实时事件

    exec    Run a command in a running container#在已存在的容器上运行命令

    export    Export a container's filesystem as a tar archive  #导出容器的内容流做为一个tar归档文件(对应import)

    history    Show the history of an image  #展现一个镜像造成历史

    images    List images  #列出系统当前镜像

    import    Import the contents from a tarball to create a filesystem image  #从tar包中的内容建立一个新的文件系统映像(对应export)

    info    Display system-wide information  #显示系统相关信息

    inspect    Return low-level information on a container or image  #查看容器详细信息

    kill    Kill a running container  #kill指定docker容器

    load    Load an image from a tar archive or STDIN  #从一个tar包中加载一个镜像(对应save)

    login    Register or log in to a Docker registry#注册或者登录一个docker源服务器

    logout    Log out from a Docker registry  #从当前Docker registry退出

    logs    Fetch the logs of a container  #输出当前容器日志信息

    pause    Pause all processes within a container#暂停容器

    port    List port mappings or a specific mapping for the CONTAINER  #查看映射端口对应的容器内部源端口

    ps    List containers  #列出容器列表

    pull    Pull an image or a repository from a registry  #从docker镜像源服务器拉取指定镜像或者库镜像

    push    Push an image or a repository to a registry  #推送指定镜像或者库镜像至docker源服务器

    rename    Rename a container  #重命名容器

    restart    Restart a running container  #重启运行的容器

    rm    Remove one or more containers  #移除一个或者多个容器

    rmi    Remove one or more images  #移除一个或多个镜像(无容器使用该镜像才能够删除,不然须要删除相关容器才能够继续或者-f强制删除)

    run    Run a command in a new container  #建立一个新的容器并运行一个命令

    save    Save an image(s) to a tar archive#保存一个镜像为一个tar包(对应load)

    search    Search the Docker Hub for images  #在docker
hub中搜索镜像

    start    Start one or more stopped containers#启动容器

    stats    Display a live stream of container(s) resource usage statistics  #统计容器使用资源

    stop    Stop a running container  #中止容器

    tag         Tag an image into a repository  #给源中镜像打标签

    top       Display the running processes of a container #查看容器中运行的进程信息

    unpause    Unpause all processes within a container  #取消暂停容器

    version    Show the Docker version information#查看容器版本号

    wait         Block until a container stops, then print its exit code  #截取容器中止时的退出状态值

 

Run 'docker COMMAND --help' for more information on a command.  #运行docker命令在帮助能够获取更多信息

1.13Docker镜像管理docker

1.13.1搜索Docker镜像shell

 

[root@docker ~]# docker search centos  #搜索全部centos的docker镜像

INDEX                NAME(名称)                  DESCRIPTION(描述)                    STARS(下载次数)OFFICIAL(官方)        AUTOMATED(自动化)

docker.io           docker.io/centos         The official build of CentOS.        1781               [OK]

docker.io   docker.io/jdeathe/centos-ssh     CentOS-6 6.7 x86_64 /             14                                               [OK]

1.13.2获取Docker镜像centos

可使用docker pull命令来从仓库获取所须要的镜像。下面的例子将从Docker Hub仓库下载一个Centos操做系统的镜像。bash

 

[root@docker ~]# docker pull centos  #获取centos镜像

[root@docker ~]# docker run -it centos /bin/bash  #完成后可使用该镜像建立一个容器

1.13.3查看docker镜像服务器

镜像的ID惟一标识了镜像,若是ID相同,说明是同一镜像。app

TAG信息来区分不一样发行版本,若是不指定具体标记,默认使用latest标记信息。ssh

[root@docker ~]# docker images  #查看docker镜像

REPOSITORY(来自那个仓库)       TAG(标签)            IMAGE ID(惟一ID)         CREATED(建立时间)         VIRTUAL SIZE(大小)

docker.io/centos                   latest              60e65a8e4030           5
days ago              196.6 MB

docker.io/nginx             latest              813e3731b203             13
days ago             133.8 MB

1.13.4删除Docker镜像

若是要移除本地的镜像,可使用docker rmi命令(在删除镜像以前先用docker rm删除依赖于这个镜像的全部容器)。注意docker rm 命令是移除容器。

 

[root@docker ~]# docker rmi imageID  #删除docker镜像

1.13.5导出Docker镜像

若是要导出镜像到本地文件,可使用docker save命令。

 

[root@docker ~]# docker save centos > /opt/centos.tar.gz  #导出docker镜像至本地

[root@docker ~]# ll /opt/

-rw-r--r--.1 root root 204205056 12月 30 09:53 centos.tar.gz

1.13.61.6.6导入Docker镜像

可使用docker load从本地文件中导入到本地docker镜像库

 

[root@docker ~]# docker load < /opt/centos.tar.gz   #导入本地镜像到docker镜像库

[root@docker~]# docker images  #查看镜像导入状况

REPOSITORY           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

docker.io/centos     latest              60e65a8e4030        5 days ago          196.6 MB

1.14Docker容器管理

1.14.1启动Docker容器

 

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

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

1.14.1.1新建容器并启动

所须要的命令主要为docker run

例如,下面的命令输出一个hehe,以后终止容器。

[root@docker ~]# docker run centos /bin/echo "hehe"  #这跟在本地直接执行 /bin/echo'hehe' 

hehe

[root@docker ~]# docker run --name mydocker -it centos /bin/bash#启动一个bash终端,容许用户进行交互。

[root@1c6c3f38ea07 /]# pwd

/

[root@1c6c3f38ea07 /]# ls

anaconda-post.log  bindev  etc  homelib  lib64  lost+foundmedia  mnt  optproc  root  runsbin  srv  systmp  usr  var

 --name:给容器定义一个名称

-i:则让容器的标准输入保持打开。

-t:让Docker分配一个伪终端,并绑定到容器的标准输入上

/bin/bash:执行一个命令

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

*检查本地是否存在指定的镜像,不存在就从公有仓库下载

*利用镜像建立并启动一个容器

*分配一个文件系统,并在只读的镜像层外面挂在一层可读写层

*从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去

*从地址池配置一个ip地址给容器

*执行用户指定的应用程序

*执行完毕后容器被终止

1.14.1.2启动已终止容器

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

容器的核心为所执行的应用程序,所须要的资源都是应用程序运行所必需的。除此以外,并无其余的资源。能够在伪终端中利用ps和top来查看进程信息。

[root@docker ~]# docker start 1c6c3f38ea07  #启动一个终止的容器

1c6c3f38ea07

[root@docker ~]# docker ps -a  #查看是否启动

CONTAINER
ID        IMAGE               COMMAND             CREATED             STATUS              PORTS       NAMES       

1c6c3f38ea07        centos              "/bin/bash"         8 minutes ago       Up 1 seconds                  mycentos   

1.14.1.3守护进程运行

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

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

[root@docker ~]# docker run -d centos /bin/bash -c "while true; do echo hehe; sleep 1;done"

961fd1162c2f6a8d04e4d8ab6ccacf4cb01a90af8ab553d5e2c5063ac483ffd8

[root@docker ~]# docker ps  #查看正在运行的docker容器

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

961fd1162c2f        centos              "/bin/bash -c 'while
"   18 minutes ago      Up 18 minutes                           agitated_raman

[root@docker ~]# docker logs 961fd1162c2f  #获取容器输出信息,经过dockerlogs命令

hehe

1.14.2中止容器

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

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

终止状态的容器能够用docker ps -a命令看到,也能够经过docker start ID 命令来启动容器。

[root@docker ~]# docker ps -a #查看全部容器的状况

CONTAINER
ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS         NAMES

961fd1162c2f        centos              "/bin/bash -c 'while
"   30 minutes ago      Up 30 minutes                           agitated_raman

[root@docker ~]# docker stop 961fd1162c2f  #中止容器

961fd1162c2f

[root@docker ~]# docker ps -a

CONTAINER
ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES

961fd1162c2f        centos              "/bin/bash -c 'while"   31 minutes ago      Exited (137) 27 seconds ago                agitated_raman

1.14.3删除容器

[root@867e6627a194 ~]# docker ps -a  #查看全部容器当前状态

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES

867e6627a194        centos              "/bin/bash"         16 minutes ago      Up 5 minutes                                    mydocker

c599b569f387        centos              "/bin/echo hehe"   19 minutes ago      Exited (0) 19 minutes ago                     fervent_torvalds

302f39c202c9        nginx               "/bin/bash"         2 hours ago          Up 2 hours               80/tcp, 443/tcp      mynginx

[root@867e6627a194 ~]#

[root@867e6627a194 ~]# docker rm c599b569f387  #删除已经中止的容器

c599b569f387

[root@867e6627a194 ~]# docker rm -f 302f39c202c9   #删除正在运行的容器

302f39c202c9

1.14.4进入容器

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

1.14.4.1attach命令

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

[root@docker ~]# docker ps -a

CONTAINER
ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES

867e6627a194        centos              "/bin/bash"         10 minutes ago      Exited (127) 47 seconds ago                       mydocker

[root@docker ~]# docker start 867e6627a194  #启动已经中止的容器

867e6627a194

[root@docker ~]# docker attach 867e6627a194  #经过docker attach进入

[root@867e6627a194 /]#

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

1.14.4.2nsenter命令

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

 

[root@docker ~]# yum install -y util-linux   #安装包中有须要用到的nsenter

[root@docker ~]# docker ps -a

CONTAINER
ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES

867e6627a194        centos              "/bin/bash"         10 minutes ago      Exited (127) 47 seconds ago                       mydocker

[root@docker ~]# docker start 867e6627a194  #启动已经关闭的容器

867e6627a194

[root@docker ~]# docker inspect --format "{{.State.Pid}}" 867e6627a194  #找到容器的第一个进程PID

20012

[root@docker ~]# nsenter -t 20012 -u -i -n -p  #经过这个PID链接到容器

[root@867e6627a194 ~]# exit

[root@docker ~]# docker ps

CONTAINER
ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

867e6627a194        centos              "/bin/bash"         13 minutes ago      Up 2 minutes                            mydocker

[root@docker ~]# cat in.sh   #编写成脚本快速进入容器空间

#!/bin/sh

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

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

[root@docker ~]# docker ps

CONTAINER
ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

867e6627a194        centos              "/bin/bash"         15 minutes ago      Up 4 minutes                            mydocker

[root@docker ~]# ./in.sh 867e6627a194  #执行脚本跟上容器ID快速进入
相关文章
相关标签/搜索