CentOS 7 中 Docker 的安装

2019-03-04  本文已影响30人  椟夜
安装Docker

卸载旧的:yum erase docker

[root@localhost ~]# yum install docker
启动 Docker 服务

安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:

[root@localhost ~]# service docker start
[root@localhost ~]# systemctl enable docker
验证安装是否成功
[root@localhost ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-91.git07f3374.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      07f3374/1.13.1
 Built:           Wed Feb 13 17:10:12 2019
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-91.git07f3374.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      07f3374/1.13.1
 Built:           Wed Feb 13 17:10:12 2019
 OS/Arch:         linux/amd64
 Experimental:    false
下载官方的 CentOS 镜像到本地
[root@localhost ~]# docker pull centos
确认 CentOS 镜像已经被获取

镜像基本操作
docker images [OPTIONS] [REPOSITORY]

-a,–all=false,显示所有镜像

-f,–filter=[],显示时过滤条件

–no-trunc=false,指定不使用截断的形式显示数据

-q,–quiet=false,只显示镜像的唯一id
docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE]

[root@localhost ~]# docker inspect docker.io/centos
[
    {
        "Id": "sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb",
        "RepoTags": [
            "docker.io/centos:latest"
        ],
        "RepoDigests": [
            "docker.io/centos@sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426"
        ],
        "Parent": "",
        "Comment": "",
docker rmi [OPTIONS] IMAGE [IMAGE]

-f,–force=false,强制删除镜像

–no-prune=false,保留未打标签的父镜像

[root@localhost ~]# docker rmi docker.io/centos
docker search [OPTIONS] TEAM

–automated=false,仅显示自动化构建的镜像

–no-trunc=false,不以截断的方式输出

–filter,添加过滤条件

[root@localhost ~]# docker search docker.io
INDEX       NAME                                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/macropin/sshd                                    (moved) Use docker.io/panubo/sshd               5                    [OK]
docker.io   docker.io/kiizawa/siriusdev                                Dev image for DOE SIRIUS project, based on...   1                    
docker.io   docker.io/maccyber/dockerhub-webhook-api                   Listen for web hooks from docker.io builds...   1                    [OK]
docker.io   docker.io/4km3/jenkins-docker                              Run a Jenkins server with Docker.io binari...   0                    [OK]
docker pull [OPTIONS] NAME [:TAG]

-a,–all-tags=false,下载所有的镜像(包含所有TAG)

[root@localhost ~]# docker pull docker.io/centos
*docker push NAME [:TAG]*

Docker允许上传我们自己构建的镜像,需要注册DockerHub的账户。也可以上传到阿里云,地址:[https://cr.console.aliyun.com/#/namespace/index](https://cr.console.aliyun.com/#/namespace/index)

通过容器构建:docker commit
通过Dockerfile:docker build
使用commit命令构建镜像

命令:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

参数:-a,–author=“”,指定镜像的作者信息

​ -m,–message=“”,提交信息

​ -p,–pause=true,commit时是否暂停容器
命令:docker build [OPTIONS] DockerFile_PATH | URL | -

参数:–force-rm=false

​ –no-cache=false

​ –pull=false

​ -q,quite=false,构建时不输出信息

​ –rm=true

​ -t,tag=“”,指定输出的镜像名称信息

镜像迁移

我们制作好的镜像,一般会迁移或分享给其他需要的人。Docker提供了几种将我们的镜像迁移、分享给其他人的方式。推荐镜像迁移应该直接使用Docker Registry,无论是直接使用Docker Hub还是使用内网私有Registry都可以。使用镜像频率不高,镜像数量不多的情况下,我们可以选择以下两种方式。

docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]

其中existing-image代表本地待上传的镜像名加tag,后面<hub-user>/<repo-name>[:<tag>]则是为上传更改的标签名,tag不指定则为latest。
可以看到,我们重新为ubuntu:16.04的镜像打上标签,观察IMAGE ID可知,同一镜像可以拥有不同的标签名。接下来,我们利用push命令直接上传镜像。

docker push <hub-user>/<repo-name>:<tag>

如图,我们已经上传成功。由于之前介绍的分层存储系统,我们这里是直接对已有的ubuntu镜像进行上传,只是重新打了标签,所以真正上传的只是变化的部分。

导出文件互传

Docker 还提供了 docker load 和 docker save 命令,用以将镜像保存为一个tar文件。比如这次我们将ubuntu:latest这个镜像保存为tar文件。

查看本地磁盘,即可看见名为ubuntu18.04的tar包。我们可以将其拷贝给其他PC,利用load命令重新导入。

上一篇 下一篇

猜你喜欢

热点阅读