mirror of
https://github.com/yeasy/docker_practice.git
synced 2024-11-01 04:03:39 +00:00
164 lines
5.3 KiB
Markdown
164 lines
5.3 KiB
Markdown
## 安装与卸载
|
||
|
||
Compose 目前支持 Linux 和 Mac OS 平台,两者的安装过程大同小异。
|
||
|
||
安装 Compose 之前,要先安装 Docker(需要 Docker Engine 1.7.1+),请参考第一部分中章节,在此不再赘述。
|
||
|
||
Compose 可以通过 Python 的 pip 工具进行安装,可以直接下载编译好的二进制文件使用,甚至直接运行在 Docker 容器中。
|
||
|
||
前两种方式是传统方式,适合本地环境下安装使用;最后一种方式则不破坏系统环境,更适合云计算场景。
|
||
|
||
### PIP 安装
|
||
这种方式是将 Compose 当作一个 Python 应用来从 pip 源中安装。
|
||
|
||
执行安装命令:
|
||
|
||
```sh
|
||
$ sudo pip install -U docker-compose
|
||
```
|
||
|
||
可以看到类似如下输出,说明安装成功。
|
||
```sh
|
||
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
|
||
```
|
||
|
||
安装成功后,可以查看 `docker-compose` 命令的用法。
|
||
```sh
|
||
$ docker-compose -h
|
||
Define and run multi-container applications with Docker.
|
||
|
||
Usage:
|
||
docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
|
||
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)
|
||
--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
|
||
|
||
Commands:
|
||
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
|
||
```
|
||
|
||
之后,可以添加 bash 补全命令。
|
||
|
||
```sh
|
||
$ curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
|
||
```
|
||
|
||
### 二进制包
|
||
官方定义编译好二进制包,供大家使用。这些发布的二进制包可以在 [https://github.com/docker/compose/releases](https://github.com/docker/compose/releases) 页面找到。
|
||
|
||
这些二进制文件,下载后直接放到执行路径下,并添加执行权限即可。
|
||
|
||
例如,在 Linux 平台上。
|
||
|
||
```
|
||
$ sudo curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
|
||
$ sudo chmod a+x /usr/local/bin/docker-compose
|
||
```
|
||
|
||
可以使用 `docker-compose version` 命令来查看版本信息,以测试是否安装成功。
|
||
|
||
```sh
|
||
$ 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 应用,自然也可以直接用容器来执行它。
|
||
|
||
```sh
|
||
$ 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` 脚本内容,如下
|
||
|
||
```sh
|
||
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` 镜像并运行。
|
||
|
||
### 卸载
|
||
如果是二进制包方式安装的,删除二进制文件即可。
|
||
|
||
```sh
|
||
$ sudo rm /usr/local/bin/docker-compose
|
||
```
|
||
|
||
如果是通过 python pip 工具安装的,则可以执行如下命令删除。
|
||
|
||
```sh
|
||
$ sudo pip uninstall docker-compose
|
||
``` |