使用 EB CLI 运行本地 java/mysql 容器堆栈返回:nc: bad address 'mysql-server'

Posted

技术标签:

【中文标题】使用 EB CLI 运行本地 java/mysql 容器堆栈返回:nc: bad address \'mysql-server\'【英文标题】:Running local java/mysql container stack with EB CLI returns: nc: bad address 'mysql-server'使用 EB CLI 运行本地 java/mysql 容器堆栈返回:nc: bad address 'mysql-server' 【发布时间】:2019-11-28 15:19:00 【问题描述】:

所以,我有一个 docker-compose 堆栈,它有 2 个容器:一个 mysql5.7 服务器和一个连接到 MySQL 服务器的 Spring Boot 应用程序。

最终我想在 Elastic Beanstalk 中将此堆栈部署为多 Docker 容器环境。

但首先要做的事情。我为堆栈编写了以下 Docker Compose 文件:

docker-compose.yml:

version: '3'

services:
  mysql-server:
    container_name: mysql-server-container
    image: mysql/mysql-server:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: packages
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_HOST: '%'
    restart: always

  saver:
    container_name: saver-poc-container
    image: travelpaq1/saver-poc:latest
    ports:
      - 3000:8080
    environment:
      DB_HOST: jdbc:mysql://mysql-server
      DB_USER: user
      DB_PASSWORD: test
    depends_on:
      - mysql-server

其中mysql-server服务使用官方MySQL5.7 docker镜像,saver服务镜像如下:

保存程序Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
RUN apk add --no-cache bash
COPY wait-for-it.sh wait-for-it.sh
RUN chmod +x wait-for-it.sh
ARG JAR_FILE
COPY $JAR_FILE app.jar
ENTRYPOINT ["./wait-for-it.sh", "--timeout=60",  "mysql-server:3306", "--", "java", "-jar", "app.jar"]

您可以注意到我正在使用wait-for-it 等待数据库容器完全初始化,然后再启动应用容器。

所以,如果我运行 docker-compose up 一切正常!

现在,问题: 正如我之前提到的,我想在 Elastic Beanstalk 的多 Docker 容器环境中运行这个堆栈。 Elastic Beanstalk 不支持使用 Docker Composer 文件进行部署,而是使用名为 Dockerrun.aws.json 的文件。我使用container-transform 获得了一个与我的docker-compose.yml 等效的Dockerrun.aws.json 文件。文件本身如下:


    "AWSEBDockerrunVersion": 2,
    "containerDefinitions": [
        
            "environment": [
                
                    "name": "MYSQL_ROOT_PASSWORD",
                    "value": "root"
                ,
                
                    "name": "MYSQL_DATABASE",
                    "value": "packages"
                ,
                
                    "name": "MYSQL_USER",
                    "value": "user"
                ,
                
                    "name": "MYSQL_PASSWORD",
                    "value": "test"
                ,
                
                    "name": "MYSQL_ROOT_HOST",
                    "value": "%"
                
            ],
            "essential": true,
            "image": "mysql/mysql-server:5.7",
            "name": "mysql-server"
        ,
        
            "environment": [
                
                    "name": "DB_HOST",
                    "value": "jdbc:mysql://mysql-server"
                ,
                
                    "name": "DB_USER",
                    "value": "user"
                ,
                
                    "name": "DB_PASSWORD",
                    "value": "test"
                
            ],
            "essential": true,
            "image": "travelpaq1/saver-poc:latest",
            "name": "saver",
            "portMappings": [
                
                    "containerPort": 8080,
                    "hostPort": 3000
                
            ]
        
    ],
    "family": "",
    "volumes": []

EB CLI 支持使用eb local run 在本地运行堆栈。但是当我运行它时,我会得到以下日志:

Creating elasticbeanstalk_mysqlserver_1 ... done
Creating elasticbeanstalk_saver_1       ... done
Attaching to elasticbeanstalk_mysqlserver_1, elasticbeanstalk_saver_1
mysqlserver_1  | [Entrypoint] MySQL Docker Image 5.7.26-1.1.11
saver_1        | wait-for-it.sh: waiting 60 seconds for mysql-server:3306
saver_1        | nc: bad address 'mysql-server'
mysqlserver_1  | [Entrypoint] Initializing database
saver_1        | nc: bad address 'mysql-server'
saver_1        | nc: bad address 'mysql-server'
saver_1        | nc: bad address 'mysql-server'
saver_1        | nc: bad address 'mysql-server'
saver_1        | nc: bad address 'mysql-server'
mysqlserver_1  | [Entrypoint] Database initialized
mysqlserver_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysqlserver_1  | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
saver_1        | nc: bad address 'mysql-server'
mysqlserver_1  | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
mysqlserver_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysqlserver_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysqlserver_1  | 
mysqlserver_1  | [Entrypoint] ignoring /docker-entrypoint-initdb.d/*
mysqlserver_1  | 
saver_1        | nc: bad address 'mysql-server'
mysqlserver_1  | [Entrypoint] Server shut down
mysqlserver_1  | 
mysqlserver_1  | [Entrypoint] MySQL init process done. Ready for start up.
mysqlserver_1  | 
mysqlserver_1  | [Entrypoint] Starting MySQL 5.7.26-1.1.11
saver_1        | nc: bad address 'mysql-server'
saver_1        | nc: bad address 'mysql-server'
...

它只是继续打印

saver_1        | nc: bad address 'mysql-server'

最后,wait-for-it 超时到期,saver 应用程序无法启动,因为它无法连接到数据库。

如您所见,数据库服务容器被初始化,wait-for-it 似乎在行动,但是saver_1 服务无法解析与数据库服务容器对应的mysql-server 地址(我猜指的是等待它尝试测试的地址mysql-server:3306

那么,可能是什么问题?如何让应用服务容器解析数据库服务容器 URL?我对 Elastic Beanstalk 中的多 Docker 容器环境不太熟悉。

【问题讨论】:

【参考方案1】:

只是我的一个愚蠢的错误。

我在 saver 应用程序容器的容器定义中缺少 links 属性,以允许容器相互“对话”,如下所示:

"links": [
    "mysql-server"
],

我可以删除这个问题,但它可能有助于作为在 Elastic Beanstalk 中部署 docker 堆栈的摘要。

【讨论】:

在最新版本的 Docker 上出现同样的错误,尽管在以前的版本中我根本没有使用 links

以上是关于使用 EB CLI 运行本地 java/mysql 容器堆栈返回:nc: bad address 'mysql-server'的主要内容,如果未能解决你的问题,请参考以下文章

安装 EB CLI 3.x 后出错

如何使用 EB cli 查看我的 EB ec2 的 python 日志

AWS EB CLI Django

使用 eb-cli 创建单实例 Elastic Beanstalk 应用程序

我正在尝试使用 AWS EB CLI (elastic beanstalk) 部署 python 应用程序

如何使用 EB CLI 在应用程序之间切换?