使用本脚本可以完成 Centos7 下的 docker 以及 docker-compose 自动安装。

完整脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
set -Eeuo pipefail

echo "Updating DNS"
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
systemctl restart sshd

echo "Updating Firewalld"
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config


rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

yum makecache
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum install docker-ce -y


systemctl enable docker
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vsktzm61.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

yum install python3 -y
pip3 install setuptools pip -U -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install docker-compose -i https://mirrors.aliyun.com/pypi/simple/

tee docker-compose.yml <<-'EOF'
version: '3'
services:
postgres:
image: postgres:13-alpine
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: 123456
command: postgres -c max_connections=2000 -c statement_timeout=20000 -c idle_in_transaction_session_timeout=15000
networks:
- docker
networks:
docker:
external: false
EOF

docker-compose up -d

分段解释

关闭SSH的DNS查询

1
2
3
echo "Updating DNS"
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
systemctl restart sshd

可以加快ssh的登录验证速度

关闭防火墙

1
2
3
4
5
6
echo "Updating Firewalld"
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

建议在开发环境中关闭,在生产环境下不关闭

配置阿里云软件源

1
2
3
4
rm -rf  /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum makecache

使用阿里源作为软件源

此处会删除掉 /etc/yum.repos.d/ 内的所有文件,可适当修改

安装docker

1
2
3
4
yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum install docker-ce -y

设置docker镜像加速

1
2
3
4
5
6
7
8
9
systemctl enable docker
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vsktzm61.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

采用的阿里云的镜像加速,也可以自己申请 加速地址

安装docker-compose并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
yum install python3 -y
pip3 install setuptools pip -U -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install docker-compose -i https://mirrors.aliyun.com/pypi/simple/
tee docker-compose.yml <<-'EOF'
version: '3'
services:
postgres:
image: postgres:13-alpine
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: 123456
command: postgres -c max_connections=2000 -c statement_timeout=20000 -c idle_in_transaction_session_timeout=15000
networks:
- docker
networks:
docker:
external: false
EOF
docker-compose up -d