版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

镜像的获取

pull from registry (online)
  1. 从registry拉取
    • public(公有)
    • private(私有)
  • build from Dockerfile (online) 从Dockerfile构建
    1. 从Dockerfile构建(在线)
    2. 文件导入(离线)
    load from file (offline) 文件导入 (离线)

    Docker Image Registry

    类似于Ubuntu的apt-get源,Go的proxy,git的GitHub,用于指定从哪里摘取镜像,默认是Docker Hub,也就是https://hub.docker.com/

    配置registry可参考:镜像加速器 - Docker — 从入门到实践

    镜像的基本操作

    代码块
    $ docker image
    
    Usage:  docker image COMMAND
    
    Manage images
    
    Commands:
    build       Build an image from a Dockerfile
    history     Show the history of an image
    import      Import the contents from a tarball to create a filesystem image
    inspect     Display detailed information on one or more images
    load        Load an image from a tar archive or STDIN
    ls          List images
    prune       Remove unused images
    pull        Pull an image or a repository from a registry
    push        Push an image or a repository to a registry
    rm          Remove one or more images
    save        Save one or more images to a tar archive (streamed to STDOUT by default)
    tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
    
    Run 'docker image COMMAND --help' for more information on a command.
    镜像的拉取Pull Image

    镜像的拉取

    默认从Docker Hub拉取,如果不指定版本,会拉取最新版。

    代码块
    $ docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    69692152171a: Pull complete
    49f7d34d62c1: Pull complete
    5f97dc5d71ab: Pull complete
    cfcd0711b93a: Pull complete
    be6172d7651b: Pull complete
    de9813870342: Pull complete
    Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    指定版本

    指定版本:

    代码块
    $ docker pull nginx:1.20.0
    1.20.0: Pulling from library/nginx
    69692152171a: Already exists
    965615a5cec8: Pull complete
    b141b026b9ce: Pull complete
    8d70dc384fb3: Pull complete
    525e372d6dee: Pull complete
    Digest: sha256:ea4560b87ff03479670d15df426f7d02e30cb6340dcd3004cdfc048d6a1d54b4
    Status: Downloaded newer image for nginx:1.20.0
    docker.io/library/nginx:1.20.0
    从Quay上拉取镜像

    从Quay上拉取镜像:

    代码块
    $ docker pull quay.io/bitnami/nginx
    Using default tag: latest
    latest: Pulling from bitnami/nginx
    2e6370f1e2d3: Pull complete
    2d464c695e97: Pull complete
    83eb3b1671f4: Pull complete
    364c139450f9: Pull complete
    dc453d5ae92e: Pull complete
    09bd59934b83: Pull complete
    8d2bd62eedfb: Pull complete
    8ac060ae1ede: Pull complete
    c7c9bc2f4f9d: Pull complete
    6dd7098b85fa: Pull complete
    894a70299d18: Pull complete
    Digest: sha256:d143befa04e503472603190da62db157383797d281fb04e6a72c85b48e0b3239
    Status: Downloaded newer image for quay.io/bitnami/nginx:latest
    quay.io/bitnami/nginx:latest

    镜像的查看

    代码块
    $ docker image ls
    REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
    quay.io/bitnami/nginx   latest    0922eabe1625   6 hours ago   89.3MB
    nginx                   1.20.0    7ab27dbbfbdf   10 days ago   133MB
    nginx                   latest    f0b8a9a54136   10 days ago   133MB
    提示

    docker image inspect <image ID>可以查看一个镜像的详细信息。

    镜像的删除

    代码块
    $ docker image rm 0922eabe1625
    Untagged: quay.io/bitnami/nginx:latest
    Untagged: quay.io/bitnami/nginx@sha256:d143befa04e503472603190da62db157383797d281fb04e6a72c85b48e0b3239
    Deleted: sha256:0922eabe16250e2f4711146e31b7aac0e547f52daa6cf01c9d00cf64d49c68c8
    Deleted: sha256:5eee4ed0f6b242e2c6e4f4066c7aca26bf9b3b021b511b56a0dadd52610606bd
    Deleted: sha256:472a75325eda417558f9100ff8b4a97f4a5e8586a14eb9c8fc12f944b26a21f8
    Deleted: sha256:cdcb5872f8a64a0b5839711fcd2a87ba05795e5bf6a70ba9510b8066cdd25e76
    Deleted: sha256:e0f1b7345a521469bbeb7ec53ef98227bd38c87efa19855c5ba0db0ac25c8e83
    Deleted: sha256:11b9c2261cfc687fba8d300b83434854cc01e91a2f8b1c21dadd937e59290c99
    Deleted: sha256:4819311ec2867ad82d017253500be1148fc335ad13b6c1eb6875154da582fcf2
    Deleted: sha256:784480add553b8e8d5ee1bbd229ed8be92099e5fb61009ed7398b93d5705a560
    Deleted: sha256:e0c520d1a43832d5d2b1028e3f57047f9d9f71078c0187f4bb05e6a6a572993d
    Deleted: sha256:94d5b1d6c9e31de42ce58b8ce51eb6fb5292ec889a6d95763ad2905330b92762
    Deleted: sha256:95deba55c490bbb8de44551d3e6a89704758c93ba8503a593cb7c07dfbae0058
    Deleted: sha256:1ad1d903ef1def850cd44e2010b46542196e5f91e53317dbdb2c1eedfc2d770c

    镜像的导出和导入 (offline)

    提示

    如果通过镜像创建了容器,则必须先删除容器,然后才能删除镜像。

    镜像的导入和导出

    提示

    参考docker image --help,要用到的命令是save和load。

    代码块
    docker image 
    代码块
    PS C:\Users\Peng Xiao\docker.tips\image> docker image ls nginx 1.20.0 7ab27dbbfbdf 12 days ago 133MB nginx latest f0b8a9a54136 12 days ago 133MB PS C:\Users\Peng Xiao\docker.tips\image> docker image
    save nginx:1.20.0 -o
    nginx.image PS C:\Users\Peng Xiao\docker.tips\image> ls Directory: C:\Users\Peng Xiao\docker.tips\image Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 5/24/2021 1:40 PM 137379328 nginx.image PS C:\Users\Peng Xiao\docker.tips\image> docker image rm 7ab Untagged:
     nginx
    :1
    .
    20.0 Deleted: sha256:7ab27dbbfbdf4031f0603a4b597cc43031ff883b54f9329f0309c80952dda6f5 Deleted: sha256:5b2a9404d052ae4205f6139190fd4b0921ddeff17bf2aaf4ee97f79e1a8242fe Deleted: sha256:03ebf76f0cbf5fd32ca010bb589c2139ce7e44c050fe3de2d77addf4cfd25866 Deleted: sha256:0191669d087dce47072254a93fe55cbedd687f27d3798e2260f846e8f8f5729a Deleted: sha256:17651c6a0ba04d31da14ac6a86d8fb3f600883f9e155558e8aad0b94aa6540a2 Deleted: sha256:5a673ff4c07a1b606f2ad1fc53697c99c45b0675734ca945e3bb2bd80f43feb8 PS C:\Users\Peng Xiao\docker.tips\image> docker
    image
    ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f0b8a9a54136 12 days ago 133MB
    
    
    PS C:\Users\Peng Xiao\
    docker
    .tips\image>
     
    docker
    image load -i 
    .\nginx.image 1839f9962bd8: Loading layer [==================================================>] 64.8MB/64.8MB a2f4f809e04e: Loading layer [==================================================>] 3.072kB/3.072kB 9b63e6289fbe: Loading layer [==================================================>] 4.096kB/4.096kB f7141923aaa3: Loading layer [==================================================>] 3.584kB/3.584kB 272bc57d3405: Loading layer [==================================================>] 7.168kB/7.168kB Loaded image: nginx:1.20.0 PS C:\Users\Peng Xiao\docker.tips\image> docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.20.0 7ab27dbbfbdf 12 days ago 133MB nginx latest f0b8a9a54136 12 days ago 133MB PS C:\Users\Peng Xiao\docker.tips\image>
    Dockerfile 介绍
    nginx.image

    Dockerfile介绍

    提示

    Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

    参考:https://docs.docker.com/engine/reference/builder/

    • Dockerfile是用于构建docker镜像的文件
    • Dockerfile里包含了构建镜像所需的“指令”
    • Dockerfile有其特定的语法规则
    举例:执行一个Python程序

    Dockerfile示例:执行Python程序

    容器及进程,所以镜像就是一个运行这个进程所需要的环境。

    假如我们要在一台ubuntu

    假设我们要在一台ubuntu 21.04上运行下面这个hello.

    py的Python程序

    py的Python程序。

    代码块
    titlehello.
    py的文件内容:
    py
    代码块
    print("hello docker")
    第一步,准备Python环境

    第一步,准备Python环境:

    代码块
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev

    第二步,运行hello.

    py

    py:

    代码块
    $ python3 hello.py
    hello docker


    以上步骤对应的Dockerfile为:

    代码块

    一个Dockerfile的基本结构

    Dockerfile

    titleDockerfile
    FROM ubuntu:21.04
    RUN apt-get update && \
        DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
    ADD hello.py /
    CMD ["python3", "/hello.py"]

    关于Dockerfile的语法可以参考:https://docs.docker.com/engine/reference/builder/

    镜像的构建和分享

    在当前目录下准备下面两个文件:

    代码块
    titlehello.py
    print("hello docker")
    代码块
    titleDockerfile
    代码块
    FROM ubuntu:21.04
    RUN apt-get update && \
        DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
    ADD hello.py /
    CMD ["python3", "/hello.py"]

    镜像的构建和分享

    通过 commit 创建镜像

    执行以下命令进行Dockerfile的构建:

    代码块
    docker build -t hello .

    命令参数可参考docker build --help-t用于指定镜像的名称和版本,比如hello:1.0.0,不指定版本时默认为latest.用于指定Dockerfile的路径。

    接下来就是创建容器并运行:

    代码块
    docker container run --it hello


    接下来是将创建好的镜像推送到Docker Hub,实现镜像的分享功能。

    首先需要注册Docker Hub的账号,然后在命令行通过docker login登录账号。接下来是构建符合Docker Hub命名风格的镜像,比如:

    代码块
    docker build -t luqiang2014/hello:v1.0 .

    或是将已经构建好的镜像重新打tag,比如:

    代码块
    docker image tag hello luqiang2014/hello:v1.0

    接下来就是通过docker image push实现镜像的推送:

    代码块
    dock image push luqiang2014/hello:v1.0

    推送完成后可以在Docker Hub的个人仓库界面查看仓库。还可以使用docker pull来拉取自己的仓库,如下:

    代码块
    docker image pull luqiang2014/hello:v1.0

    通过 commit 创建镜像

    从修改过的container中创建镜像,比如上面的python程序例子,可以先启动一个标准的ubuntu镜像,然后在容器的命令行中执行apt-get以安装python环境,然后再创建出hello.py。这时这个容器已经被修改过了,可以通过docker container commit命令将这个已经修改过的容器的镜像保存起来,再重新运行以修改过的镜像,如下:

    代码块
    docker container run -it ubuntu:21.04 sh
    apt-get update && ...
    echo "print('hello docker')" > /hello.py
    exit
    docker container commit <container ID> luqiang2014/python-demo
    docker container run -it luqiang2014/python-demo python3 /hello.py

    关于 scratch 镜像

    Scratch是一个空的Docker镜像。

    通过scratch来构建一个基础镜像。

    代码块
    titlehello.c
    #include <stdio.h>
    int main()
    {
        printf("hello docker\n");
    }

    编译成一个二进制文件

    代码块
    $ gcc --static -o hello hello.c
    $ ./hello
    hello docker
    $
    代码块
    titleDockerfile
    FROM scratch
    ADD hello /
    CMD ["/hello"]

    构建

    代码块
    $ docker build -t hello .
    $ docker image ls
    REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
    hello        latest    2936e77a9daa   40 minutes ago   872kB

    运行

    代码块
    $ docker container run -it hello
    hello docker






    目录