docker_practice/compose/install.md

163 lines
5.2 KiB
Markdown
Raw Normal View History

2016-12-20 07:35:52 +00:00
## 安装与卸载
2015-05-08 12:10:01 +00:00
2017-10-31 16:20:30 +00:00
Compose 目前支持 Linux、macOS、Windows 10 三大平台。
2016-12-20 07:35:52 +00:00
2017-10-31 16:20:30 +00:00
Compose 可以通过 Python 的包管理工具 pip 进行安装,也可以直接下载编译好的二进制文件使用,甚至直接运行在 Docker 容器中。
2016-12-20 07:35:52 +00:00
前两种方式是传统方式,适合本地环境下安装使用;最后一种方式则不破坏系统环境,更适合云计算场景。
2015-05-08 12:10:01 +00:00
### PIP 安装
2016-12-20 07:35:52 +00:00
这种方式是将 Compose 当作一个 Python 应用来从 pip 源中安装。
执行安装命令:
2015-05-08 12:10:01 +00:00
2017-11-22 03:13:23 +00:00
```bash
2015-05-08 12:10:01 +00:00
$ sudo pip install -U docker-compose
```
2016-12-20 07:35:52 +00:00
可以看到类似如下输出,说明安装成功。
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
Collecting docker-compose
Downloading docker-compose-1.8.0.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
```
2015-05-08 12:10:01 +00:00
安装成功后,可以查看 `docker-compose` 命令的用法。
2017-11-22 03:13:23 +00:00
```bash
2015-05-08 12:10:01 +00:00
$ docker-compose -h
2016-12-20 07:35:52 +00:00
Define and run multi-container applications with Docker.
2015-05-08 12:10:01 +00:00
Usage:
2016-12-20 07:35:52 +00:00
docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
2015-05-08 12:10:01 +00:00
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
2016-12-20 07:35:52 +00:00
--x-networking (EXPERIMENTAL) Use new Docker networking functionality.
Requires Docker 1.9 or later.
--x-network-driver DRIVER (EXPERIMENTAL) Specify a network driver (default: "bridge").
Requires Docker 1.9 or later.
--verbose Show more output
-v, --version Print version and exit
2015-05-08 12:10:01 +00:00
Commands:
2016-12-20 07:35:52 +00:00
build Build or rebuild services
help Get help on a command
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pulls service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
unpause Unpause services
up Create and start containers
migrate-to-labels Recreate containers to add labels
version Show the Docker-Compose version information
2015-05-08 12:10:01 +00:00
```
之后,可以添加 bash 补全命令。
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
2015-05-08 12:10:01 +00:00
```
### 二进制包
2016-12-20 07:35:52 +00:00
官方定义编译好二进制包,供大家使用。这些发布的二进制包可以在 [https://github.com/docker/compose/releases](https://github.com/docker/compose/releases) 页面找到。
2015-05-08 12:10:01 +00:00
2016-12-20 07:35:52 +00:00
这些二进制文件,下载后直接放到执行路径下,并添加执行权限即可。
2015-05-08 12:10:01 +00:00
2017-10-31 16:20:30 +00:00
例如,在 Linux 和 macOS 平台上。
2015-05-08 12:10:01 +00:00
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ sudo curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2015-05-08 12:10:01 +00:00
$ sudo chmod a+x /usr/local/bin/docker-compose
```
2016-12-20 07:35:52 +00:00
可以使用 `docker-compose version` 命令来查看版本信息,以测试是否安装成功。
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ docker-compose version
docker-compose version 1.8.0, build 94f7016
docker-py version: 1.9.0
CPython version: 2.7.6
OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
```
### 容器中执行
Compose 既然是一个 Python 应用,自然也可以直接用容器来执行它。
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ 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` 脚本内容,如下
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
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` 镜像并运行。
### 卸载
如果是二进制包方式安装的,删除二进制文件即可。
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ sudo rm /usr/local/bin/docker-compose
```
如果是通过 python pip 工具安装的,则可以执行如下命令删除。
2015-05-08 12:10:01 +00:00
2017-11-22 03:13:23 +00:00
```bash
2016-12-20 07:35:52 +00:00
$ sudo pip uninstall docker-compose
2017-10-31 16:20:30 +00:00
```