Docker常用命令汇总

  1. 搜索镜像
    $ docker search centos
    NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    centos                             The official build of CentOS.                   6385      [OK]       
    ansible/centos7-ansible            Ansible on Centos7                              132                  [OK]
    consol/centos-xfce-vnc             Centos container with "headless" VNC session…   125                  [OK]
    jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   117                  [OK]
    centos/systemd                     systemd enabled base container.                 93                   [OK]
    centos/mysql-57-centos7            MySQL 5.7 SQL database server                   87                   
    imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                   [OK]
    tutum/centos                       Simple CentOS docker image with SSH access      46                   
    kinogmt/centos-ssh                 CentOS with SSH                                 29                   [OK]
    pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   13                   
    guyton/centos6                     From official centos6 container with full up…   10                   [OK]
    nathonfowlie/centos-jre            Latest CentOS image with the JRE pre-install…   8                    [OK]
    centos/tools                       Docker image that has systems administration…   7                    [OK]
    drecom/centos-ruby                 centos ruby                                     6                    [OK]
    pivotaldata/centos                 Base centos, freshened up a little with a Do…   5                    
    pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   3                    
    darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
    pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   3                    
    mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                    [OK]
    indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                    [OK]
    blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
    mcnaughton/centos-base             centos base image                               1                    [OK]
    pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                    
    pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                    
    smartentry/centos                  centos with smartentry                          0                    [OK]
    
  2. 拉取镜像
    $ docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    a076a628af6f: Already exists 
    0732ab25fa22: Pull complete 
    d7f36f6fe38f: Pull complete 
    f72584a26f32: Pull complete 
    7125e4df9063: Pull complete 
    Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    
  3. 查看镜像
    $ docker images
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    mysql        5.7       a70d36bc331a   9 days ago    449MB
    nginx        latest    f6d0b4767a6c   2 weeks ago   133MB
    centos       latest    300e315adb2f   7 weeks ago   209MB
    
  4. 删除镜像
    $ docker rmi centos
    Untagged: centos:latest
    Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
    Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
    Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
    
  5. 创建容器
    $ docker create -it centos
    0d320f497f02db0577174b95926659bcd6ccb6d58ed7ae8d96439465012f4742
    $ docker ps -a
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                 NAMES
    0d320f497f02   centos      "bash"                   10 seconds ago   Created                               sleepy_babbage
    
  6. 查看容器
    # 查看当前正在运行的容器
    $ docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                 NAMES
    e3266aa82bfa   mysql:5.7   "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   3306/tcp, 33060/tcp   charming_euler
    
    # 查看所有容器
    $ docker ps -a
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                 NAMES
    e3266aa82bfa   mysql:5.7   "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   3306/tcp, 33060/tcp   charming_euler
    
  7. 启动容器
    # 通过id或名称来操作
    $ docker start 0d320f497f02
    0d320f497f02
    
  8. 进入容器(exec与attach的区别)

  • exec是执行命令的意思,就是在当前宿主机上执行容器中的命令,通过-it的参数可以输入与输出,执行bash命令就是其中的一种,这个命令会打开一个终端,此时宿主主机当前的终端被容器内的终端所覆盖,所以的操作都会被容器接管
  • attach:Attach local standard input, output, and error streams to a running container,意思是跟踪容器内的输入与输出,也就是说,如果多个终端同时attach到一个容器,大家的可以看到相同的界面,我能看到你输入的,你也能看到我输出的,同步。

    # 通过Docker执行脚本的方式打开容器内的终端
    $ docker exec -it 582eb515c4ee bash
    [root@582eb515c4ee /]# 
    
    # 通过attach命令进入容器的控制台
    $ docker attach 582eb515c4ee
    [root@582eb515c4ee /]# 
    
  1. 退出容器: Ctrl + P + Q 注意顺序不能乱

  2. 停止容器

    # 通过id或名称来操作
    $ docker stop sleepy_babbage 
    sleepy_babbage
    
  3. 删除容器
     # 删除容器通过id或名称来删除
     $ docker rm 0d320f497f02
     0d320f497f02
    
  4. 端口映射
     # 宿主主机的1080端口映射到容器中主机的80端口
     $ docker run -d -p 1080:80 nginx
     eace3d16b4f19e5d555bddc90c57a5b261196c62f1482513cfabc5b6ec7d4cce
    
     # 查看创建好的容器
     $ docker ps
     CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                  NAMES
     eace3d16b4f1   nginx       "/docker-entrypoint.…"   3 seconds ago    Up 1 second     0.0.0.0:1080->80/tcp   gallant_maxwell
    
  5. 卷宗映射
    # 将宿主机中/root/data 目录挂载到 容器中/data目录
    $ docker create -it -v /root/docker:/data centos bash
    a0c4b4f3bcc256193c49e86b88a1eb239051c955a5e6af031e821d7d3644d755
    
    #查看挂载详情
    $ docker inspect a0c4b4f3bcc2
    "Mounts": [
        {
            "Type": "bind",
            "Source": "/root/docker",
            "Destination": "/data",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        }
    ],
    

Leave a Reply