当前位置:   article > 正文

docker(十四):docker-compose安装以及实例_doker compose 1.24.0 示例

doker compose 1.24.0 示例

一、实现功能

1.产生原因

从Dockerfile build image 或者 Dockerhub拉取image,或者创建多个container的时候。管理这些container就非常难。所以,compose的作用就是,批处理一个应用的所有Dockerfile。

2.Docker Compose实现功能

(1)    通过一个yml文件定义多容器的docker应用
(2)    也可以通过一条命令就可以根据yml文件的定义去创建或者管理这多个容器

二、Docker-compose.yml构成

1.    Docker-compose.yml由三部分构成:Services、Networks和Volumes。最常用版本是3,可以集群部署;而版本2,只能单机部署。
2.    具体结构

(1)service:启动服务

  1. services:
  2.   db:
  3.     image: postgres:9.4
  4.     volumes:
  5.       - "db-data:/var/lib/postgresql/data"
  6.     networks:
  7.       - back-tier
  8. 等效
  9. docker run -d –network back-tier -v db-data: /var/lib/postgresql/data postgres:9.4

(2)volumes:持久化位置

  1. services:
  2.   db:
  3.     image: postgres:9.4
  4.     volumes:
  5.       - "db-data:/var/lib/postgresql/data"
  6.     networks:
  7.       - back-tier
  8. volumes:
  9.   mysql-data:
  10. volumes等效
  11. docker volume create db-data

(3)networks
创建docker网络

  1. services:
  2.   worker:
  3.     build:./worker
  4.     linkes:
  5.       - db
  6.       - redis
  7.     networks:
  8.       - front-tier
  9. networks:
  10.   front-tier:
  11.     driver: bridge
  12.   front-bridge:
  13.     driver: bridge

其中,worker容器下的网络front-tier是后续通过networks创建。

三、Docker compose安装和基本使用

1.安装
网页:https://docs.docker.com/compose/compose-file/

下载当前稳定版本的docker-compose

  1. [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
  2.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  3.                                  Dload  Upload   Total   Spent    Left  Speed
  4.   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
  5.   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.    检查安装

  1.  [root@docker-node1 ~]# docker-compose --version
  2. docker-compose version 1.24.0, build 0aa59064

4.提示命令

  1. [root@docker-node1 ~]# docker-compose
  2. Define and run multi-container applications with Docker.
  3. Usage:
  4.   docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  5.   docker-compose -h|--help
  6. Options:
  7.   -f, --file FILE             Specify an alternate compose file
  8.                               (default: docker-compose.yml)
  9.   -p, --project-name NAME     Specify an alternate project name
  10.                               (default: directory name)
  11.   --verbose                   Show more output
  12.   --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  13.   --no-ansi                   Do not print ANSI control characters
  14.   -v, --version               Print version and exit
  15.   -H, --host HOST             Daemon socket to connect to
  16.   --tls                       Use TLS; implied by --tlsverify
  17.   --tlscacert CA_PATH         Trust certs signed only by this CA
  18.   --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  19.   --tlskey TLS_KEY_PATH       Path to TLS key file
  20.   --tlsverify                 Use TLS and verify the remote
  21.   --skip-hostname-check       Don't check the daemon's hostname against the
  22.                               name specified in the client certificate
  23.   --project-directory PATH    Specify an alternate working directory
  24.                               (default: the path of the Compose file)
  25.   --compatibility             If set, Compose will attempt to convert keys
  26.                               in v3 files to their non-Swarm equivalent
  27. Commands:
  28.   build              Build or rebuild services
  29.   bundle             Generate a Docker bundle from the Compose file
  30.   config             Validate and view the Compose file
  31.   create             Create services
  32.   down               Stop and remove containers, networks, images, and volumes
  33.   events             Receive real time events from containers
  34.   exec               Execute a command in a running container
  35.   help               Get help on a command
  36.   images             List images
  37.   kill               Kill containers
  38.   logs               View output from containers
  39.   pause              Pause services
  40.   port               Print the public port for a port binding
  41.   ps                 List containers
  42.   pull               Pull service images
  43.   push               Push service images
  44.   restart            Restart services
  45.   rm                 Remove stopped containers
  46.   run                Run a one-off command
  47.   scale              Set number of containers for a service
  48.   start              Start services
  49.   stop               Stop services
  50.   top                Display the running processes
  51.   unpause            Unpause services
  52.   up                 Create and start containers
  53.   version            Show the Docker-Compose version information

5.    启动

  1. -》docker-compose -f docker-compose.yml up
  2. ps: -f指定启动yml文件路径,如果不指定-f,则
  3. -》docker-compose up
  4. 默认启动:docker-compose.iml文件

四、部署wordpress实例

1.mysql和wordpress单独部署

(1)拉取wordpress和mysql

  1. docker pull hub.c.163.com/library/wordpress:latest
  2. 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内容

  1. version: '3'
  2. services:
  3. wordpress:
  4. image: hub.c.163.com/library/wordpress
  5. ports:
  6. - 8080:80
  7. environment:
  8. WORDPRESS_DB_HOST: mysql
  9. WORDPRESS_DB_PASSWORD: root
  10. networks:
  11. - my-bridge
  12. mysql:
  13. image: hub.c.163.com/library/mysql:5.7
  14. environment:
  15. MYSQL_ROOT_PASSWORD: root
  16. MYSQL_DATABASE: wordpress
  17. volumes:
  18. - mysql-data:/var/lib/mysql
  19. networks:
  20. - my-bridge
  21. volumes:
  22. mysql-data:
  23. networks:
  24. my-bridge:
  25. driver: bridge

(2)启动
docker-compose up

(3)访问http://192.168.205.10:8080

注册如同单步一样


五、常用命令

1.查看进程

  1. [vagrant@docker-node1 labs6]$ docker-compose ps
  2. Name Command State Ports
  3. ---------------------------------------------------------------------------------
  4. labs6_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
  5. labs6_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp

2停止

  1. [vagrant@docker-node1 labs6]$ docker-compose stop
  2. Stopping labs6_wordpress_1 ... done
  3. Stopping labs6_mysql_1 ... done
  4. [vagrant@docker-node1 labs6]$ docker-compose ps
  5. Name Command State Ports
  6. -------------------------------------------------------------------
  7. labs6_mysql_1 docker-entrypoint.sh mysqld Exit 0
  8. 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

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/74282
推荐阅读
相关标签
  

闽ICP备14008679号