特别声明:该文章出于传递知识而非盈利之目的,同时并不代表赞成其观点或证实其描述,内容仅供参考。在能联网的机器上,从 docker 官网下载二进制包,上传到部署服务器 /data
目录(个人习惯自定义安装软件和数据默认都放在该目录)。下载地址:https://download.docker.com/linux/static/stable/x86_64/解压并复制所有二进制文件到 /usr/local/bin
目录下tar -xvf docker-27.4.0.tgz
mv docker/* /usr/local/bin/
rm -rf docker
# 说明: 个人习惯自定义安装服务的 bin 文件都存放在 /usr/local/bin/ 目录下,实际使用中可以放在任何路径下。
docker-27.4.0.tgz 解压后的文件内容如下:
docker/
docker/docker-proxy
docker/docker-init
docker/containerd-shim-runc-v2
docker/dockerd
docker/containerd
docker/docker
docker/runc
docker/ctr
echo "export PATH=$PATH:/usr/local/bin" >> /etc/profile.d/docker.sh
说明: 个性化的环境变量配置不要放在 /etc/profile 文件中,建议在 /etc/profile.d/ 目录新建一个以服务名命名的配置文件。source /etc/profile
docker version
# 因为,Docker 服务还没启动,执行命令看到的是 Client 的信息)
[root@ubuntu data]# docker version
Client:
Version: 27.4.0
API version: 1.47
Go version: go1.22.10
Git commit: bde2b89
Built: Sat Dec 7 10:37:44 2024
OS/Arch: linux/amd64
Context: default
Cannot connect to the Docker daemon at unix:
编辑 Docker 服务配置文件
为了实现 Docker 服务的个性化配置,我们需要自定义 daemon.json
配置文件。
- 创建配置文件
/etc/docker/daemon.json
cat > /etc/docker/daemon.json <<EOF
{
"data-root": "/data/docker",
"exec-opts": [
"native.cgroupdriver=systemd"
],
"log-level": "info",
"log-opts": {
"max-size": "100m",
"max-file": "5"
},
"storage-driver": "overlay2",
"registry-mirrors": [
"https://0d48af847480f30d0f89c018e6ca00e0.mirror.swr.myhuaweicloud.com",
"https://mirror.ccs.tencentyun.com"
]
}
EOF
说明:data-root 指定 Docker 服务的默认数据目录为 /data/docker,生产环境使用请将独立的数据盘挂载到 /data 目录, registry-mirrors 配置了华为的镜像源。
配置 systemd 管理 Docker 服务
为了在日常使用中方便的管理 Docker 服务,我们编写 systemd 配置文件实现 Docker 服务的管理。
cat > /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
#BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
#Requires=docker.socket
[Service]
Type=notify
ExecStart=/usr/local/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
#Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable docker --now
[root@ubuntu data]# docker info
Client:
Version: 27.4.0
Context: default
Debug Mode: false
Server:
Containers: 7
Running: 7
Paused: 0
Stopped: 0
Images: 7
Server Version: 27.4.0
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 88bf19b2105c8b17560993bee28a01ddc2f97182
runc version: v1.2.2-0-g7cb3632
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: builtin
cgroupns
Kernel Version: 5.15.0-142-generic
Operating System: Ubuntu 22.04.5 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 15.61GiB
Name: lavm-q0xep7hsll
ID: 3705217a-78ad-46ea-836a-d75832c1b650
Docker Root Dir: /data/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://0d48af847480f30d0f89c018e6ca00e0.mirror.swr.myhuaweicloud.com/
https://mirror.ccs.tencentyun.com/
Live Restore Enabled: false
Product License: Community Engine
安装 docker-compose
上传 docker-compose 离线二进制包
在能联网的机器上,从 docker 官网下载二进制包,上传到部署服务器 /data 目录(个人习惯自定义安装软件和数据默认都放在该目录)。
cd /data
cp docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
验证 docker-compose 信息
[root@ubuntu data]# docker-compose version
Docker Compose version v2.32.1
# docker 服务启动失败
# 查看 journalctl 报错日志
[root@ubuntu data]# journalctl -xe
░░ Subject: Process /usr/local/bin/dockerd could not be executed
░░ Defined-By: systemd
░░ Support: https:
░░
░░ The process /usr/local/bin/dockerd could not be executed and failed.
░░
░░ The error number returned by this process is ERRNO.
Apr 21 11:10:44 ubuntu (dockerd)[18962]: docker.service: Failed at step EXEC spawning /usr/local/bin/dockerd: Permission denied
# 查看 messages 日志
[root@ubuntu data]# tail /var/log/messages -n 20
Apr 21 11:10:46 ubuntu systemd[1]: Starting Docker Application Container Engine...
Apr 21 11:10:46 ubuntu (dockerd)[18966]: docker.service: Failed to locate executable /usr/local/bin/dockerd: Permission denied
Apr 21 11:10:46 ubuntu (dockerd)[18966]: docker.service: Failed at step EXEC spawning /usr/local/bin/dockerd: Permission denied
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
本文分享了使用二进制文件离线部署 Docker 和 Docker-Compose 的详细流程及注意事项。主要内容概括如下:
- 使用二进制文件离线部署 Docker-Compose
- 使用 Docker-Compose 创建应用验证测试 Docker 服务