【Docker】关于docker-compose顺序问题

docker-compose.yml文件

version: "3"

services:

mysql:

build: ./services/mysql

container_name: mysql

ports:

- ${MYSQL_PORT}:3306

volumes:

- ${MYSQL_DATA_PATH}:/var/lib/mysql:rw

- ./services/mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro

environment:

MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

MYSQL_USER: ${MYSQL_USER}

MYSQL_PASSWORD: ${MYSQL_PASSWORD}

MYSQL_DATABASE: ${MYSQL_DATABASE}

nodejs:

build: ./services/nodejs

container_name: nodejs

ports:

- ${NODE_PORT}:3000

links:

- mysql

depends_on:

- mysql

volumes:

- ./api:/var/www/html:rw

nodejs的dockerfile

FROM daocloud.io/library/node:11.6.0-alpine

WORKDIR /var/www/html

RUN npm config set unsafe-perm true

RUN npm config set registry https://registry.npm.taobao.org/

RUN npm install

RUN npm install -g pm2

CMD ./node_modules/.bin/sequelize db:migrate && npm start && pm2 list && pm2 logs

mysql的dockerfile文件

FROM mysql:8.0

现在问题是,mysql运行后接着nodejs也运行,去到./node_modules/.bin/sequelize db:migrate数据迁移这一步时会报错,因为mysql启动了,但是初始化数据库还没有完成,所以连接不成功。

想问问各位大牛,有什么比较好的解决办法呢?

回答

depends_on 只能决定启动顺序,不能决定就绪顺序(容器启动到程序就绪有个时间窗口)
如果只用 compose 这样简单的编排工具,需要自己去做就绪检查。
另外,不建议将 mysql 等数据持久化层和应用放在一起,最好 易变更组件 和 不易变更组件 分别部署。

这个顺序没有问题,只有 mysql 启动了数据迁移才可能执行成功啊。具体的报错信息是什么,方便贴一下吗?

报错信息

【Docker】关于docker-compose顺序问题

Successfully built b411e34b4606

Successfully tagged koaapi_nginx:latest

Recreating mysql ...

Recreating mysql ... done

Recreating nodejs ...

Recreating nodejs ... done

Recreating nginx ...

Recreating nginx ... done

Attaching to mysql, nodejs, nginx

mysql | Initializing database

nodejs |

nodejs | Sequelize CLI [Node: 11.6.0, CLI: 5.4.0, ORM: 4.42.0]

nodejs |

nodejs | Loaded configuration file "config/db.js".

nodejs | Using environment "development".

nodejs | Tue, 26 Feb 2019 02:42:01 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/[email protected]@sequelize/lib/sequelize.js:242:13

nodejs |

nodejs | ERROR: connect ECONNREFUSED 172.23.0.2:3306

nodejs |

nodejs exited with code 1

mysql | Database initialized

mysql | MySQL init process in progress...

mysql | MySQL init process in progress...

mysql | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.

mysql | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.

mysql | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.

mysql | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

mysql | mysql: [Warning] Using a password on the command line interface can be insecure.

mysql | mysql: [Warning] Using a password on the command line interface can be insecure.

mysql | mysql: [Warning] Using a password on the command line interface can be insecure.

mysql | mysql: [Warning] Using a password on the command line interface can be insecure.

mysql |

mysql |

mysql | MySQL init process done. Ready for start up.

mysql |

通过 depends_on 设置的依赖关系会决定容器启动的先后顺序,但是这里的判断标准是容器运行了就继续启动下一个(运行与执行完任务是有区别的),如果你想更好的控制启动顺序,可以使用 wait-for-it 或者 dockerize 等官方推荐的第三方开源工具。
可参考官方文档https://docs.docker.com/compo...

以上是 【Docker】关于docker-compose顺序问题 的全部内容, 来源链接: utcz.com/a/77562.html

回到顶部