Occasional mysql error `ER_NO_SUCH_TABLE: Table 'circle_test.foobar' doesn't exist`

I encounter this error occasionally, table name can be an arbitrary table in the database. It can usually be fixed by rebuild without cache a couple of times. BTW, I have never run into this on my local machine.

Here’s my circle.yml, what am I doing wrong?

machine:
  python:
    version: 2.7.6
  node:
    version: 4.0
  services:
    - docker

dependencies:
  pre:
    - pip install awscli
    - npm install npm -g
  override:
    - docker info
    - docker build -t myorg/myrepo:$CIRCLE_SHA1 .

test:
  pre:
    - sudo sed -i "s/bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
    - sudo /etc/init.d/mysql restart
    - npm i
  override:
    - ./node_modules/.bin/lab -c -t 100 -L -v -r html -o $CIRCLE_ARTIFACTS/coverage.html
    - docker run -d --net=host -e CI=true -e NODE_ENV=test myorg/myrepo:$CIRCLE_SHA1; sleep 10
    - curl --retry 10 --retry-delay 5 -v http://localhost:8378

deployment:
  production:
    branch: master
    commands:
      - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
      - docker push myorg/myrepo:$CIRCLE_SHA1
      - chmod u+x eb-deploy.sh && ./eb-deploy.sh
  staging:
    branch: staging
    commands:
      - chmod u+x eb-deploy.sh && ./eb-deploy.sh

It honestly sounds like this is a cacheing issue where the cache should be flat out deleted. You can do this yourself through the API using the DELETE: /project/:username/:project/build-cache API call. Alternately a CircleCI support engineer can clear the cache for you if you provide the organization and project name.

As this is getting fixed with a few rebuilds for the same SHA, I would suppose the issue is related to timing of specific actions / race conditions.

How exactly do you set the database up before the tests hit it? Are the tests hitting the database that’s running in the container? If not, could it be that the database is not fully up when the tests start running?

There should be no problem with my test scripts, because I ran them locally dozens of times everyday and never had such an issue.
However I don’t ‘hard restart’ mysql on my local machine every time I run tests, while I do so on Circle(see test/pre section in the yml). Is it the cause?

BTW, the test/pre section is adapted from docs here