赞
踩
1.产生原因
从Dockerfile build image 或者 Dockerhub拉取image,或者创建多个container的时候。管理这些container就非常难。所以,compose的作用就是,批处理一个应用的所有Dockerfile。
2.Docker Compose实现功能
(1) 通过一个yml文件定义多容器的docker应用
(2) 也可以通过一条命令就可以根据yml文件的定义去创建或者管理这多个容器
1. Docker-compose.yml由三部分构成:Services、Networks和Volumes。最常用版本是3,可以集群部署;而版本2,只能单机部署。
2. 具体结构
(1)service:启动服务
- services:
- db:
- image: postgres:9.4
- volumes:
- - "db-data:/var/lib/postgresql/data"
- networks:
- - back-tier
-
- 等效
- docker run -d –network back-tier -v db-data: /var/lib/postgresql/data postgres:9.4
(2)volumes:持久化位置
- services:
- db:
- image: postgres:9.4
- volumes:
- - "db-data:/var/lib/postgresql/data"
- networks:
- - back-tier
- volumes:
- mysql-data:
-
- volumes等效
- docker volume create db-data
(3)networks
创建docker网络
- services:
- worker:
- build:./worker
- linkes:
- - db
- - redis
- networks:
- - front-tier
-
- networks:
- front-tier:
- driver: bridge
- front-bridge:
- driver: bridge
其中,worker容器下的网络front-tier是后续通过networks创建。
1.安装
网页:https://docs.docker.com/compose/compose-file/
下载当前稳定版本的docker-compose
- [root@docker-node1 ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 0 0 0 0 0 0 0 0 --:--:-- --:--:-- - 0 0 0 0 0 0 0 0 --:--:-- --:--:-- - 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 -100 617 0 617 0 0 372 0 --:--:-- 0:00:01 --:--:-- 372
- 0 0 0 0 0 0 0 0 --:--:-- 0:00:03 - 0 0 0 0 0 0 0 0 --:--:-- 0:00:03 - 0 15.4M 0 0 0 0 0 0 --:--:-- 0:00:04 - 1 15.4M 1 169k 0 0 31960 0 0:08:25 0:00:05 29 15.4M 29 4657k 0 0 408k 0 0:00:38 0:00:11 0 38 15.4M 38 6067k 0 0 489k 0 0:00:32 0:00:12 47 15.4M 47 7530k 0 0 562k 0 0:00:28 0:00:13 57 15.4M 57 9110k 0 0 633k 0 0:00:24 0:00:14 67 15.4M 67 10.4M 0 0 692k 0 0:00:22 0:00:15 77 15.4M 77 11.9M 0 0 748k 0 0:00:21 0:00:16 88 15.4M 88 13.6M 0 0 804k 0 0:00:19 0:00:17 98 15.4M 98 15.1M 0 0 845k 0 0:00:18 0:00:18 -100 15.4M 100 15.4M 0 0 853k 0 0:00:18 0:00:18 --:--:-- 1624k
2. 赋予目录可执行权限
[root@docker-node1 ~]# sudo chmod +x /usr/local/bin/docker-compose
3. 检查安装
- [root@docker-node1 ~]# docker-compose --version
- docker-compose version 1.24.0, build 0aa59064
4.提示命令
- [root@docker-node1 ~]# docker-compose
- 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
-
- 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
5. 启动
- -》docker-compose -f docker-compose.yml up
- ps: -f指定启动yml文件路径,如果不指定-f,则
-
- -》docker-compose up
- 默认启动:docker-compose.iml文件
1.mysql和wordpress单独部署
(1)拉取wordpress和mysql
- docker pull hub.c.163.com/library/wordpress:latest
-
- docker pull hub.c.163.com/library/mysql:5.7
(2)启动mysql
不需要暴露端口,因为是在宿主机上访问
docker run --name mysql -v /opt/mysql-data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress hub.c.163.com/library/mysql:5.7
(3)启动wordpress
docker run --name some-wordpress --link mysql:mysql -p 8080:80 -d hub.c.163.com/library/wordpress
其中:--link指的将远程名称为mysql的容器,映射到wordpress容器,名称为mysql;然后,映射wordpress的80端口到宿主机的8080端口
(4)注册应用
-》访问:http://192.168.205.10:8080
-》填入必要信息
-》登陆
-》查看信息
2.使用docker-compose部署wordpress
(1)docker-compose.yml内容
- version: '3'
-
- services:
-
- wordpress:
- image: hub.c.163.com/library/wordpress
- ports:
- - 8080:80
- environment:
- WORDPRESS_DB_HOST: mysql
- WORDPRESS_DB_PASSWORD: root
- networks:
- - my-bridge
-
- mysql:
- image: hub.c.163.com/library/mysql:5.7
- environment:
- MYSQL_ROOT_PASSWORD: root
- MYSQL_DATABASE: wordpress
- volumes:
- - mysql-data:/var/lib/mysql
- networks:
- - my-bridge
-
- volumes:
- mysql-data:
-
- networks:
- my-bridge:
- driver: bridge
(2)启动
docker-compose up
(3)访问http://192.168.205.10:8080
注册如同单步一样
1.查看进程
- [vagrant@docker-node1 labs6]$ docker-compose ps
- Name Command State Ports
- ---------------------------------------------------------------------------------
- labs6_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
- labs6_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp
2停止
- [vagrant@docker-node1 labs6]$ docker-compose stop
- Stopping labs6_wordpress_1 ... done
- Stopping labs6_mysql_1 ... done
- [vagrant@docker-node1 labs6]$ docker-compose ps
- Name Command State Ports
- -------------------------------------------------------------------
- labs6_mysql_1 docker-entrypoint.sh mysqld Exit 0
- labs6_wordpress_1 docker-entrypoint.sh apach ... Exit 0
3启动
docker-compose start
4关闭删除down
docker-compose down
等效:stop+rm删除
5.启动,后台启动
docker-compose up -d
ps:如果调试,则不需要启动-d,因为需要查看log
6. 查看images
docker-compose images
7.exec进入容器内部
docker-compose exec mysql bash
或者
docker-compose exec wordpress bash
8.查看network
docker network ls
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。