Docker是Linux系统中的容器管理工具,基于go语言开发,容器使用鲜明的特点是需要具有隔离能力。

Docker安装前首先准备环境

[root@localhost ~]# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2081 100 2081 0 0 3768 0 --:--:-- --:--:-- --:--:-- 3769

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2021-05-10 11:03:35-- http://mirrors.aliyun.com/repo/epel-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 61.159.80.240, 61.159.81.243, 61.159.81.242, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|61.159.80.240|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:664 [application/octet-stream]
正在保存至: “/etc/yum.repos.d/epel.repo”

100%[=============================================================>] 664 --.-K/s 用时 0s

2021-05-10 11:03:36 (73.3 MB/s) - 已保存 “/etc/yum.repos.d/epel.repo” [664/664])

[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 8315 0 --:--:-- --:--:-- --:--:-- 8326

 安装Docker之前先安装依赖包

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com

---------------------------------------------------------------------------------------------------------------------------------------------------------------

省略部分输出信息

----------------------------------------------------------------------------------------------------------------------------------------------------------------

软件包 7:lvm2-2.02.187-6.el7_9.5.x86_64 已安装并且是最新版本
无须任何处理

[root@localhost ~]# yum list docker-ce.x86_64 --showduplicates | sort -r    #查看docker-ce相关的安装包
file:///media/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /media/cdrom/repodata/repomd.xml"
正在尝试其它镜像。
已加载插件:fastestmirror, langpacks
可安装的软件包
* updates: mirrors.aliyun.com

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

省略部分输出信息

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.aliyun.com

安装Docker软件包

[root@localhost ~]# yum install -y --setopt=obsoletes=0 \               #注意此处的书写方式
> docker-ce-17.03.2.ce-1.el7.centos.x86_64 \
> docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
file:///media/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /media/cdrom/repodata/repomd.xml"
正在尝试其它镜像。
正在解决依赖关系

 

----------------------------------------------------------------------------------------------------------------------

省略部分输出信息

----------------------------------------------------------------------------------------------------------------------

已安装:
docker-ce.x86_64 0:17.03.2.ce-1.el7.centos docker-ce-selinux.noarch 0:17.03.2.ce-1.el7.centos

完毕!

启动Docker服务 

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker version    #查看版本
Client:
Version: 17.03.2-ce

--------------------------------------------------------------------------------------------------------

省略部分输出信息

---------------------------------------------------------------------------------------------------------

Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

配置镜像加速,记得保存退出

[root@localhost ~]# vim /etc/docker/daemon.json

{
"registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"]
}

Docker镜像管理基础(命令)

获取镜像

[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6541 [OK]

----------------------------------------------------------------------------------------------

省略部分输出信息

-----------------------------------------------------------------------------------------------
smartentry/centos centos with smartentry 0 [OK]

[root@localhost ~]# docker pull centos:6.9   #获取centos6.9 的镜像   

6.9: Pulling from library/centos
831490506c47: Pull complete
Digest: sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2
Status: Downloaded newer image for centos:6.9

[root@localhost ~]# docker pull centos:7.5.1804    #获取7.5的镜像

7.5.1804: Pulling from library/centos
5ad559c5ae16: Pull complete
Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720a
Status: Downloaded newer image for centos:7.5.1804

[root@localhost ~]# docker pull nginx  #获取最新的镜像

Using default tag: latest
latest: Pulling from library/nginx
f7ec5a41d630: Pull complete
aa1efa14b3bf: Pull complete
b78b95af9b17: Pull complete
c7d6bca2b8dc: Pull complete
cf16cd8e71e0: Pull complete
0241c68333ef: Pull complete
Digest: sha256:75a55d33ecc73c2a242450a9f1cc858499d468f077ea942867e662c247b5e412
Status: Downloaded newer image for nginx:latest

查询镜像

[root@localhost ~]# docker images
REPOSITORY     TAG            IMAGE ID                 CREATED                SIZE
nginx                    latest          62d49f9bab67           3 weeks ago            133 MB
centos                  6.9              2199b8eb8390          2 years ago             195 MB
centos                  7.5.             cf49811e3cdb            2 years ago             200 MB

[root@localhost ~]# docker images -q   #查询镜像的ID
62d49f9bab67
2199b8eb8390
cf49811e3cdb

 删除镜像命令:  docker rm IID、docker rm 'docker images -q'、docker rm $(docker images -q)

一般情况下,不要删除,我们只需要了解命令即可,强制删除的时候只需要在rm后面加-f即可。

镜像的导入导出

[root@localhost ~]# docker image save nginx >/opt/nginx.tar.gz  #导入镜像
[root@localhost ~]# ls /opt/nginx.tar.gz    #查看是否导入
/opt/nginx.tar.gz

[root@localhost ~]# docker image load -i /opt/nginx.tar.gz   #导出镜像
Loaded image: nginx:latest

 

 

 

posted on 2021-05-10 16:40  与所有美好不期而遇  阅读(165)  评论(0)    收藏  举报