docker_practice/compose/install.md
康怀帅 e1db77a27a
Update compose install (#267)
* Update drone

* Update compose install
2017-11-30 08:38:35 -06:00

127 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 安装与卸载
`Compose` 支持 LinuxmacOSWindows 10 三大平台
`Compose` 可以通过 Python 的包管理工具 `pip` 进行安装也可以直接下载编译好的二进制文件使用甚至能够直接在 Docker 容器中运行
前两种方式是传统方式适合本地环境下安装使用最后一种方式则不破坏系统环境更适合云计算场景
`Docker for Mac` `Docker for Windows` 自带 `docker-compose` 二进制文件安装 Docker 之后可以直接使用
```bash
$ docker-compose --version
docker-compose version 1.17.1, build 6d101fb
```
Linux 系统请使用以下介绍的方法安装
### 二进制包
Linux 上的也安装十分简单 [官方 GitHub Release](https://github.com/docker/compose/releases) 处直接下载编译好的二进制文件即可。
例如 Linux 64 位系统上直接下载对应的二进制包
```bash
$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
```
### PIP 安装
** `x86_64` 架构的 Linux 建议按照上边的方法下载二进制包进行安装如果您计算机的架构是 `ARM` (例如树莓派)再使用 `pip` 安装
这种方式是将 Compose 当作一个 Python 应用来从 pip 源中安装
执行安装命令
```bash
$ sudo pip install -U docker-compose
```
可以看到类似如下输出说明安装成功
```bash
Collecting docker-compose
Downloading docker-compose-1.17.1.tar.gz (149kB): 149kB downloaded
...
Successfully installed docker-compose cached-property requests texttable websocket-client docker-py dockerpty six enum34 backports.ssl-match-hostname ipaddress
```
### bash 补全命令
```bash
$ curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
```
### 容器中执行
Compose 既然是一个 Python 应用自然也可以直接用容器来执行它
```bash
$ curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
```
实际上查看下载的 `run.sh` 脚本内容如下
```bash
set -e
VERSION="1.8.0"
IMAGE="docker/compose:$VERSION"
# Setup options for connecting to docker host
if [ -z "$DOCKER_HOST" ]; then
DOCKER_HOST="/var/run/docker.sock"
fi
if [ -S "$DOCKER_HOST" ]; then
DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -e DOCKER_HOST"
else
DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
fi
# Setup volume mounts for compose config and context
if [ "$(pwd)" != '/' ]; then
VOLUMES="-v $(pwd):$(pwd)"
fi
if [ -n "$COMPOSE_FILE" ]; then
compose_dir=$(dirname $COMPOSE_FILE)
fi
# TODO: also check --file argument
if [ -n "$compose_dir" ]; then
VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
fi
if [ -n "$HOME" ]; then
VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
fi
# Only allocate tty if we detect one
if [ -t 1 ]; then
DOCKER_RUN_OPTIONS="-t"
fi
if [ -t 0 ]; then
DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
fi
exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"
```
可以看到它其实是下载了 `docker/compose` 镜像并运行
### 卸载
如果是二进制包方式安装的删除二进制文件即可
```bash
$ sudo rm /usr/local/bin/docker-compose
```
如果是通过 `pip` 安装的则执行如下命令即可删除
```bash
$ sudo pip uninstall docker-compose
```