之前提到使用docker-maven-plugin将项目发布到docker容器中,在实际操作中,项目周边的依赖环境相对复杂,如数据库,redis,zookeeper,mq等,一种方案是自己将这些周边产品与tomcat作成一个镜像,在项目组内部使用。另一种是各周边产品各自部署到一个docker容器中,通过Docker Compose进行统一管理。个人偏向于第二种方案,虽然初期安装、配置相对复杂一些,但好处是周边产品的版本更换相对方便。比如将mysql的版本从5.6升级到5.7,如果采用第一种方案必将导致重新构建镜像。而第二种方案则只需pull一个5.7的mysql镜像并稍稍修改Docker Compose配置文件即可。
安装Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
命令自动补全
curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
查看版本
docker-compose --version
docker-compose官方文档地址: https://docs.docker.com/compose/
编写docker-compose.yml(文件名只能是这个?)
[samhxm@localhost app]$ mkdir docker-compose
[samhxm@localhost app]$ cd docker-compose/
[samhxm@localhost docker-compose]$ vim docker-compose.yml
mysql:
image: "mysql:5.6.34"
environment: [MYSQL_ROOT_PASSWORD=111111]
volumes:
- /home/samhxm/app/mysql_data:/var/lib/mysql
ports:
- 3306:3306
dev:
image: "sam/spring-boot-dockerimage:latest"
ports:
- 8080:8080
启动容器
#交互模式
[samhxm@localhost docker-compose]$ docker-compose up
#后台模式
[samhxm@localhost docker-compose]$ docker-compose up -d
启动过程
[root@localhost docker-compose]# docker-compose up
Creating dockercompose_dev_1
Creating dockercompose_mysql_1
Attaching to dockercompose_dev_1, dockercompose_mysql_1
mysql_1 | 2016-12-10 16:14:12 0 [Note] mysqld (mysqld 5.6.34) starting as process 1 ...
# 略去日志
mysql_1 | 2016-12-10 16:14:13 1 [Note] InnoDB: Waiting for purge to start
mysql_1 | 2016-12-10 16:14:13 1 [Note] InnoDB: 5.6.34 started; log sequence number 1630066
mysql_1 | 2016-12-10 16:14:13 1 [Note] Server hostname (bind-address): '*'; port: 3306
mysql_1 | 2016-12-10 16:14:13 1 [Note] IPv6 is available.
mysql_1 | 2016-12-10 16:14:13 1 [Note] - '::' resolves to '::';
mysql_1 | 2016-12-10 16:14:13 1 [Note] Server socket created on IP: '::'.
mysql_1 | 2016-12-10 16:14:13 1 [Warning] 'proxies_priv' entry '@ root@d10d642b73b4' ignored in --skip-name-resolve mode.
mysql_1 | 2016-12-10 16:14:13 1 [Note] Event Scheduler: Loaded 0 events
mysql_1 | 2016-12-10 16:14:13 1 [Note] mysqld: ready for connections.
mysql_1 | Version: '5.6.34' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
dev_1 |
dev_1 | . ____ _ __ _ _
dev_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
dev_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
dev_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
dev_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
dev_1 | =========|_|==============|___/=/_/_/_/
dev_1 | :: Spring Boot :: (v1.4.2.RELEASE)
dev_1 |
dev_1 | 2016-12-10 16:14:15.440 INFO 1 --- [ main] c.sam.demo.spring.boot.docker.Appstart : Starting Appstart v0.0.1-SNAPSHOT on ba95fbc4ae46 with PID 1 (/dockerimage.jar started by root in /)
dev_1 | 2016-12-10 16:14:15.443 INFO 1 --- [ main] c.sam.demo.spring.boot.docker.Appstart : No active profile set, falling back to default profiles: default
# 略去日志
dev_1 | 2016-12-10 16:14:35.039 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
dev_1 | 2016-12-10 16:14:35.166 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
dev_1 | 2016-12-10 16:14:35.173 INFO 1 --- [ main] c.sam.demo.spring.boot.docker.Appstart : Started Appstart in 20.662 seconds (JVM running for 23.948)
可以看出,通过docker-compose启动了2个容器,名字分别是dockercompose_dev_1、dockercompose_mysql_1。
我使用的是docker-compose up启动,所以2个容器的日志都会在shell中输出。
其他docker-compose命令
Commands:
build Build or rebuild services
help Get help on a command
kill Kill containers
logs View output from containers
port Print the public port for a port binding
ps List containers
pull Pulls service images
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
restart Restart services
up Create and start containers
docker-compose补充
由于是测试环境,所以没有去设置OS的环境变量,所有的docker-compose命令均由root用户执行。
执行docker-compose命令时需在docker-compose.yml文件所在路径下执行。
每次up容器时,最好kill并rm掉之前的容器(生产环境慎用)以免出现端口冲突等问题。测试环境中可以把这3个命令做成一个sh文件
[root@localhost docker-compose]# docker-compose kill
Killing dockercompose_mysql_1 ... done
Killing dockercompose_dev_1 ... done
[root@localhost docker-compose]# docker-compose rm
Going to remove dockercompose_mysql_1, dockercompose_dev_1
Are you sure? [yN] y
Removing dockercompose_mysql_1 ... done
Removing dockercompose_dev_1 ... done
[root@localhost docker-compose]# docker-compose up
Creating dockercompose_dev_1
Creating dockercompose_mysql_1
Attaching to dockercompose_mysql_1, dockercompose_dev_1