Docker,MySQL-在.sh文件中找不到命令

时间:2018-11-09 04:03:40

标签: mysql node.js docker docker-compose

我是Docker的新手。我只是设置了一个Dockerfile和一个docker-compose.yml。并且由于某些原因,docker无法运行该脚本。每当运行此脚本时,它将遍历所有SQL文件并迁移该文件

#!/bin/bash

DB_NAME="database_name"
DB_USER="username"
DB_PASS="password"
DB_HOST="localhost"

#get current version
GET_DATABASE_VERSION="SELECT name FROM version TOP 1"
#

echo "Getting data version"
version=$(mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e "SELECT name FROM version")
#trim string to get current version name
version=${version:5}
x="./config/scripts/migration/$version"
echo $version
MIGRATION_PATH="./config/scripts/migration/*"
#get migration file for newer version
for filename in $MIGRATION_PATH -maxdepth 2
do
    if [[ -f $filename ]]; then
        if [[ "$filename" > "$x" ]] || [ "$filename" == "$x" ]; then
            echo "Running migration file: $filename"
            mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME < $filename
        fi
    fi
done

我不断收到错误消息./config/scripts/migrate_local.sh: line 25: mysql: command not found,这是此行mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME < $filename

有人知道如何解决此问题吗?这是Dockerfiledocker-compose.yml,我正在使用sudo docker-compose up --build来运行。

Dockerfile

FROM node:8
MAINTAINER Tri Nguyen "me@mydomain.com"

RUN mkdir -p /usr/src
WORKDIR /usr/src

COPY package.json /usr/src
RUN npm install
RUN npm rebuild node-sass --force
COPY . /usr/src

EXPOSE 3000 8000
CMD [ "npm", "start" ]

docker-compose.yml

version: "2"
services:
  universe:
    build: .
    working_dir: /usr/src
    environment:
    - NODE_ENV=default
    - PORT=3000
    volumes:
    - /usr/src
    ports:
    - "3000:3000"
    - "8000:8000"
    links:
    - redis
    - mysql

  redis:
    image: redis
    volumes:
    - /data/redis:/data

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "database_name"
      MYSQL_USER: "username"
      MYSQL_PASSWORD: "password"
    volumes:
    - /data/mysql:/var/lib/mysql

更新 我使用winston作为记录器,但是无论如何都无法正常工作,console.log可以正常工作。这可能是错误:

universe_1  |   errno: 'ECONNREFUSED',
universe_1  |   code: 'ECONNREFUSED',
universe_1  |   syscall: 'connect',
universe_1  |   address: '127.0.0.1',
universe_1  |   port: 3307,
universe_1  |   fatal: true 

1 个答案:

答案 0 :(得分:1)

您的universe映像未安装mysql命令行工具。您必须在构建docker映像时安装它。

尝试遵循Dockerfile:

FROM node:8
MAINTAINER Tri Nguyen "me@mydomain.com"

RUN mkdir -p /usr/src
WORKDIR /usr/src

COPY package.json /usr/src
RUN npm install
RUN npm rebuild node-sass --force
RUN set -ex; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
    mysql-client

COPY . /usr/src

EXPOSE 3000 8000
CMD [ "npm", "start" ]