docker 部署和使用--部署

本文介绍了如何选择Docker版本,包括免费的Docker CE和商业的Docker EE。接着,讲解了如何查看操作系统版本,设置YUM源,并提供相关文件介绍。文章还详细说明了如何卸载旧版Docker,安装Docker CE,启动Docker服务,以及通过运行'hello-world'镜像来验证Docker安装是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 选择docker版本:
    docker ce:免费
    docker ee :商业
  2. 查看操作系统版本
[root@Grafana ~]# cat /etc/redhat-release   CentOS仅在7.0版本之后支持docker运行
CentOS Linux release 7.8.2003 (Core)
  1. 设置yum源
# 更换成aliyun yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#curl 是发出网络请求,然后获取数据,显示在"标准输出"(stdout)上面,加上 -o 文件名 可以将获取的数据保存到文件中
# 编译CentOS-Base.repo,把带mirrors.aliyuncs.com的行都删除
vi /etc/yum.repos.d/CentOS-Base.repo
# 运行以下命令生成缓存
yum clean all
yum makecache
# 查看已启用的repo,确保centos-extras repository是启用了,安装docker时需要
yum repolist
#--------------------------------------------------------------------------
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                  repo name                                                        status
base/7/x86_64                            CentOS-7 - Base - mirrors.aliyun.com                             9,591
extras/7/x86_64                          CentOS-7 - Extras - mirrors.aliyun.com                             284
updates/7/x86_64                         CentOS-7 - Updates - mirrors.aliyun.com                          1,540
repolist: 11,415
#--------------------------------------------------------------------------
#更新当前系统已安装的所有yum源
yum update

yum源相关文件介绍:
文件/etc/yum.conf

[main]
cachedir=/var/cache/yum         #yum下载的RPM包的缓存目录
keepcache=0             #缓存是否保存,1保存,0不保存。
debuglevel=2             #调试级别(0-10),默认为2(具体调试级别的应用,我也不了解)。
logfile=/var/log/yum.log         #yum的日志文件所在的位置
exactarch=1             #在更新的时候,是否允许更新不同版本的RPM包,比如是否在i386上更新i686的RPM包。
obsoletes=1             #这是一个update的参数,具体请参阅yum(8),简单的说就是相当于upgrade,允许更新陈旧的RPM包。
gpgcheck=1             #是否检查GPG(GNU Private Guard),一种密钥方式签名。
plugins=1             #是否允许使用插件,默认是0不允许,但是我们一般会用yum-fastestmirror这个插件。
installonly_limit=3         #允许保留多少个内核包。
exclude=selinux*         #屏蔽不想更新的RPM包,可用通配符,多个RPM包之间使用空格分离。

目录/etc/yum.repos.d/

[root@610 ~]# cd /etc/yum.repos.d/
[root@610 yum.repos.d]# ll
total 36
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo_bk
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo_bk
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo_bk
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo_bk
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo_bk
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo_bk
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo_bk
-rw-r--r--. 1 root root  186 Jun 30 21:45 localyum.repo
[root@610 yum.repos.d]# cat localyum.repo 
[localyum]
name=localyum
#baseurl=http://172.19.64.100:9999/yum/centos6.8
baseurl=http://10.49.100.50:8888/centos76
#baseurl=http://172.19.64.100:9999/yum/centos7.3
enabled=1
gpgcheck=0
  1. 卸载旧有版本docker,如果不存在删空气
[root@Grafana ~]# yum remove docker docker-common container-selinux docker-selinux docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-common
No Match for argument: container-selinux
No Match for argument: docker-selinux
No Match for argument:  docker-engine
No Packages marked for removal

或者

[root@Grafana ~]#yum remove docker \
                            docker-client \
                  			docker-client-latest \
                  			docker-common \
                  			docker-latest \
                  			docker-latest-logrotate \
                  			docker-logrotate \
  1. 安装docker ce
yum install -y yum-utils device-mapper-persistent-data lvm2
#device-mapper-persistent-data和lvm2是devicemapper存储驱动程序所必需的。devicemapper是docker的存储驱动
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
#--------------------------------------------------------------------------
.......
Installed:
  docker-ce.x86_64 0:17.09.1.ce-1.el7.centos                                                                                                                                    

Dependency Installed:
  audit-libs-python.x86_64 0:2.7.6-3.el7
  checkpolicy.x86_64 0:2.5-4.el7
  container-selinux.noarch 2:2.28-1.git85ce147.el7
  libcgroup.x86_64 0:0.41-13.el7
  libseccomp.x86_64 0:2.3.1-3.el7
  libsemanage-python.x86_64 0:2.5-8.el7
  policycoreutils-python.x86_64 0:2.5-17.1.el7
  python-IPy.noarch 0:0.75-6.el7    
  setools-libs.x86_64 0:3.3.8-1.1.el7       
......
#--------------------------------------------------------------------------
# 根据需要选择是否开启edge和test(边缘存储库) repositories  
yum-config-manager --enable docker-ce-edge
yum-config-manager --enable docker-ce-test
## 禁用命令
yum-config-manager --disable docker-ce-edge

## 安装指定的版本
yum list docker-ce --showduplicates | sort -r
#--------------------------------------------------------------------------
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            @docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
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 
Available Packages
#--------------------------------------------------------------------------

yum install docker-ce-17.06.1.ce
  1. 启动docker
systemctl start docker

# 查看docker的版本信息
docker version
#--------------------------------------------------------------------------
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
#--------------------------------------------------------------------------

# 查看网络信息
ip addr
#--------------------------------------------------------------------------
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:ab:4c:50 brd ff:ff:ff:ff:ff:ff
    inet 10.240.4.185/24 brd 10.240.4.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:feab:4c50/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN  # docker0 虚拟网桥
    link/ether 02:42:72:ac:05:bf brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:72ff:feac:5bf/64 scope link 
       valid_lft forever preferred_lft forever
#--------------------------------------------------------------------------

systemctl list-unit-files | grep docker
#--------------------------------------------------------------------------
docker.service                                disabled
#--------------------------------------------------------------------------

# 设置成自启服务
systemctl enable docker.service

# 查看状态
systemctl status docker
#--------------------------------------------------------------------------
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-12-12 17:24:31 CST; 13min ago
     Docs: https://docs.docker.com
 Main PID: 23479 (dockerd)
   CGroup: /system.slice/docker.service
           ├─23479 /usr/bin/dockerd
           └─23490 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libco...

Dec 12 17:24:29 docker01 dockerd[23479]: time="2017-12-12T17:24:29.594209004+08:00" level=info msg="libcontainerd: new containerd process, pid: 23490"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.596093094+08:00" level=warning msg="failed to rename /var/lib/docker/tmp for background deletio...chronously"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654014669+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654714697+08:00" level=info msg="Loading containers: start."
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.852920366+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17...IP address"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.996504508+08:00" level=info msg="Loading containers: done."
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004149257+08:00" level=info msg="Docker daemon" commit=19e2cf6 graphdriver(s)=overlay version=17.09.1-ce
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004282017+08:00" level=info msg="Daemon has completed initialization"
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.015479108+08:00" level=info msg="API listen on /var/run/docker.sock"
Dec 12 17:24:31 docker01 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
#--------------------------------------------------------------------------

运行docker run hello-world image验证docker安装是否成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
  1. 升级和卸载docker
# 升级
yum -y upgrade docker-ce

# 卸载
yum remove docker-ce

# 删除Images, containers, volumes, or customized configuration files 
rm -rf /var/lib/docker
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值