手摸手教学 - Docker(七) 旋转跳跃!Docker Compose管理多服务!

终于到了docker最爽的一节了,掌握它,我 升空 旋转 跳跃 起飞!z啊!
最近在国外社区中了解到,一些output信息或者啥的尽量都不要用截图,因为这样更容易简历搜索索引,也就是更容易让问题者找到问题
所以以后能用markdown就不用screenshot~

预计阅读时间:11min

作者变优秀的小白

Github关注YX-XiaoBai

爱好Americano More Ice !

QQ学习交流群(new): 811792998

目录

  • 使用Docker Compose
    • 下载Docker Compose
    • 创建我们的Compose文件
    • 定义app服务
      • 定义MYSQL服务
    • 运行我们应用栈
    • 在docker dashboard中查看我们应用堆栈
    • 将启动的容器tear down
  • 总结

使用Docker Compose

Docker Compose一个有助于定义和共享应用程序的工具,使用compose我们可创建一个yaml文件定义服务、可使用一个命令将所有东西启动关闭

  • compose最大的优点:
    • 可以在文件定义你的应用栈,将它放在你项目仓库的根目录下(现在是版本控制)
    • 可以很容易让别人为你项目作出贡献,别人仅需克隆你的仓库和启动compose应用

下载Docker Compose

  • 官方下载地址
# 完成下载,检查版本
$ docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.1g  21 Apr 2020

创建我们的Compose文件

  1. 在应用的项目的根目录中,创建一个文件名称为docker-compose.yml
  2. compose文件中,我们首先需要定义版本。在大多数情况下,推荐使用最新的支持版本。您可以查看有关当前架构版本和兼容性列表的Compose file reference

version: "3.7"

  1. 然后,我们定义一系列的服务(或者容器),用于我们应用的一部分
version: "3.7"

services:

接下来我们开始一次迁移服务到compose文件

定义app服务

首先,展示下我们定义应用的容器命令

docker run -dp 3000:3000 \
  -w /app -v "$(pwd):/app" \
  --network todo-app \
  -e MYSQL_HOST=mysql \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=todos \
  node:12-alpine \
  sh -c "yarn install && yarn run dev"
  1. 首先,我们定义服务的目录(entry)和容器的镜像(image)。我们起使用任何名称给我们的容器,这个名称会自动变成一个网络别名,当我们定义我们的mysql这个会非常有用
version: "3.7"

services:
  app:
    image: node:12-alpine
  1. 通常上你会看见command靠近与image定义,尽管顺序是不被要求的。
version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
  1. 我们通过定义服务的端口迁移-p 3000:3000。我们此处使用短参数(short syntax),但是也可以使用长参数(long syntax)
version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
  1. 然后,我们通过使用working_dirvolumes定义迁移工作文件夹(-w /app)和volume映射(-v "$(pwd):/app")
version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
  1. 最后,我们需要通过使用environment值迁移环境变量定义
version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos
定义MYSQL服务

首先,一样的,我们来看下定义mysql容器的命令

docker run -d \
  --network todo-app --network-alias mysql \
  -v todo-mysql-data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  -e MYSQL_DATABASE=todos \
  mysql:5.7
  1. 我们先定义一个新的服务(service)命名为mysql,它可以自动获取网络别名
version: "3.7"

services:
  app:
    # The app service definition
  mysql:
    image: mysql:5.7
  1. 然后,我们定义volume映射,当我们运行容器(docker run)时,被命名的volumne会自动创建。然而,使用compose时前者是不会发生的。我们需要在volumes:顶部定义volume并且在服务配置指定安装点。通过简单提供唯一的volume名称,默认选项即可被使用。
version: "3.7"

services:
  app:
    # The app service definition
  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
volumes:
  todo-mysql-data:

3.最后,指定环境变量

version: "3.7"

services:
  app:
    # The app service definition
  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos
volumes:
  todo-mysql-data:

最后应该长这样

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

运行我们应用栈

我们已经完成了docker-compose.yml文件,启动TA~

  1. 确保没有app或者db在运行(docker ps并且docker rm -f)

  2. 运行我们的应用

docker-compose up -d

$ docker-compose up -d
Creating network "app_default" with the default driver
Creating volume "app_todo-mysql-data" with default driver
Creating app_mysql_1 ... done
Creating app_app_1   ... done

