Docker镜像常用命令

Docker镜像常用命令

查看镜像列表

  • docker images

  • $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    ubuntu              16.04               b6f507652425        3 months ago        135MB
    simagix/maobi       latest              356c9f7cb831        7 months ago        26.3MB
    training/webapp     latest              6fae60ef3446        6 years ago         349MB
    
  • 运行指定标签(16.04)的(ubuntu)镜像:

    docker run -it ubuntu:16.04 /bin/bash
    

拉取镜像

  • docker pull imajes_name:version

  • #拉取13.10版本的ubuntu镜像
    $ docker pull ubuntu:13.10
    

查找镜像

  • docker search

  • $ docker search ubuntu
    >>>
    NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   13350               [OK]                
    dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   595                                     [OK]
    websphere-liberty                                         WebSphere Liberty multi-architecture images …   282                 [OK]                
    
    
  • official表示该镜像是否由官方发布,automated表示自动构建

删除镜像

  • docker rmi imange_name

  • $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    ubuntu              16.04               b6f507652425        3 months ago        135MB
    simagix/maobi       latest              356c9f7cb831        7 months ago        26.3MB
    training/webapp     latest              6fae60ef3446        6 years ago         349MB
    
    $docker rmi ubuntu:16.04
    >>>
    Untagged: ubuntu:16.04
    Untagged: ubuntu@sha256:0f71fa8d4d2d4292c3c617fda2b36f6dabe5c8b6e34c3dc5b0d17d4e704bd39c
    Deleted: sha256:b6f50765242581c887ff1acc2511fa2d885c52d8fb3ac8c4bba131fd86567f2e
    Deleted: sha256:0214f4b057d78b44fd12702828152f67c0ce115f9346acc63acdf997cab7e7c8
    Deleted: sha256:1b9d0485372c5562fa614d5b35766f6c442539bcee9825a6e90d1158c3299a61
    Deleted: sha256:3c0f34be6eb98057c607b9080237cce0be0b86f52d51ba620dc018a3d421baea
    Deleted: sha256:be96a3f634de79f523f07c7e4e0216c28af45eb5776e7a6238a2392f71e01069
    
    $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    simagix/maobi       latest              356c9f7cb831        7 months ago        26.3MB
    training/webapp     latest              6fae60ef3446        6 years ago         349MB
    

设置镜像标签

  • docker tag --help
    
    Usage:	docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
    
    Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
    
  • 例如给已存在的ubuntu镜像新建一个 dev 标签:

    $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    $ docker tag ba6acccedd29 ubuntu:dev
    $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              dev                 ba6acccedd29        2 months ago        72.8MB
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    
    

创建镜像

  • 有两种方式来更新镜像:
    • 从已创建的容器中更新镜像,并提交这个镜像至仓库
    • 使用Dockerfile文件构建镜像

更新镜像

  • docker commit a="author name" m="更新镜像" CONTAINER_ID REPOSITORY:TAG

    $ docker commit --help
    >>>
    Usage:	docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
    
    Create a new image from a container's changes
    
    Options:
      -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
      -c, --change list      Apply Dockerfile instruction to the created image
      -m, --message string   Commit message
      -p, --pause            Pause container during commit (default true)
    
  • 以ubuntu镜像为例,首先使用ubuntu镜像开启容器,进行更新并退出容器:

    $ docker images
    >>>
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    
    $ docker run -it ubuntu /bin/bash
    root@a6332b541b40:$ apt-get update
    root@a6332b541b40:$ exit
    
  • 此时ID为 a6332b541b40 的容器就是实现了更新需求的容器,将此容器提交到仓库创建镜像:

    $ docker commit -a="author" -m="for test" a6332b541b40 test/ubuntu:v1
    >>>
    sha256:85661e43eed11155616638deac5cd9e3818f1b587e0cb6880f5f0603ce057712
    

    此时查看镜像,发现在 test/ubuntu仓库已经有一个tag=v1的镜像:

    $ docker images
    >>>
    docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    test/ubuntu         v1                  85661e43eed1        2 minutes ago       105MB
    ubuntu              latest              ba6acccedd29        2 months ago        72.8MB
    

    使用新镜像来创建一个容器:

    $ docker run -it test/ubuntu:v1 /bin/bash
    root@e7605d76cf3b:$
    

构建镜像

  • docker build

    $ docker build --help
    >>>
    Usage:	docker build [OPTIONS] PATH | URL | -
    
    Build an image from a Dockerfile
    
    Options:
          --add-host list           Add a custom host-to-IP mapping (host:ip)
          --build-arg list          Set build-time variables
          --cache-from strings      Images to consider as cache sources
          --cgroup-parent string    Optional parent cgroup for the container
          --compress                Compress the build context using gzip
          --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
          --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
      -c, --cpu-shares int          CPU shares (relative weight)
          --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
          --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
          --disable-content-trust   Skip image verification (default true)
      -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
          --force-rm                Always remove intermediate containers
          --iidfile string          Write the image ID to the file
          --isolation string        Container isolation technology
          --label list              Set metadata for an image
      -m, --memory bytes            Memory limit
          --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
          --network string          Set the networking mode for the RUN instructions during build (default "default")
          --no-cache                Do not use cache when building the image
          --pull                    Always attempt to pull a newer version of the image
      -q, --quiet                   Suppress the build output and print image ID on success
          --rm                      Remove intermediate containers after a successful build (default true)
          --security-opt strings    Security options
          --shm-size bytes          Size of /dev/shm
      -t, --tag list                Name and optionally a tag in the 'name:tag' format
          --target string           Set the target build stage to build.
          --ulimit ulimit           Ulimit options (default [])
    
  • Dockerfile内容:

    FROM    centos:6.7
    MAINTAINER      Fisher "fisher@sudops.com"
    
    RUN     /bin/echo 'root:123456' |chpasswd
    RUN     useradd runoob
    RUN     /bin/echo 'runoob:123456' |chpasswd
    RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
    EXPOSE  22
    EXPOSE  80
    CMD     /usr/sbin/sshd -D
    
  • 构建镜像,docker build 的Path参数是Dockerfile文件所处的文件夹,不是文件地址,否则提示报错:

    unable to prepare context: context must be a directory: /root/Dockerfile
    
    # -t 参数执行构建镜像所处的仓库以及tag
    $ docker build -t test/centos:v1 /root
    >>>
    Sending build context to Docker daemon ...
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值