3.Alpine 配置脚本

除了可以用“setup-alpine”配置服务器,也可通过配置脚本单独配置

建立基于Alpine3.20.2虚拟机优化版的docker环境(二)_配置文件

具体用法详见Alpine setup scripts - Alpine Linux


4.APK Repositories及更换国内镜像源

APK Repositories三种存储库:main(官方正式), community (官方与社区合作),testing(其他第三方)。

一般只需配置main 和 community ,配置文件为:  /etc/apk/repositories


4.1 自动配置

执行setup-apkrepos -cf脚本会自动查找最快的镜像url并更新/etc/apk/repositories,随后执行apk update更新包索引。

localhost:~# setup-apkrepos -cf
Finding fastest mirror...
0.65 http://dl-cdn.alpinelinux.org/alpine/
1.73 http://uk.alpinelinux.org/alpine/
0.63 http://mirrors.hostico.ro/alpinelinux/
wget: server returned error: HTTP/1.1 403 Forbidden
0.66 http://ftp.lysator.liu.se/pub/alpine/
...
0.77 http://elmirror.cl/alpine/
0.54 http://alpinelinux.mirrors.ovh.net/
Added mirror mirrors.jcut.edu.cn
Updating repository indexes... done.
localhost:~# apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.jcut.edu.cn/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.jcut.edu.cn/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
v3.20.2-168-gb223e202b56 [http://dl-cdn.alpinelinux.org/alpine/v3.20/main]
v3.20.2-168-gb223e202b56 [http://mirrors.jcut.edu.cn/alpine/v3.20/main]
v3.20.2-173-g48919cb11b1 [http://mirrors.jcut.edu.cn/alpine/v3.20/community]
OK: 24162 distinct packages available
localhost:~#

参考: setup-apkrepos 

4.2手工配置国内源

 稳定的国内源主要有下面三个,一般选用与系统版本匹配的URL

中科大:http://mirrors.ustc.edu.cn/alpine/

https://mirrors.ustc.edu.cn/alpine/v3.20/main/

https://mirrors.ustc.edu.cn/alpine/v3.20/community/


阿里云:https://mirrors.aliyun.com/alpine/

https://mirrors.aliyun.com/alpine/v3.20/main/

https://mirrors.aliyun.com/alpine/v3.20/community/


清华大学:https://mirror.tuna.tsinghua.edu.cn/alpine/

https://mirror.tuna.tsinghua.edu.cn/alpine/v3.20/main/

https://mirror.tuna.tsinghua.edu.cn/alpine/v3.20/community/

注释原来的配置,选用中科大的配置(随便选一组配置即可)。

#/media/cdrom/apks
https://mirrors.ustc.edu.cn/alpine/v3.20/main
https://mirrors.ustc.edu.cn/alpine/v3.20/community
#http://dl-cdn.alpinelinux.org/alpine/v3.20/main
#http://dl-cdn.alpinelinux.org/alpine/v3.20/community
localhost:~# vi /etc/apk/repositories
localhost:~# localhost:~# apk update
fetch https://mirrors.ustc.edu.cn/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.ustc.edu.cn/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
v3.20.2-168-gb223e202b56 [https://mirrors.ustc.edu.cn/alpine/v3.20/main]
v3.20.2-173-g48919cb11b1 [https://mirrors.ustc.edu.cn/alpine/v3.20/community]
OK: 24162 distinct packages available
localhost:~#

5.安装docker及更换国内镜像源

参考:Docker - Alpine Linux

5.1 安装docker

安装docker  apk add docker

localhost:~# apk add docker
(1/22) Installing ca-certificates (20240705-r0)
...
(22/22) Installing docker (26.1.5-r0)
Executing busybox-1.36.1-r29.trigger
Executing ca-certificates-20240705-r0.trigger
Executing glib-2.80.2-r0.trigger
OK: 353 MiB in 83 packages
localhost:~#

docker 添加到开机自启 rc-update add docker boot

localhost:~# rc-update add docker boot
 * service docker added to runlevel boot
localhost:~# service docker start
 * Caching service dependencies ...                                                                               [ ok ]
 * /var/log/docker.log: creating file
 * /var/log/docker.log: correcting owner
 * Starting Docker Daemon ...                                                                                     [ ok ]
localhost:~#

然后启动 docker 服务 service docker start

localhost:~# service docker start
 * Caching service dependencies ...                                                                               [ ok ]
 * /var/log/docker.log: creating file
 * /var/log/docker.log: correcting owner
 * Starting Docker Daemon ...                                                                                     [ ok ]
localhost:~#

最后可以运行 docker version,查看程序版本,如下图就是安装成功:

localhost:~# docker version
Client:
 Version:           26.1.5
 API version:       1.45
 Go version:        go1.22.5
 Git commit:        a72d7cdbeb991662bf954bfb8d02274124af21e3
 Built:             Fri Jul 26 17:51:06 2024
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          26.1.5
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.22.5
  Git commit:       411e817ddf710ff8e08fa193da80cb78af708191
  Built:            Fri Jul 26 17:51:06 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.7.17
  GitCommit:        3a4de459a68952ffb703bbe7f2290861a75b6b67
 runc:
  Version:          1.1.12
  GitCommit:        51d5e94601ceffbbd85688df1c928ecccbfa4685
 docker-init:
  Version:          0.19.0
  GitCommit:
localhost:~#

5.2 修改国内源

在 /etc/docker/daemon.json 文件(如果没有该文件则新建)里新增配置:

{
  "registry-mirrors": [
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://dockerhub.azk8s.cn",
    "https://mirror.ccs.tencentyun.com",
    "https://registry.cn-hangzhou.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.m.daocloud.io",   
    "https://noohub.ru", 
    "https://huecker.io",
    "https://dockerhub.timeweb.cloud" 
  ]
}

创建目录 mkdir -p /etc/docker/

建立  /etc/docker/daemon.json 文件 vi /etc/docker/daemon.json

重启docker service docker restart

localhost:~# mkdir -p /etc/docker/
localhost:~# vi /etc/docker/daemon.json
localhost:~# localhost:~# service docker restart
 * WARNING: you are stopping a boot service
 * Stopping Docker Daemon ...                                                                                     [ ok ]
 * Starting Docker Daemon ...                                                                                     [ ok ]
localhost:~#

检查一下是否修改成功:docker info

localhost:~# docker info
Client:
 Version:    26.1.5
 Context:    default
 Debug Mode: false
...
Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://registry.docker-cn.com/
  http://hub-mirror.c.163.com/
  https://dockerhub.azk8s.cn/
  https://mirror.ccs.tencentyun.com/
  https://registry.cn-hangzhou.aliyuncs.com/
  https://docker.mirrors.ustc.edu.cn/
  https://docker.m.daocloud.io/
  https://noohub.ru/
  https://huecker.io/
  https://dockerhub.timeweb.cloud/
 Live Restore Enabled: false

localhost:~#

5.3 安装docker Compose

docker-cli-compose 位于 Alpine Linux 3.15 开始的社区存储库中。

执行apk add docker-cli-compose

localhost:~# apk add docker-cli-compose
fetch https://mirrors.ustc.edu.cn/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.ustc.edu.cn/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
(1/1) Installing docker-cli-compose (2.27.0-r3)
Executing busybox-1.36.1-r29.trigger
OK: 412 MiB in 84 packages
localhost:~#

测试一下是否成功,执行docker-compose -v

localhost:~# docker-compose -v
Docker Compose version v2.27.0
localhost:~#