• Docker container 集装箱说明


    容器操作

    使用 docker 命令行操作 docker 容器

    启动容器

    core@localhost ~ $ docker run
    
    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    
    Run a command in a new container
    
      -a, --attach=[]            Attach to STDIN, STDOUT or STDERR.
      -c, --cpu-shares=0         CPU shares (relative weight)
      --cap-add=[]               Add Linux capabilities
      --cap-drop=[]              Drop Linux capabilities
      --cidfile=""               Write the container ID to the file
      --cpuset=""                CPUs in which to allow execution (0-3, 0,1)
      -d, --detach=false         Detached mode: run container in the background and print new container ID
      --device=[]                Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc)
      --dns=[]                   Set custom DNS servers
      --dns-search=[]            Set custom DNS search domains
      -e, --env=[]               Set environment variables
      --entrypoint=""            Overwrite the default ENTRYPOINT of the image
      --env-file=[]              Read in a line delimited file of environment variables
      --expose=[]                Expose a port from the container without publishing it to your host
      -h, --hostname=""          Container host name
      -i, --interactive=false    Keep STDIN open even if not attached
      --link=[]                  Add link to another container in the form of name:alias
      --lxc-conf=[]              (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
      -m, --memory=""            Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
      --name=""                  Assign a name to the container
      --net="bridge"             Set the Network mode for the container
                                   'bridge': creates a new network stack for the container on the docker bridge
                                   'none': no networking for this container
                                   'container:<name|id>': reuses another container network stack
                                   'host': use the host network stack inside the container.  Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
      -P, --publish-all=false    Publish all exposed ports to the host interfaces
      -p, --publish=[]           Publish a container's port to the host
                                   format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
                                   (use 'docker port' to see the actual mapping)
      --privileged=false         Give extended privileges to this container
      --restart=""               Restart policy to apply when a container exits (no, on-failure, always)
      --rm=false                 Automatically remove the container when it exits (incompatible with -d)
      --sig-proxy=true           Proxy received signals to the process (even in non-TTY mode). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.
      -t, --tty=false            Allocate a pseudo-TTY
      -u, --user=""              Username or UID
      -v, --volume=[]            Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)
      --volumes-from=[]          Mount volumes from the specified container(s)
      -w, --workdir=""           Working directory inside the container
    

    容器启动能够使用上述參数,以下是一些常见的參数的使用演示样例:

    core@localhost ~ $ docker run -ti ubuntu:14.04  /bin/bash
    root@fd2db7b20f6b:/#
    # -t 表示返回一个 tty 终端。-i 表示打开容器的标准输入。使用这个命令能够得到一个容器的 shell 终端。假设不使用 dockerfile 的话,能够使用这样的方式来对容器做一些更改,然后使用「docker commit」提交到新的 image
    #让我们打开另外一个终端,查看下当前执行的容器
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                                 COMMAND                CREATED             STATUS              PORTS                                          NAMES
    fd2db7b20f6b        dl.dockerpool.com:5000/ubuntu:14.04   "/bin/bash"            23 seconds ago      Up 22 seconds                                                      thirsty_hawking
    9cb2e45814e0        4b32789c7d66                          "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                      "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7      "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    #能够看到 fd2db7b20f6b 正处于执行中
    root@fd2db7b20f6b:/# exit
    exit
    #当我们 「exit」 退出的时候,再查看下「docker ps」
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    #我们发现容器已经退出了,所以这样的启动的方法不是我们使用容器来执行服务的好办法
    
    core@localhost ~ $ docker images
    REPOSITORY                                              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    base/163                                                latest              468d347c06bc        46 hours ago        249.1 MB
    test/supervisord                                        latest              468d347c06bc        46 hours ago        249.1 MB
    ubuntu                                                  14.04               1357f421be38        5 days ago          192.7 MB
    dl.dockerpool.com:5000/ubuntu                           14.04               1357f421be38        5 days ago          192.7 MB
    mysql                                                   5.7                 e95cbb9f48ea        7 days ago          258.6 MB
    dl.dockerpool.com:5000/mysql                            5.7                 e95cbb9f48ea        7 days ago          258.6 MB
    <none>                                                  <none>              4b32789c7d66        6 weeks ago         469.8 MB
    tutum/tomcat                                            8.0                 866eb07a675e        6 weeks ago         539.4 MB
    tutum/tomcat                                            latest              02e84f04100e        6 weeks ago         539.4 MB
    dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g   latest              ba16d5d5e1aa        7 months ago        2.388 GB
    core@localhost ~ $ docker run -p 100:22 -d  base/163
    6d9542bdc544e20b2f9718633a479e09047abef675ea24377d4daf4f04f20237
    #这里我们使用了前面创建的 base/163 的容器。并使用了 -p 和 -d 2个參数。他们分别表示映射宿主主机端口和后台执行容器的意思
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    6d9542bdc544        base/163:latest                    "/usr/bin/supervisor   5 minutes ago       Up 5 minutes        0.0.0.0:100->22/tcp                            desperate_franklin
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp
    #从输出的 port 列。我们能够看到 0.0.0.0:100->22/tcp ,它表示该容器映射了宿主主机的 100 端口到容器的 22 端口,即 ssh 服务端口
    #这样我们就创建了一个具有 ssh 服务的容器,假设须要创建其它类型服务的容器。步骤跟创建 ssh 服务差点儿相同,我们会在后面的章节具体介绍
    core@localhost ~ $ ssh root@127.0.0.1 -p 100
    Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-58-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com/
    
    The programs included with the Ubuntu system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
    applicable law.
    
    root@6d9542bdc544:~#exit
    logout
    Connection to 127.0.0.1 closed.
    core@localhost ~ $ docker  ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    6d9542bdc544        base/163:latest                    "/usr/bin/supervisor   11 minutes ago      Up 11 minutes       0.0.0.0:100->22/tcp                            desperate_franklin
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    #我们从 ssh 终端退出后,容器也不会停止
    

    注意:假设读者没有跟着我们的教程来创建 base/163 的镜像,这里是看不到的这个 images 的,点击这里查看教程 另外。另一些如 -v --link 等与容器数据存储和网络相关的參数将相关章节再具体介绍

    停止容器

    core@localhost ~ $ docker stop
    
    Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
    
    Stop a running container by sending SIGTERM and then SIGKILL after a grace period
    
      -t, --time=10      Number of seconds to wait for the container to stop before killing it.
    Default is 10 seconds.
    #还能够设定在强制杀死容器进程之前等待多少秒来等待容器停止,一般使用默认值就可以
    core@localhost ~ $ docker stop 6d9
    #也能够使用容器的名称来停止,停止多个容器在每一个容器之间加空格就可以
    6d9
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    

    删除容器

    core@localhost ~ $ docker rm
    
    Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
    
    Remove one or more containers
    
      -f, --force=false      Force the removal of a running container (uses SIGKILL)
      #使用 SIGKILL 强制删除一个正在执行的容器
      -l, --link=false       Remove the specified link and not the underlying container
      #删除容器的连接。而非容器
      -v, --volumes=false    Remove the volumes associated with the container
      #删除容器相关的数据卷
    core@localhost ~ $ docker start 6d9
    6d9
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    6d9542bdc544        base/163:latest                    "/usr/bin/supervisor   25 minutes ago      Up 2 seconds        0.0.0.0:100->22/tcp                            desperate_franklin
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    #当我们使用「docker stop」停止一个容器之后,容器并没有被删除。我们还能够使用「docker start」来启动它
    core@localhost ~ $ docker start 6d9
    6d9
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    6d9542bdc544        base/163:latest                    "/usr/bin/supervisor   25 minutes ago      Up 2 seconds        0.0.0.0:100->22/tcp                            desperate_franklin
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              21 hours ago        Up 21 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   23 hours ago        Up 23 hours         3306/tcp                                       db001,tomcat001/tomysql
    core@localhost ~ $ docker rm -f 6d9
    #使用 -f 强制删除容器
    6d9
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              22 hours ago        Up 22 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              24 hours ago        Up 24 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   24 hours ago        Up 24 hours         3306/tcp                                       db001,tomcat001/tomysql
    core@localhost ~ $ docker rm -l   tomcat001/tomysql
    tomcat001/tomysql
    #使用 -l 删除了之前建立的到 mysql 的连接
    core@localhost ~ $ docker ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              22 hours ago        Up 22 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              24 hours ago        Up 24 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   24 hours ago        Up 24 hours         3306/tcp                                       db001
    core@localhost ~ $
    

    导出容器

    core@localhost ~ $ docker export
    
    Usage: docker export CONTAINER
    
    Export the contents of a filesystem as a tar archive to STDOUT
    core@localhost ~ $ docker run -d base/163
    5883b400a03ed81b76a22badeb8948642023dd4b64c083cc74e787a2e3d7f7a4
    core@localhost ~ $ docker  ps
    CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
    5883b400a03e        base/163:latest                    "/usr/bin/supervisor   3 seconds ago       Up 2 seconds        22/tcp                                         agitated_heisenberg
    9cb2e45814e0        4b32789c7d66                       "/run.sh"              23 hours ago        Up 23 hours         0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
    e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              24 hours ago        Up 24 hours         0.0.0.0:80->8080/tcp                           tomcat001
    fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   24 hours ago        Up 24 hours         3306/tcp                                       db001
    core@localhost ~ $ docker export 588 >>base/163.tar
    core@localhost ~/base $ ls -lh
    total 227M
    -rw-r--r-- 1 core core 227M Oct 16 03:00 163.tar
    #导出了一个227M的文件,我们又能够将他导入成为 images
    

    容器原理

    很多其它内容请关注 http://www.dockerpool.com

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    【Jenkins】jenkins 配置腾讯企业邮箱
    Monkey 用户指南(译)
    Windows,easygui 安装
    python笔记:list--pop与remove的区别
    递归--Java_汉诺塔
    appium安装 For windows
    【web开发--js学习】functionName 如果是一个属性值,函数将不会被调用
    python爬取煎蛋网图片
    pv&pvc
    docker
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4837158.html
Copyright © 2020-2023  润新知