Docker 无法使用 mysql 链接到未运行的容器

Posted

技术标签:

【中文标题】Docker 无法使用 mysql 链接到未运行的容器【英文标题】:Docker cannot link to a non running container using mysql 【发布时间】:2018-12-25 00:58:51 【问题描述】:

我正在尝试使用 gitlab 的 CI(持续集成)为 Laravel 项目进行自动测试(使用 Dusk)。

但是在composer初始化后我总是收到这个错误

ERROR: Job failed (system failure): Error response from daemon: Cannot link to a non running container: /runner-0a53e0ab-project-30-concurrent-0-mysql-0 AS /runner-0a53e0ab-project-30-concurrent-0-predefined-5/mysql

使用的版本

我正在使用 docker (18.03.1-ce) 来管理一切。

我有 Gitlab (10.0.3)、gitlab-runner (11.0)。我还发现了这个docker image,它似乎非常适合我的用例。

我不使用docker-compose(所以this thread不适用于我)。

警告

一开始我也有这个警告

Running with gitlab-runner 11.0.0 (5396d320)
  on my-web-runner 0a53e0ab
Using Docker executor with image chilio/laravel-dusk-ci:stable ...
Starting service mysql:5.7 ...
Pulling docker image mysql:5.7 ...
Using docker image sha256:c356247174ed09823171ae537fd70273900103f2474c2eb62d8782ececcba7ab for mysql:5.7 ...
Waiting for services to be up and running...

*** WARNING: Service runner-0a53e0ab-project-30-concurrent-0-mysql-0 probably didn't start properly.

Health check error:
service "runner-0a53e0ab-project-30-concurrent-0-mysql-0-wait-for-service" timeout

但是容器创建得很好,在警告之后我也有这些信息

Service container logs:
2018-07-17T08:15:15.332803346Z Initializing database
2018-07-17T08:15:18.023855458Z 2018-07-17T08:15:18.020174Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-17T08:15:18.979992069Z 2018-07-17T08:15:18.979239Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-17T08:15:19.697917282Z 2018-07-17T08:15:19.697211Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8b6ce71e-8999-11e8-aee6-0242ac110003.
2018-07-17T08:15:19.839972447Z 2018-07-17T08:15:19.838426Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

.gitlab-ci.yml

我正在用这个 .gitlab-ci.yml

构建一切
stages:
  - build
  - test

# Variables
variables:
  MYSQL_ROOT_PASSWORD: my_pw
  MYSQL_USER: my_user
  MYSQL_PASSWORD: my_pw
  MYSQL_DATABASE: testing_db
  DB_HOST: mysql
  DB_CONNECTION: mysql

build:
  stage: build
  services:
     - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts

test:
  stage: test
  cache:
    key: $CI_BUILD_REF_NAME
    paths:
      - vendor
      - node_modules
    policy: pull

  services:
    - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - cp .env.example .env
    - configure-laravel
    - start-nginx-ci-project
    - php artisan dusk

  artifacts:
    paths:
      - ./storage/logs # for debugging
      - ./tests/Browser/screenshots
      - ./tests/Browser/console
    expire_in: 7 days
    when: always

尝试

我尝试重启整机,然后重启docker服务 我删除了所有内容并从头开始重建 我用--privileged 启动了跑步者并检查了config.toml 按照here 的解释,我尝试了 chilio/laravel-dusk 的 3 个版本

日志

这是 gitlab runner 容器的日志

# docker logs gitlab-runner
Starting multi-runner from /etc/gitlab-runner/config.toml ...  builds=0
Running in system-mode.

Configuration loaded                                builds=0
WARNING: 'metrics_server' configuration entry is deprecated and will be removed in one of future releases; please use 'listen_address' instead
Metrics server disabled
Checking for jobs... received                       job=29 repo_url=http://gitlab.local/Customize/Web.git runner=0a53e0ab
ERROR: Job failed (system failure): Error response from daemon: Cannot link to a non running container: /runner-0a53e0ab-project-30-concurrent-0-mysql-0 AS /runner-0a53e0ab-project-30-concurrent-0-predefined-5/mysql  job=29 project=30 runner=0a53e0ab

【问题讨论】:

【参考方案1】:

尝试重启容器; docker restart gitlab-runner.

对照最新的GitLab requirements检查您的服务器

【讨论】:

以上是关于Docker 无法使用 mysql 链接到未运行的容器的主要内容,如果未能解决你的问题,请参考以下文章

无法使用链接的 mysql 数据库在 docker 中启动应用程序

无法将 mysql docker 容器与 Spring Boot 应用程序链接 - 通信链接失败

强制链接到未使用的共享库

如何在交叉编译期间强制链接到未安装的库

Docker节点应用程序无法连接链接的mysql容器

无法连接到在 docker 中运行的 MySQL