[SOP]linux上docker和与docker-compose只能分开装
2026/4/8大约 2 分钟
[SOP]linux上docker和与docker-compose只能分开装
为什么在 Linux 上需要分开装?
这主要源于历史和技术实现。在 Windows 和 Mac 上,Docker Desktop 为了方便用户,已经将两者打包在了一起【常见于企业内使用mac的研发出现这种疑惑】
- Docker 和 Docker Compose 可以分开安装,是因为它们本就是两个各司其职的独立工具。
- docker官网:https://docs.docker.com/get-started/
- 阿里云搭建环境:https://help.aliyun.com/zh/ecs/user-guide/install-and-use-docker?spm=a2c4g.11186623.help-menu-25365.d_0_10_4_7.54406740xcBK9J#86c2feaaf85rn
1、步骤1:【使用root】安装docker
下载解压离线包,拷贝命令到可执行文件夹下
wget https://download.docker.com/linux/static/stable/x86_64/docker-24.0.5.tgz
tar -zxvf docker-24.0.5.tgz
cp docker/* /usr/bin/编写docker启动文件
cat > /etc/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[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
ExecReload=/bin/kill -s HUP $MAINPID
# 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
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# 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
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF配置/etc/docker/daemon.json
mkdir -p /etc/docker && mkdir -p /home/docker/data
cat > /etc/docker/daemon.json << EOF
参考的json
EOF启动docker
systemctl daemon-reload
systemctl enable docker
systemctl restart docker验证
# docker info
省略2、步骤2【使用root】安装docker-compose
curl -SL https://github.com/docker/compose/releases/download/v2.32.1/docker-compose-linux-x86_64 -o /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose验证【无论root还是普通用户都可以】
# docker-compose version
Docker Compose version v2.32.1配置命令补全
cat > /etc/bash_completion.d/docker-compose << 'EOF'
#!/usr/bin/env bash
__docker_compose_completion() {
local cur prev words cword
_init_completion || return
local commands=(
"build" "config" "cp" "create" "down" "events" "exec" "images" "kill"
"logs" "pause" "port" "ps" "pull" "push" "restart" "rm" "run"
"start" "stop" "top" "up" "version" "wait"
)
local options=(
"-f" "--file" "-p" "--project-name" "--verbose" "--log-level"
"--no-ansi" "--env-file" "--help" "-v" "--version"
)
if [[ $cword -eq 1 ]]; then
COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur"))
return 0
fi
case "${words[1]}" in
up|down|restart|start|stop|kill|rm|exec|logs)
local services
services=$(_docker_compose_get_services)
COMPREPLY=($(compgen -W "$services ${options[*]}" -- "$cur"))
;;
*)
COMPREPLY=($(compgen -W "${options[*]}" -- "$cur"))
;;
esac
}
_docker_compose_get_services() {
if [[ -f docker-compose.yml || -f docker-compose.yaml ]]; then
awk '/^[ \t]*[a-zA-Z0-9_-]+:[ \t]*$/{gsub(/:$/,"");print}' docker-compose.yml docker-compose.yaml 2>/dev/null | sort -u
fi
}
complete -F __docker_compose_completion docker-compose
EOF