终于到了
docker
最爽的一节了,掌握它,我 升空 旋转 跳跃 起飞!z
啊!
最近在国外社区中了解到,一些output
信息或者啥的尽量都不要用截图,因为这样更容易简历搜索索引,也就是更容易让问题者找到问题
所以以后能用markdown
就不用screenshot
~
预计阅读时间:11min
作者:变优秀的小白
Github:关注YX-XiaoBai
爱好:Americano More Ice !
QQ学习交流群(new): 811792998
Docker Compose
一个有助于定义和共享应用程序的工具,使用compose
我们可创建一个yaml
文件定义服务、可使用一个命令将所有东西启动
和关闭
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
docker-compose.yml
compose
文件中,我们首先需要定义版本。在大多数情况下,推荐使用最新的支持版本。您可以查看有关当前架构版本和兼容性列表的Compose file referenceversion: "3.7"
version: "3.7"
services:
接下来我们开始一次迁移服务到compose
文件
首先,展示下我们定义应用的容器命令
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"
entry
)和容器的镜像(image
)。我们起使用任何名称给我们的容器,这个名称会自动变成一个网络别名,当我们定义我们的mysql
这个会非常有用version: "3.7"
services:
app:
image: node:12-alpine
command
靠近与image
定义,尽管顺序是不被要求的。version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
-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
working_dir
和volumes
定义迁移工作文件夹(-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
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
容器的命令
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
service
)命名为mysql
,它可以自动获取网络别名version: "3.7"
services:
app:
# The app service definition
mysql:
image: mysql:5.7
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~
确保没有app
或者db
在运行(docker ps
并且docker rm -f
)
运行我们的应用
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
在创建的同时也创建了一个network
。docker compose
默认情况下会自动创建一个网络转为application stack
(这也是我们为什么没有在compose
文件定义)
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
是不是很方便,一个命令就搞定了!
打开dashboard
我们可以看到app
的组,这是Docker Compose
项目名称,将容器分组在一起,这样我查看哪个容器使我们的应用
和mysql
非常Easy。
有两种方式:
Dashboard
点击垃圾桶 --> 容器停止 & 网络删除docker-compose down
但这不会删除volume
,若要删除要添加--volumes
本文中,我们了解了Docker Compose
如何帮助我们极大的简化了多服务应用程序的定义和共享,我们使用将命令转换为文件去管理容器