Docker-Compose是一个部署多个容器的简单但是非常必要的工具.安装Docker-Compose之前,请先安装 python-pip
一、安装 python-pip
[root@hsp1 ~]# pip -V
-bash: pip: 未找到命令
[root@hsp1 ~]# yum install -y epel-release
[root@hsp1 ~]# yum install -y python-pip
[root@hsp1 ~]# pip install --upgrade pip
[root@hsp1 ~]# pip -V
pip 20.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
二、安装Docker Compose
[root@hsp1 ~]# pip install docker-compose
---------------------------------------------------------------------------------------
如果执行中报错:
ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out
解决:
[root@hsp1 ~]# pip --default-timeout=200 install -U docker-compose
--------------------------------------------------------------------------------------
如果执行中报错:
Setup script exited with error: command 'gcc' failed with exit status 1异常
这是没有正确安装Python开发环境导致,解决如下:
[root@hsp1 ~]# yum install -y python-devel libevent-devel
[root@hsp1 ~]# pip install gevent
[root@hsp1 ~]# yum install groupinstall 'development tools'
--------------------------------------------------------------------------------------
再次执行:
[root@hsp1 ~]# pip install docker-compose
Successfully installed docker-compose-1.25.5 subprocess32-3.5.4 texttable-1.6.2
检验docker-compose是否安装成功
[root@hsp1 ~]# docker-compose --version
docker-compose version 1.25.5, build unknown
--------------------------------------------------------------------------------------
如果报错:
pkg_resources.DistributionNotFound: backports.ssl-match-hostname>=3.5
解决方法:更新backports.ssl-match-hostname的版本,在终端输入命令
[root@hsp1 ~]# pip install --upgrade backports.ssl_match_hostname
Docker-compose常用命令
[root@hsp1 ~]# docker-compose --help
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)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent
--env-file PATH Specify an alternate environment file
Commands:
build Build or rebuild services
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push 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
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
==================================================================================
docker-compose up -d nginx 构建启动nignx容器
docker-compose exec nginx bash 登录到nginx容器中
docker-compose down 删除所有nginx容器,镜像
docker-compose ps 显示所有容器
docker-compose restart nginx 重新启动nginx容器
docker-compose run --no-deps --rm php-fpm php -v 在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器
docker-compose build nginx 构建镜像
docker-compose build --no-cache nginx 不带缓存的构建。
docker-compose logs nginx 查看nginx的日志
docker-compose logs -f nginx 查看nginx的实时日志
==================================================================================
docker-compose config -q 验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。
docker-compose events --json nginx 以json的形式输出nginx的docker日志
docker-compose pause nginx 暂停nignx容器
docker-compose unpause nginx 恢复ningx容器
docker-compose rm nginx 删除容器(删除前必须关闭容器)
docker-compose stop nginx 停止nignx容器
docker-compose start nginx 启动nignx容器