2

Docker | 常用命令

 2 years ago
source link: https://ijayer.github.io/post/tech/devops/docker/20170409-common-cmd/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

获取镜像:docker pull

# Usage
$ docker pull [选项] [Docker Registry地址]<仓库名>:<标签>  
$ docker pull ubuntu:14.04

列出镜像:docker images

  • 列出顶层镜像

    $ docker images      
    # 说明:列表包含:仓库名(REPOSITORY)、标签(TAG)、镜像ID、创建时间、占用空间
  • 列出部分镜像

    # 根据仓库名显示   
    $ docker images ubuntu
    
    # 根据仓库名和标签显示    
    $ docker images ubuntu:14.04
    
    # 根据过滤参数显示: --filter简写-f    
    # 显示mongo:3.2之后的所有镜像
    $ docker images -f since=mongo:3.2
    
    # 显示mongo:3.2之前的所有镜像
    $ docker images -f before=mongo:3.2 
  • 虚悬镜像:仓库名和标签都为<none>, 可删除

    # 列出
    $ docker images -f dangling=true         
    # 删除               
    $ docker rmi $(docker images -q -f dangling=true) 
  • 中间层镜像:标签为<none>,其他镜像的依赖的镜像,不应独立删除

    $ docker images -a
  • 以特定格式显示(Go模板语法)

    # 列出镜像ID
    $ docker images -q      
    # --filter配合-q列出指定范围的Image ID列表,然后作为另一个docker命令的参数
    
    # 列出镜像ID和仓库名
    $ docker images --format "{{.ID}}: {{.Repository}}" 
    
    # 按自定义语法显示
    $ docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"

重命名镜像:docker tag

docker tag image_name:image_tag new_image_name:new_image_tag

移除镜像:docker rmi

  • 使用中的镜像不能移除

    # Usage
    $ docker rmi [OPTIONS] IMAGE [IMAGE...]
    # 强制移除ubuntu:14.04
    $ docker rmi -f ubuntu:14.04
    
    # docker rmi 输出信息
    # Untagged: 取消某个标签
    # Deleted: 删除镜像

保存镜像:docker commit

  • 在原有镜像基础上,叠加上容器的存储层并构成新的镜像,该镜像保存原有容器最后的文件修改

    $ docker commit [选项] <容器ID或容器名> [<仓库名>:<标签>]
  • 查看容器存储层的修改

    $ docker diff container
  • 查看镜像内的历史修改记录

    $ docker history [<仓库名>:<标签>]

运行容器:docker run

    $ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    $ docker run -it --rm ubuntu:14.04 bash
    $ docker run --name web2 -d -p 81:80 nginx:v2

    参数说明:
        -it:            -i, 交互式操作;-t, 终端
        --rm:           容器退出后将其删除
        ubuntu:14.04:   以ubuntu:14.04为基础启动容器
        bash:           命令,启动交互式shell

https://www.jianshu.com/p/ea4a00c6c21c

查看容器列表(运行中):docker ps

    # Usage
    $ docker ps [OPTIONS]
    # 查看所有容器
    $ docker ps -a

进入容器(运行中):docker exec

    # Usage
    $ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    # 示例
    $ docker exec -it hello /bin/bash
    # 容器未运行报错
    # Error response from daemon: Container 9e7644 is not running

    # 进入alphine容器
    $ docker exec -it apiv0.1 sh

移除容器:docker rm

  • 移除一个或多个容器

    # Usage
    $ docker rm [OPTIONS] CONTAINER [CONTAINER...]
    # 强制移除容器web
    $ docker rm -f web

清理日志 docker inspect + pipe 组合

    docker inspect [name] | grep LogPath | cut -d ':' -f 2 | cut -d ',' -f 1 | xargs echo | xargs truncate -s 0

简单练习——配置nginx服务器

  • 获取nginx镜像

    $ docker pull nginx
  • 在 /home/docker/nginx下创建index.html

    $ mkdir /home/docker/nginx -p && cd /home/docker/ngnix
    $ echo '<h1>Hello, Docker!</h1>' > index.html
  • 基于nginx镜像创建一个容器,并运行

    $ docker run --name web -d -v $(pwd):/usr/share/nginx/html -p 80:80 nginx
    # --name新容器名称web
    # -d    后台执行容器
    # -v    指定当前目录为数据卷, 提供nginx文件目录
    # -p    映射主机80端口到容器80端口
    # nginx 基础镜像

See Also

Thanks to the authors 🙂


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK