On the order of docker-compose

docker-compose.yml file

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

dockerfile of nodejs

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

dockerfile file of mysql

FROM mysql:8.0

the problem now is that after mysql runs, nodejs also runs. When you go to . / node_modules/.bin/sequelize db:migrate data migration, you will get an error because mysql is started, but the database initialization is not complete, so the connection is not successful.

I would like to ask you Daniel, is there any better solution?

There is no problem with the order of

. Only if mysql initiates the data migration can it be executed successfully. What is the specific error message? is it convenient to post it?


error message

clipboard.png

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-sharpoperators at node_modules/_sequelize@4.42.0@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     | 

the dependency set through depends_on determines the order in which the container starts, but the criterion here is to start the next one once the container is running (there is a difference between running and executing tasks). If you want to better control the startup order, you can use officially recommended third-party open source tools such as wait-for-it or dockerize.
refer to the official document https://docs.docker.com/compo...


depends_on can only determine the startup sequence. Cannot decide the order of readiness (there is a time window from container startup to program readiness)
if you only use a simple orchestration tool such as compose, you need to do your own readiness check.
in addition, it is not recommended to put data persistence layers such as mysql and applications together. It is best to deploy components that are easy to change and those that are not easy to change.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bdc019-3966d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bdc019-3966d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?