-
能做什么?
一个用来把 docker 自动化的东西。有了 docker-compose 你可以把所有繁复的 docker 操作全都一条命令,自动化的完成。通过创建compose文件(YUML语法),在这个文件上面描述应用的架构,如使用什么镜像、数据卷、网络、绑定服务端口等等,然后再用一条命令就可以管理所有的服务(如启动、停止、重启、日志监控等等)。
Supported filenames: docker-compose.yml, docker-compose.yaml
https://docs.docker.com/compose/overview 官网描述非常详尽,示例步骤清晰易懂,建议详读。
-
怎么做?
sudo curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose 查看版本:docker-compose -version docker-compose version 1.19.0, build 9e633ef 查看帮助:docker-compose -h 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 --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 (for example if your docker host is an IP address) --project-directory PATH Specify an alternate working directory (default: the path of the Compose file) Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file 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
官网的例子,描述非常详细,本处不再复述,详见链接: https://docs.docker.com/compose/gettingstarted
通过发布3个APP(App1,App2,App3),来演示Docker在服务发布、网络、共享分区以及信息隔离与连通方面的工作,详见链接: http://blog.csdn.net/yl_1314/article/details/53761049
-
问题合集
-
Creating network "composetest_default" with the default driver
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-529e7cd3bb6a -j RETURN: iptables: No chain/target/match by that name.
(exit status 1))场景:官网例子,执行 docker-compose up 报出
解决:发现 “/etc/sysconfig/iptables” 不存在,执行“iptables-save > /etc/sysconfig/iptables”, 编辑产生的iptables文件,为*filter 和 *nat 增加 ”:DOCKER - [0:0]“。然后执行 ”sudo systemctl restart iptables.service“。重试解决。
- Failed to restart iptables.service: Unit not found.
场景:执行“sudo systemctl restart iptables.service” 报出
解决:“whereis iptables” 发现没有安装,执行”yum install iptables-services“,安装完成后解决。
-
Creating network "composetest_default" with the default driver
场景:官网例子,执行 docker-compose up 报出
ERROR: unable to insert jump to DOCKER-ISOLATION rule in FORWARD chain: (iptables failed: iptables --wait -I FORWARD -j DOCKER-ISOLATION: iptables v1.4.21: Couldn't load target `DOCKER-ISOLATION':No such file or directory
解决:执行"systemctl restart docker" 。在解决问题1后,未重启docker导致。重启后,官网例子可正常进行。
- bash: ping: command not found。容器内bash没有 ping 工具
场景:例子二执行 ”docker-compose exec App1 bash“,后执行”ping“报出。
解决:执行”whereis ping“ 发现没有安装ping。执行”apt-get update“,”apt install iputils-ping # ping“ 安装后正常。
- 容器启动会,几秒钟自动退出
场景:执行自己写的docker-compose.yml 时发生
解决:容器内进程退出了容器会立马退出,容器内最后一个进程如果在后台运行容器也会立马退出。加上启动的命令就行了, 比如/bin/bash
"NOTICE: PHP message: PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /data1/php_release/xsbn/govzf-web/backend/public/index.php on line 52"
场景:“实战”部署php环境时报出。经检查代码确实没错,逐步排查发现部分依赖框架方法找不到。怀疑是版本问题引起。当前php、php-fpm为5.4,升级至5.6
// 更新版本
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64
yum install php56w-fpm
升级后问题解决。(版本问题真难查啊。)
- [pool php-fpm-004] child 76 said into stderr: "NOTICE: PHP message: PHP Fatal error: Class 'redis' not found in /data1/php_release/xsbn/govzf-web/backend/kernel/Util/Http/RedisDatabase.php on line 68"
场景:“实战”项目php环境启动报错
解决:php -m|grep redis 发现没有安装redis模块。Dockerfile 中加入 RUN yum -y install php56w-pecl-redis.x86_64。
- 其他:日志中各种路径报错、文件权限报错、域名报错等,根据日志提示,一一解决。