第一天docker入门

Posted nerdlerss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一天docker入门相关的知识,希望对你有一定的参考价值。

【01 入门】

  docker 最核心为三部分组成 镜像,仓库和容器

  镜像:一个只读的模板

  仓库:代码仓库,镜像的集合

  容器:镜像的实例化进程

我们可以这样理解 容器就是一个沙箱,docker利用容器运行和隔离应用,每个容器之间都是彼此独立的

第一次使用docker 

[1]查看版本

[email protected]:/data# docker version
Client:
 Version:      1.13.1
 API version:  1.26
 Go version:   go1.7.4
 Git commit:   092cba3
 Built:        Thu Sep  7 17:09:45 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.13.1
 API version:  1.26 (minimum version 1.12)
 Go version:   go1.7.4
 Git commit:   092cba3
 Built:        Thu Sep  7 17:09:45 2017
 OS/Arch:      linux/amd64
 Experimental: false

docker 由客户端和服务器组成

[2]使用镜像

##查看本地镜像
[email protected]:/data# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        3 weeks ago         112 MB
##这里我们有一个Ubuntu 16.04 镜像

我们还可以获取其他镜像 如果不加参数 默认会去 docker hub 里面拉取 你可以指定远程的docker源 这个很pip yum 很像

[email protected]:/data# docker pull ubuntu:15.04
15.04: Pulling from library/ubuntu
9502adfba7f1: Pull complete 
4332ffb06e4b: Pull complete 
2f937cc07b5f: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:2fb27e433b3ecccea2a14e794875b086711f5d49953ef173d8a03e8707f1510f
Status: Downloaded newer image for ubuntu:15.04
[email protected]:/data# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        3 weeks ago         112 MB
ubuntu              15.04               d1b55fd07600        2 years ago         131 MB

这里我们就有两个镜像啦

当然我们可以执行远程的源

[email protected]:/data# docker pull hub.c.163.com/public/ubuntu:14.04
14.04: Pulling from public/ubuntu
f4ab0b34ba6a: Pull complete 
2f0787dc0bfe: Pull complete 
54c427d77362: Pull complete 
51e87d5424ff: Pull complete 
5dfa314e7290: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:ffc2fc66f8e0bfa4b417b817054d3ebec130c8db44342b8fa394e25779633257
Status: Downloaded newer image for hub.c.163.com/public/ubuntu:14.04
[email protected]:/data# 

这里演示的是从163的源 下载镜像

docket tag  可以一个docker镜像增加一个标签 实际上还是一个镜像 但是新建一个快捷名称

 

[email protected]:/data# docker tag ubuntu:15.04 test:15.04
[email protected]:/data# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
ubuntu                        16.04               f975c5035748        3 weeks ago         112 MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237 MB
ubuntu                        15.04               d1b55fd07600        2 years ago         131 MB
test                          15.04               d1b55fd07600        2 years ago         131 MB

docker inspect 可以列出镜像的详细信息

[email protected]:/data# docker inspect ubuntu:15.04
[
    {
        "Id": "sha256:d1b55fd07600b2e26d667434f414beee12b0771dfd4a2c7b5ed6f2fc9e683b43",
        "RepoTags": [
            "test:15.04",
            "ubuntu:15.04"
        ],
        "RepoDigests": [
            "[email protected]:2fb27e433b3ecccea2a14e794875b086711f5d49953ef173d8a03e8707f1510f"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2016-01-26T17:48:34.465253028Z",
        "Container": "cc20825e9e02f79fe62f1b08bc30aea4299dbcd77ad0da58b00dec38aba7334a",
        "ContainerConfig": {
            "Hostname": "d2d404286fc4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) CMD [\"/bin/bash\"]"
            ],
            "Image": "4913eece27c087d06635d32b98c50464b2f1d17bba86be7d4616738f1cfef0af",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "1.8.3",
        "Author": "",
        "Config": {
            "Hostname": "d2d404286fc4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "4913eece27c087d06635d32b98c50464b2f1d17bba86be7d4616738f1cfef0af",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 131299498,
        "VirtualSize": 131299498,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:3cbe18655eb617bf6a146dbd75a63f33c191bf8c7761bd6a8d68d53549af334b",
                "sha256:84cc3d400b0d610447fbdea63436bad60fb8361493a32db380bd5c5a79f92ef4",
                "sha256:ed58a6b8d8d6a4e2ecb4da7d1bf17ae8006dac65917c6a050109ef0a5d7199e6",
                "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
            ]
        }
    }
]
r

这里docker会返回一个字典

用-f 参数就可以获取其中一个

[email protected]:/data# docker inspect ubuntu:15.04 -f {{".Id"}}
sha256:d1b55fd07600b2e26d667434f414beee12b0771dfd4a2c7b5ed6f2fc9e683b43

用docker history 可以查看镜像历史

[email protected]:/data# docker history ubuntu:15.04
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d1b55fd07600        2 years ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0 B                 
<missing>           2 years ago         /bin/sh -c sed -i ‘s/^#\s*\(deb.*universe\...   1.88 kB             
<missing>           2 years ago         /bin/sh -c echo ‘#!/bin/sh‘ > /usr/sbin/po...   701 B               
<missing>           2 years ago         /bin/sh -c #(nop) ADD file:3f4708cf445dc1b...   131 MB  

  

[3] 搜索镜像

使用docker serch 可以搜索镜像名称  

支持的参数有:

  --automated=ture|false 仅显示 自动创建的镜像,默认为否

  --no-trunc=ture|false 输出信息不截断显示 默认为否

   -s 指定评分

[email protected]:/data# docker search --automated -s 3 nginx
Flag --automated has been deprecated, use --filter=automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1300                 [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   540                  [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as...   338                  [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              97                   [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server ...   48                   [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      45                   [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          31                   [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   19                   [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                9                    [OK]
webdevops/nginx                                        Nginx container                                 8                    [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          8                    [OK]
nginxdemos/hello                                       NGINX webserver that serves a simple page ...   5                    [OK]
1science/nginx                                         Nginx Docker images that include Consul Te...   4                    [OK]

 [4] 删除镜像

  docker rmi  指定tag 或者 ID 

   -f 强制删除正在运行的容器

[5] 创建镜像

  

以上是关于第一天docker入门的主要内容,如果未能解决你的问题,请参考以下文章

力扣——算法入门计划第一天

Vue实战-第一天开发工具的使用

struts2第一天——入门和基本操作

docker第一天:docker的基本原理与安装

C++从入门到精通开发第一天 - C++的Hello World

学习php第一天