镜像使用

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载

镜像列表

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
qiang/ubuntu        latest              8804036498ef        2 weeks ago         74.2MB
qiang/ubuntu        test-dockerfile     8804036498ef        2 weeks ago         74.2MB
ubuntu              latest              74435f89ab78        4 weeks ago         73.9MB
training/webapp     latest              6fae60ef3446        5 years ago         349MB
  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签,同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

查找镜像

docker search [镜像]

获取镜像

docker pull [镜像][:TAG]

删除镜像

docker rmi [名称]

创建镜像

当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

  • 从已经创建的容器中更新镜像,并且提交这个镜像
  • 使用 Dockerfile 指令来创建一个新的镜像

更新镜像

  • 行运行并进入或进入一个运行的容器
    docker exec -it 0546f66f7e25 /bin/bash
  • 在运行的容器内使用 apt-get update 命令进行更新, 之后exit退出

      apt-get update
    
      Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]                               
      Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
      Get:3 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [33.9 kB]
      Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [165 kB]
      Get:5 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [45.2 kB]
      Get:6 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [1078 B]
      Get:7 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
      Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
      Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
      Get:10 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
      Get:11 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
      Get:12 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
      Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [310 kB]                                                                                                      
      Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [4202 B]                                                                                                
      Get:15 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [157 kB]                                                                                                  
      Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [33.9 kB]                                                                                               
      Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [3209 B]                                                                                                
      Fetched 14.2 MB in 7s (1966 kB/s)                                                                                                                                                       
      Reading package lists... Done
    
  • 此时 ID 为 0546f66f7e25 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本

      docker commit -m="test update" -a="zhaojq" 0546f66f7e25 zhaojq/ubuntu:v1
      sha256:7c9585e7c2def642516fe9a55a726ac3a325c60c0f4c40fc5e3f07ca3c921ef8
    
      # -m: 提交的描述信息
      # -a: 指定镜像作者
      # 0546f66f7e25: 容器ID
      # zhaojq/ubuntu:v1: 要创建的目标镜像名
    
  • 此时再次docker images 就可看到刚创建的镜像了

构建镜像

  • 创建Dockerfile文件

      FROM ubuntu:latest
      MAINTAINER zhaojq "test@qq.com"
    
      RUN /bin/echo 'root:123456'|chpasswd
      RUN useradd qiang
      RUN /bin/echo 'qiang: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 构建镜像

      # -t 指定创建的目标镜像名
      # . Dockerfile文件所在目录
      docker build -t zhaojq_dockerfile/ubuntu:v1 .
    
      Sending build context to Docker daemon  2.048kB
      Step 1/9 : FROM ubuntu:latest
      ---> 74435f89ab78
      Step 2/9 : MAINTAINER zhaojq "test@qq.com"
      ---> Running in 4622ba2d0438
      Removing intermediate container 4622ba2d0438
      ---> 4cc82d05c454
      Step 3/9 : RUN /bin/echo 'root:123456'|chpasswd
      ---> Running in 23f01fbf0bb4
      Removing intermediate container 23f01fbf0bb4
      ---> 9b6776103afa
      Step 4/9 : RUN useradd qiang
      ---> Running in ba293211f269
      Removing intermediate container ba293211f269
      ---> 8a62690dce30
      Step 5/9 : RUN /bin/echo 'qiang:123456'|chpasswd
      ---> Running in 73c84bc6b8b0
      Removing intermediate container 73c84bc6b8b0
      ---> dfc99a7193d8
      Step 6/9 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >etc/default/local
      ---> Running in 39ba22f4387b
      Removing intermediate container 39ba22f4387b
      ---> a49e9249f23a
      Step 7/9 : EXPOSE 22
      ---> Running in d6c13b91fc82
      Removing intermediate container d6c13b91fc82
      ---> 0232f7837c27
      Step 8/9 : EXPOSE 80
      ---> Running in 7669c7a72559
      Removing intermediate container 7669c7a72559
      ---> 278af938ab7d
      Step 9/9 : CMD /usr/sbin/sshd -D
      ---> Running in bad5d00e250b
      Removing intermediate container bad5d00e250b
      ---> e7ed32cec67e
      Successfully built e7ed32cec67e
      Successfully tagged zhaojq_dockerfile/ubuntu:v1
    
  • docker images查看刚才创建的镜像

设置镜像标签

`docker tag [镜像ID] [标签名称]

Copyright © zhaojq 2019 all right reserved,powered by Gitbook本书发布时间: 2021-01-19 20:53:28

results matching ""

    No results matching ""