• 06、对容器操作


    1、启动容器

    1)方法1(不推荐):

      先创建一个容器:docker create 镜像名

      再启动容器:docker start  容器名

    2)方法2:docker run 镜像名

    格式:

    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

    options常用命令

    -t  打开一个终端,像使用交换机一样使用容器

    -i  交互式访问

    --name  指定容器名字

    --network  指定网络模式

    --rm    容器一停,自动删除

    -d  剥离与当前终端的关系;否则会一直占据着终端

    -p  端口映射,将容器内服务的段鸥映射在宿主机的指定端口

    • -p <container port>
    • -p <hostport>:<container port>
    • -p <hostip>:<hostport>:<container port>

    运行一个容器

    [root@hackerlin ~]# docker run --name web2 -it docker.io/nginx:1.14-alpine /bin/sh
    / # ls
    bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
    / # exit

    2、查看容器运行的状态

    docker ps

    -a  查询所有的容器

    注:容器内的第一个进程必须一直处于运行的状态,否则这个容器就会处于退出状态!

    [root@hackerlin ~]# docker ps  #只显示正在运行的容器
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                  NAMES
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   5 minutes ago       Up 5 minutes        0.0.0.0:8888->80/tcp   web1
    [root@hackerlin ~]# docker ps -a  #显示所有的容器
    CONTAINER ID        IMAGE                         COMMAND                  CREATED              STATUS                          PORTS                  NAMES
    90b865a6b118        docker.io/nginx:1.14-alpine   "/bin/sh"                About a minute ago   Exited (0) About a minute ago                          web2
    f43b710c4f7a        docker.io/busybox:latest      "/bin/bash"              5 minutes ago        Created                                                b1
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   5 minutes ago        Up 5 minutes                    0.0.0.0:8888->80/tcp   web1

    3、停止运行的容器

    docker stop  关闭运行的容器

    docker kill  杀死运行的容器

    -s:指定信号,和kill用法一样;-9强制停止容器

    [root@hackerlin ~]# docker ps
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                  NAMES
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   9 minutes ago       Up 2 seconds        0.0.0.0:8888->80/tcp   web1
    [root@hackerlin ~]# docker kill web1
    web1
    [root@hackerlin ~]# docker ps    #显示正在运行的容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    [root@hackerlin ~]# docker ps -a    #显示所有的容器
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                       PORTS               NAMES
    90b865a6b118        docker.io/nginx:1.14-alpine   "/bin/sh"                4 minutes ago       Exited (0) 4 minutes ago                         web2
    f43b710c4f7a        docker.io/busybox:latest      "/bin/bash"              8 minutes ago       Created                                          b1
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   9 minutes ago       Exited (137) 6 seconds ago                       web1

    4、激活关闭的容器

    docker start 

    格式:

    Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

     

    options选项:

    -a:  附加到当前终端

    -i:   交互式

    示例:

    [root@hackerlin ~]# docker start web1
    web1
    [root@hackerlin ~]# docker ps
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                  NAMES
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   13 minutes ago      Up 1 second         0.0.0.0:8888->80/tcp   web1

    5、查看容器信息

    [root@hackerlin ~]# docker inspect web1
    [root@hackerlin ~]# docker inspect web1 | grep "IPAddress"    #查询到容器的IP地址
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    [root@hackerlin ~]# curl 172.17.0.2  #通过地址访问容器提供的服务
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>

    6、删除容器(先停止后删除)

    [root@hackerlin ~]# docker ps
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                  NAMES
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   16 minutes ago      Up 2 minutes        0.0.0.0:8888->80/tcp   web1
    [root@hackerlin ~]# docker kill web1     #先关闭容器
    web1
    [root@hackerlin ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    [root@hackerlin ~]# docker ps -a
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                       PORTS               NAMES
    90b865a6b118        docker.io/nginx:1.14-alpine   "/bin/sh"                12 minutes ago      Exited (0) 11 minutes ago                        web2
    f43b710c4f7a        docker.io/busybox:latest      "/bin/bash"              16 minutes ago      Created                                          b1
    147c77201ec1        docker.io/nginx:1.14-alpine   "nginx -g 'daemon ..."   16 minutes ago      Exited (137) 9 seconds ago                       web1
    [root@hackerlin ~]# docker rm web1    #再删除容器
    web1
    [root@hackerlin ~]# docker ps -a
    CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS                      PORTS               NAMES
    90b865a6b118        docker.io/nginx:1.14-alpine   "/bin/sh"           12 minutes ago      Exited (0) 12 minutes ago                       web2
    f43b710c4f7a        docker.io/busybox:latest      "/bin/bash"         16 minutes ago      Created                                         b1
    [root@hackerlin ~]# docker rm -f `docker ps -aq`    #删除所有容器,-f 强制删除
    90b865a6b118
    f43b710c4f7a
    [root@hackerlin ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

    7、对运行的容器执行指定命令exec

    格式:

    Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

    options选项

    • -d:在后台运行命令
    • -e:设置环境变量
    • -i:交互式
    • -t:打开一个终端
    • -u:用户名或UID

    示例:

    [root@hackerlin ~]# docker run --name web1 -d nginx:1.14-alpine
    81f336e878c0fb3187596f2acd12705d94f532978a8ad37c9f8ae33cc39bfb61
    交互式进入容器
    [root@hackerlin ~]# docker exec -it web1 /bin/sh   
    / # ls /
    bin    etc    lib    mnt    root   sbin   sys    usr
    dev    home   media  proc   run    srv    tmp    var
    / # exit
    查询ip
    [root@hackerlin ~]# docker exec web1 ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    43: eth0@if44: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.2/16 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:acff:fe11:2/64 scope link 
           valid_lft forever preferred_lft forever

    8、查询容器内部日志

    [root@hackerlin ~]# curl 172.17.0.2
    [root@hackerlin ~]# docker logs web1
    172.17.0.1 - - [03/Jan/2019:09:00:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

      

  • 相关阅读:
    [引]Windows窗体编程基础学习: 对话框组件
    UML学习四:UML在微软的VS与Visio间的应用 VEA(Visio for Enterprise Architects)
    SQL查询 FOR XML [RAW|AUTO|EXPLICIT]
    微软企业类库:Enterprise Library for .NET Framework 2.0 January 2006
    自定义制作 自动定时更换图片的桌面背景 html页面
    [引]Windows Server 2003 : Windows 群集
    LOVE
    SQL Server Mobile 学习(三):SQL Server Mobile 远程数据访问(RDA)
    游戏分类
    [摘]UML学习二:标准建模语言UML的静态建模机制
  • 原文地址:https://www.cnblogs.com/hackerlin/p/12673508.html
Copyright © 2020-2023  润新知