CentOS安装docker

这里介绍两种方式安装,压缩包方式和rpm安装包方式

1 rpm方式

1.1 前提条件

1.1.1 内核

Docker运行对内核要求比较高。

  • CentOS 7(64位)
  • CentOS 6.5(64位)及以后

CentOS 6.5及以后版本时,需要内核版本>=2.6.32-431,CentOS 7是3.10内核

# 查询内核信息
uname -r
1.1.2 Device Mapper

Docker默认使用AUFS作为存储驱动,但是AUFS并没有被包括在Linux的主线内核中。

CentOS中可以使用Device Mapper作为存储驱动,这是在2.6.9内核版本引入的新功能。我们需要先确认是否启用该功能:

ls -l /sys/class/misc/device-mapper

如果没有检测到Device Mapper,需要安装device-mapper软件包:

下载地址:http://mirror.centos.org/centos/7/updates/x86_64/Packages/device-mapper-1.02.170-6.el7_9.5.x86_64.rpm

# 安装
sudo yum install -y device-mapper

# 重新加载dm_mod内核模块
sudo modprobe dm_mod

1.2 安装

1.2.1 在线安装
  • 设置源
# 更新yum
sudo yum update

# 卸载旧版docker
sudo yum remove docker  docker-common docker-selinux docker-engine

# 安装需要的软件包,yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 设置yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • 安装
# 查看仓库中docker软件版本
yum list docker-ce.x86_64  --showduplicates | sort -r    #从高到低列出Docker-ce的版本

# 安装最新版
sudo yum -y install docker-ce
1.2.2 离线安装

下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

rpm -ivh docker-ce-19.03.14-3.el7.x86_64.rpm
1.2.3 启动
# 启动并开机启动
sudo systemctl start docker
sudo systemctl enable docker

# 验证,有client和service两部分表示docker安装启动都成功
docker version

1.3 安装 docker-compose

地址:https://docs.docker.com/compose/install/

# 可离线下载,上传至/usr/local/bin 改名为 docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose --version

# 增加docker-compose自动补全功能
 sudo curl -L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

1.4 配置远程访问

vi /usr/lib/systemd/system/docker.service

再 ExecStart 后加上参数

 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

完整信息如下

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

重启服务即可

systemctl daemon-reload
systemctl restart docker

2 tar.gz方式

2.1 下载

地址:https://download.docker.com/linux/static/stable/x86_64/

这里下载的是:docker-19.03.14

2.2 安装

将压缩包上传至服务器,解压

tar zxf docker-19.03.14.tgz

将docker 相关命令移到 /usr/bin,方便直接运行命令

mv docker/* /usr/bin/

2.3 自启服务

vi /etc/systemd/system/docker.service

内容

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
 
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
 
[Install]
WantedBy=multi-user.target

添加执行权限

chmod +x /etc/systemd/system/docker.service

配置启动

systemctl daemon-reload

systemctl enable docker.service

systemctl start docker

查看版本

docker -v

2.4 配置

镜像配置

cat > /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
EOF

刷新配置

systemctl daemon-reload

systemctl restart docker
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值