更新时间:2023-01-08 10:34:30浏览次数:498+次
如何在Ubuntu 22.04/20.04/18.04、Debian 11/10、CentOS 8、Fedora 37/36/35/34/33/32上安装Docker Compose?本文将向您展示如何在Linux系统上安装最新Docker Compose。其实这篇文章旨在为寻求在Linux上安装Docker Compose的开发人员和SysAdmins提供一个简明的分步指导。我们将检查项目的GithubAPI发布页面,并提取最新的二进制文件。
如何在Linux上安装Docker Compose
您需要在系统上安装curl和wget才能执行此操作。当然,作为具有sudo权限的用户访问终端:
1、CentOS/RHEL
sudo yum -y install curl wget
2、Debian/Ubuntu
sudo apt update
sudo apt install -y curl wget
3、Fedora
sudo dnf -y install curl wget
安装curl后,在Linux机器上下载最新的Compose(Compose是一个用于定义和运行多容器Docker应用程序的工具):
curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
使二进制文件可执行:
chmod +x docker-compose-linux-x86_64
将文件移动到PATH:
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
确认版本:
$ docker-compose version
Docker Compose version v2.14.2
将用户添加到docker组:
sudo usermod -aG docker $USER
newgrp docker
配置Compose命令行完成
Compose具有bash和zsh shell的命令完成功能。
1、对于Bash用户
将完成脚本放在/etc/bash_completion.d/中:
sudo curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
源文件或重新登录以享受完成功能:
source /etc/bash_completion.d/docker-compose
2、对于Zsh用户
在~/.zsh/completion/中下载完成脚本:
mkdir -p ~/.zsh/completion
curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > ~/.zsh/completion/_docker-compose
通过添加~/.zshrc将目录包含在$fpath中:
fpath=(~/.zsh/completion $fpath)
确保compinit已加载,或者通过添加~/.zshrc来加载:
autoload -Uz compinit && compinit -i
然后重新加载shell:
exec $SHELL -l
测试Docker Compose安装
创建一个测试Docker Compose文件:
vim docker-compose.yml
将以下数据添加到文件中:
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
links:
- php
php:
image: php:7-fpm
启动服务容器:
$ docker-compose up -d
Creating network "root_default" with the default driver
Pulling php (php:7-fpm)...
7-fpm: Pulling from library/php
b4d181a07f80: Pull complete
78b85dd8f014: Pull complete
8589b26a90be: Pull complete
f5af5d641946: Pull complete
4611dfe4969e: Pull complete
8d335174dcfe: Pull complete
e4ac2aba3855: Pull complete
1ec2992b064f: Pull complete
d0702e432261: Pull complete
508ceaa40a86: Pull complete
Digest: sha256:c5132fe8a019128a89bcc157f5e2b8544ea3078fb9aba076bfe27486f34b0fb5
Status: Downloaded newer image for php:7-fpm
Pulling web (nginx:latest)...
latest: Pulling from library/nginx
b4d181a07f80: Already exists
edb81c9bc1f5: Pull complete
b21fed559b9f: Pull complete
03e6a2452751: Pull complete
b82f7f888feb: Pull complete
5430e98eba64: Pull complete
Digest: sha256:47ae43cdfc7064d28800bc42e79a429540c7c80168e8c8952778c0d5af1c09db
Status: Downloaded newer image for nginx:latest
Creating root_php_1 ... done
Creating root_web_1 ... done
显示正在运行的容器:
$ docker-compose ps
销毁容器:
$ docker-compose stop
Stopping root_web_1 ... done
Stopping root_php_1 ... done
$ docker-compose rm -f
Going to remove root_web_1, root_php_1
Removing root_web_1 ... done
Removing root_php_1 ... done
如需要更多的资源,可以阅读Docker官方文档(https://docs.docker.com/)和Docker Compose文档(https://docs.docker.com/compose/)以了解更多信息。
相关资讯