你应该注意到volume在创建的同时也创建了一个networkdocker compose默认情况下会自动创建一个网络转为application stack(这也是我们为什么没有在compose文件定义)

  1. 让我们使用docker-compose logs -f查看logs
# 第一启动app失败了,mysql已经起来了
Attaching to app_app_1, app_mysql_1
app_1    | yarn install v1.22.5
app_1    | [1/4] Resolving packages...
app_1    | [2/4] Fetching packages...
# 自动重连n次
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
app_1    | error An unexpected error occurred: "https://registry.yarnpkg.com/define-property/-/define-property-1.0.0.tgz: Request failed \"502 Bad Gateway\"".
app_1    | info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
mysql_1  | 2021-02-02 08:08:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.32-1debian10 started.
mysql_1  | 2021-02-02 08:08:50+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1  | 2021-02-02 08:08:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.32-1debian10 started.
mysql_1  | 2021-02-02 08:08:50+00:00 [Note] [Entrypoint]: Initializing database files
...
...
# 上面省略bala bala
# 所以重试了,可以看到mysql已经准备好了
mysql_1  | 2021-02-02T08:09:02.120842Z 0 [Note] mysqld: ready for connections.
mysql_1  | Version: '5.7.32'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
app_1    | [4/4] Building fresh packages...
app_1    | Done in 80.29s.
# 到此已经两个app和mysql都启动完成了
app_1    | yarn run v1.22.5
app_1    | $ nodemon src/index.js
app_1    | [nodemon] 1.19.2
app_1    | [nodemon] to restart at any time, enter `rs`
app_1    | [nodemon] watching dir(s): *.*
app_1    | [nodemon] starting `node src/index.js`
mysql_1  | 2021-02-02T08:19:33.047122Z 2 [Note] Got an error reading communication packets
# 等待检查需连接的mysql是否正常
app_1    | Waiting for mysql:3306.
# 连接成功
app_1    | Connected!
app_1    | Connected to mysql db at host mysql
app_1    | Listening on port 3000

虽然上面服务名称在开头已经区分消息,我们也可以查看特定服务的日志

docker-compose logs -f app

Attaching to app_app_1
app_1    | yarn install v1.22.5
app_1    | [1/4] Resolving packages...
app_1    | [2/4] Fetching packages...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
app_1    | error An unexpected error occurred: "https://registry.yarnpkg.com/define-property/-/define-property-1.0.0.tgz: Request failed \"502 Bad Gateway\"".
app_1    | info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | info There appears to be trouble with your network connection. Retrying...
app_1    | yarn install v1.22.5
# 下面是第二次重试日志
app_1    | [1/4] Resolving packages...
app_1    | [2/4] Fetching packages...
app_1    | info [email protected]: The platform "linux" is incompatible with this module.
app_1    | info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
app_1    | [3/4] Linking dependencies...
app_1    | [4/4] Building fresh packages...
app_1    | Done in 80.29s.
app_1    | yarn run v1.22.5
app_1    | $ nodemon src/index.js
app_1    | [nodemon] 1.19.2
app_1    | [nodemon] to restart at any time, enter `rs`
app_1    | [nodemon] watching dir(s): *.*
app_1    | [nodemon] starting `node src/index.js`
app_1    | Waiting for mysql:3306.
app_1    | Connected!
app_1    | Connected to mysql db at host mysql
app_1    | Listening on port 3000

是不是很方便,一个命令就搞定了!

在docker dashboard中查看我们应用堆栈

打开dashboard我们可以看到app的组,这是Docker Compose项目名称,将容器分组在一起,这样我查看哪个容器使我们的应用mysql非常Easy。

手摸手教学 - Docker(七) 旋转跳跃!Docker Compose管理多服务!_第1张图片

将启动的容器tear down

有两种方式:

  • 直接在Dashboard点击垃圾桶 --> 容器停止 & 网络删除
  • 当然你可以使用docker-compose down但这不会删除volume,若要删除要添加--volumes

总结

本文中,我们了解了Docker Compose如何帮助我们极大的简化了多服务应用程序的定义和共享,我们使用将命令转换为文件去管理容器

结束语:不懂算我输,当然~如果遇到什么疑问或者建议的,可直接留言评论!作者看到会马上一一回复!
如果觉得小白此文章不错或对你有所帮助,期待你的一键三连!❤️ni!

你可能感兴趣的:(运维DevOps,docker,linux,运维,devops,docker-compose)