CentOS 7 安装Docker

本文详细介绍了在CentOS 7系统上安装Docker的步骤,包括环境检查、更新包、设置YUM源、安装Docker及依赖、启动与配置Docker、加速镜像拉取、设置IP转发和创建用户等,适用于初学者和需要在CentOS环境下部署Docker的开发者。

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

 

1、检查环境

a)查看CentOS版本

[root@localhost etc]# cat /etc/system-release    
CentOS Linux release 7.3.1611 (Core) 

b)查看内核版本(需高于3.10)

[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64

2、更新yum包(需root权限)

[root@localhost ~]# sudo yum update

3、卸载旧版本(如果安装过旧版本的话)

[root@localhost ~]# sudo yum remove docker  docker-common docker-selinux docker-engine

4、安装需要的软件包

yum-util 提供yum-config-manager功能

另外两个是devicemapper驱动依赖的

[root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2

5、设置yum源

[root@localhost ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo


Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

6、可以查看所有仓库中所有docker版本,并选择特定版本安装

[root@localhost ~]# yum list docker-ce --showduplicates | sort -r


 * updates: mirrors.tuna.tsinghua.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
 * extras: mirrors.tuna.tsinghua.edu.cn
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.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.3.ce-1.el7                    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
 * base: mirrors.aliyun.com
Available Packages

7、安装docker

a)在线

[root@localhost ~]# sudo yum install docker-ce


Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.06.1.ce-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: docker-ce-18.06.1.ce-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.68-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                            Arch                    Version                            Repository                         Size
=======================================================================================================================================
Installing:
 docker-ce                          x86_64                  18.06.1.ce-3.el7                   docker-ce-stable                   41 M
Installing for dependencies:
 container-selinux                  noarch                  2:2.68-1.el7                       extras                             36 k

Transaction Summary
=======================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 41 M
Installed size: 41 M
Is this ok [y/d/N]: y

b)离线

下载

ftp.scientificlinux.org/linux/scientific/7x/external_products/extras/x86_64/container-selinux-2.68-1.el7.noarch.rpm

https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.06.1.ce-3.el7.x86_64.rpm

[root@localhost docker]# pwd
/usr/local/src/docker
[root@localhost docker]# ll
total 41748
-rw-r--r--. 1 root root    35676 Oct  3 22:34 container-selinux-2.68-1.el7.noarch.rpm
-rw-r--r--. 1 root root 42712724 Oct  3 22:26 docker-ce-18.06.1.ce-3.el7.x86_64.rpm

安装

[root@localhost docker]# rpm -ivh docker-ce-18.06.1.ce-3.el7.x86_64.rpm 
warning: docker-ce-18.06.1.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
error: Failed dependencies:
        container-selinux >= 2.9 is needed by docker-ce-18.06.1.ce-3.el7.x86_64

注意:需先安装依赖!

下面代码为正确安装顺序

[root@localhost docker]# rpm -ivh container-selinux-2.68-1.el7.noarch.rpm
warning: container-selinux-2.68-1.el7.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID 192a7d7d: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:container-selinux-2:2.68-1.el7   ################################# [100%]
[root@localhost docker]# rpm -ivh docker-ce-18.06.1.ce-3.el7.x86_64.rpm 
warning: docker-ce-18.06.1.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:docker-ce-18.06.1.ce-3.el7       ################################# [100%]
[root@localhost docker]# 

8、启动并加入开机启动

[root@localhost docker]# sudo systemctl start docker
[root@localhost docker]# sudo systemctl enable docker


Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

9、验证安装是否成功

(有client和service两部分表示docker安装启动都成功了)

[root@localhost docker]# docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:23:03 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:25:29 2018
  OS/Arch:          linux/amd64
  Experimental:     false

10、启用停用

[root@localhost docker]# systemctl stop docker.service 
[root@localhost docker]# systemctl start docker.service

11、镜像加速

[root@localhost workspace]# mkdir -p  /etc/docker
[root@localhost docker]# sudo tee -a /etc/docker/daemon.json <<-EOF
{
   "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
[root@localhost workspace]# sudo systemctl daemon-reload
[root@localhost workspace]# sudo systemctl restart docker

12、IP转发

sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF


# 重新加载
sudo sysctl -p

13、建立用户

# 增加docker用户到 docker组
useradd docker -g docker
# 修改密码
passwd docker
sudo systemctl restart docker

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值