美文网首页
安装 Docker

安装 Docker

作者: 慕容鸿煊 | 来源:发表于2019-03-24 16:16 被阅读0次

    卸载

    1.查询安装过的包

    yum listinstalled | grep docker

    docker-engine.x86_64                17.03.0.ce-1.el7.centos        @dockerrepo

     2.删除安装的软件包

    yum -y remove docker-engine.x86_64

    3.删除镜像/容器等

    rm -rf /var/lib/docker

    CentOS6

    对于 CentOS6,可以使用 EPEL 库安装 Docker,命令如下

    $ sudo yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm

    $ sudo yum install docker-io

    CentOS7

    CentOS7 系统 CentOS-Extras 库中已带 Docker,可以直接安装:

    $ sudo yum install docker            ##不是最新版本

    [if !vml]

    [endif]

    #最新版安装

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2

    sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

    sudo yum install docker-ce

    查看docker版本

    docker version

    docker info

    启动docker

    sudo service docker start

    设置随系统启动

    sudo chkconfig docker on

    Docker初体验

    docker run hello-world  ##进入docker世界                                      [if !vml]

    [endif]

    Docker基本操作

    容器操作

    docker [run|start|stop|restart|kill|rm|pause|unpause]

    [if !supportLists]·  [endif]run/create[镜像名]:  创建一个新的容器并运行一个命令

    [if !supportLists]·  [endif]start/stop/restart[容器名]:启动/停止/重启一个容器

    [if !supportLists]·  [endif]kill [容器名]: 直接杀掉容器,不给进程响应时间

    [if !supportLists]·  [endif]rm[容器名]:删除已经停止的容器

    [if !supportLists]·  [endif]pause/unpause[容器名]:暂停/恢复容器中的进程

    docker [ps|inspect|exec|logs|export|import]

    [if !supportLists]·  [endif]ps:查看容器列表(默认查看正在运行的容器,-a查看所有容器)

    [if !supportLists]·  [endif]inspect[容器名]:查看容器配置元数据

    [if !supportLists]·  [endif]exec -it [容器名] /bin/bash:进入容器环境中交互操作

    [if !supportLists]·  [endif]logs --since="2019-02-01" -f --tail=10 [容器名]:查看容器日志

    [if !supportLists]·  [endif]cp path1 [容器名]:path 容器与主机之间的数据拷贝

    [if !supportLists]·  [endif]export -o test.tar [容器名] / docker export [容器名]>test.tar : 文件系统作为一个tar归档文件

    [if !supportLists]·  [endif]import

    test.tar [镜像名:版本号]:导入归档文件,成为一个镜像

    镜像操作

    docker images|rmi|tag|build|history|save|load]

    [if !supportLists]·  [endif]images:列出本地镜像列表

    [if !supportLists]·  [endif]rmi [镜像名:版本]:删除镜像

    [if !supportLists]·  [endif]tag [镜像名:版本] [仓库]/[镜像名:版本]:标记本地镜像,将其归入某一仓库

    [if !supportLists]·  [endif]build -t [镜像名:版本]

    [path]:Dockerfile 创建镜像

    [if !supportLists]·  [endif]history [镜像名:版本]: 查看指定镜像的创建历史

    [if !supportLists]·  [endif]save -o xxx.tar [镜像名:版本] /  save [镜像名:版本]>xxx.tar : 将镜像保存成 tar 归档文件

    [if !supportLists]·  [endif]load --input  xx.tar / docker load<xxx.tar

    : 从归档文件加载镜像

    镜像与容器原理及用法探究

    history命令查看镜像层

    例:docker history hello-world

    [if !vml]

    [endif]

    显示镜像hello-world分三层,其中两个空层

    查看镜像文件

    镜像存放在imagedb里

    一般在image/overlay2/imagedb/content/sha256下

    [if !vml]

    [endif]

    打开一个镜像文件查看其内容:

    cat f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80

    {

        "architecture": "amd64",

        "config": {

                    "Hostname":"",

                    "Domainname":"",

                    "User": "",

                    "AttachStdin": false,

                    "AttachStdout": false,

                    "AttachStderr": false,

                    "Tty": false,

                    "OpenStdin": false,

                    "StdinOnce": false,

                    "Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],

                    "Cmd":["/hello"],

                    "ArgsEscaped": true,

                    "Image": "sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",

                    "Volumes": null,

                    "WorkingDir":"",

                    "Entrypoint": null,

                    "OnBuild": null,

                    "Labels": null

        },

        "container":"8e2caa5a514bb6d8b4f2a2553e9067498d261a0fd83a96aeaaf303943dff6ff9",

        "container_config": {

                    "Hostname":"8e2caa5a514b",

                    "Domainname":"",

                    "User": "",

                    "AttachStdin": false,

                    "AttachStdout": false,

                    "AttachStderr": false,

                    "Tty": false,

                    "OpenStdin": false,

                    "StdinOnce": false,

                    "Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],

                    "Cmd":["/bin/sh", "-c", "#(nop) ", "CMD[\"/hello\"]"],

                    "ArgsEscaped": true,

                    "Image":"sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",

                    "Volumes": null,

                    "WorkingDir":"",

                    "Entrypoint": null,

                    "OnBuild": null,

                    "Labels": {}

        },

        "created":"2019-01-01T01:29:27.650294696Z",

        "docker_version":"18.06.1-ce",

        "history": [{

                    "created":"2019-01-01T01:29:27.416803627Z",

                    "created_by":"/bin/sh -c #(nop) COPY file:f77490f70ce51da25bd21bfc30cb5e1a24b2b65eb37d4af0c327ddc24f0986a6in / "

        }, {

                    "created":"2019-01-01T01:29:27.650294696Z",

                    "created_by":"/bin/sh -c #(nop)  CMD[\"/hello\"]",

                    "empty_layer": true

        }],

        "os": "linux",

        "rootfs": {

                    "type": "layers",

                    "diff_ids":["sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3"]

        }

    }

    ----其中,history数组内,标识了镜像的历史记录(与history命令内容对应)

    ----rootfs的diff_ids中,对应了依赖使用中镜像层文件(history命令中size大于0的层)

    查看镜像层文件

    层文件在layerdb里

    ll/var/lib/docker/image/overlay2/layerdb/sha256

    [if !vml]

    [endif]

    #镜像层文件内结构:

    [if !vml]

    [endif]

    镜像与容器总结

    一个镜像就是一层层的layer层文件,盖楼而成,上层文件叠于下层文件上,若上层文件有与下层文件重复的,则覆盖掉下层文件重复的部分,如下图:

    [if !vml]

    [endif]

    ---------初始挂载时读写层为空。

    ---------当需要修改镜像内的某个文件时,只对处于最上方的读写层进行了变动,不复写下层已有文件系统的内容,已有文件在只读层中的原始版本仍然存在,但会被读写层中的新版本文件所隐藏,当 docker commit 这个修改过的容器文件系统为一个新的镜像时,保存的内容仅为最上层读写文件系统中被更新过的文件。

    ---------联合挂载是用于将多个镜像层的文件系统挂载到一个挂载点来实现一个统一文件系统视图的途径,是下层存储驱动(aufs、overlay等) 实现分层合并的方式。

    容器创建详解

    交互式创建容器并进入:

    [if !vml]

    [endif]

    docker run -it --name centos centos /bin/bash(前台进程)

    ------------------------exit退出也关闭容器; Ctrl+P+Q退出不关闭容器

    [if !vml]

    [endif]

    后台启动容器:

    [if !vml]

    [endif]

    docker run -d --name nginx nginx

    进入已运行的容器:

    docker exec -it nginx /bin/bash

    查看容器的元数据:docker inspect nginx 

    绑定容器端口到主机:

    docker run -d -p 8080:80 --name nginx nginx:latest

    挂载主机文件目录到容器内:

     docker run -dit -v/root/peter_dir/:/pdir --name cent centos

    复制主机文件到容器内:

    docker cp anaconda-ks.cfg cent:/var

    相关文章

      网友评论

          本文标题:安装 Docker

          本文链接:https://www.haomeiwen.com/subject/hlxmyttx.html