5

I just set up a Dockerfile and a docker-compose.yml. And for some reason, Docker cannot run this script. Whenever this script is run, it goes through all SQL files and migrates that file.

#!/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

I keep getting error messages ./config/scripts/migrate_local.sh: line 25: mysql: command not found which is this line mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME < $filename.

How can I fix this? Here are the Dockerfile and docker-compose.yml, I'm using sudo docker-compose up --build to run.

File 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" ]

File 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

I use winston as my logger. However, it doesn't work whatsoever, but console.log works fine. This might be the error:

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tri Nguyen
  • 1,688
  • 3
  • 18
  • 46
  • where this script is running? on your `universe` image? – Emruz Hossain Nov 09 '18 at 05:39
  • @EmruzHossain yeah exactly, here is the script `chmod +x ./config/scripts/dbsetup_local.sh && ./config/scripts/dbsetup_local.sh` running inside the `universe` image. I updated what I found, that's might be the error – Tri Nguyen Nov 09 '18 at 06:35

1 Answers1

5

Your universe image does not have mysql command line tools installed. You have to install it while building the Docker image.

Try the following 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" ]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emruz Hossain
  • 4,764
  • 18
  • 26
  • 1
    Thanks. It's solved the above problem but this problem came up `ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)` – Tri Nguyen Nov 09 '18 at 07:34
  • try using `DB_HOST="127.0.0.1"` instead of `DB_HOST="localhost"`. Ref: https://help.memsql.com/hc/en-us/articles/115001215603-ERROR-2002-HY000-Can-t-connect-to-local-MySQL-server-through-socket-var-run-mysqld-mysqld-sock- – Emruz Hossain Nov 09 '18 at 08:23
  • 1
    I got `ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)`. I think `localhost` was correct, it's something with permission of `/var/run/mysqld/mysqld.sock` – Tri Nguyen Nov 09 '18 at 08:47
  • are you able to access mysql database from local machine? – Emruz Hossain Nov 09 '18 at 08:59
  • Yes it runs perfectly fine – Tri Nguyen Nov 09 '18 at 09:02
  • I have modified script and docker_compose file slightly. Can you try them? https://gist.github.com/hossainemruz/522fbcc69bee4f73fe63a3bcbff336c7 https://gist.github.com/hossainemruz/53303a48f1906610cdb8c7257cc08f53 – Emruz Hossain Nov 09 '18 at 09:30
  • Thanks, I tried it still giving the error, I'm gonna go back to the original `docker` way instead of `docker-compose` – Tri Nguyen Nov 09 '18 at 11:33
  • It does but for some reason it keeps showing this line `while ! nc -q 1 $DB_HOST $DB_PORT – Tri Nguyen Nov 09 '18 at 16:27
  • @TriNguyen My answer was deleted because I posted too many similar answers in different places. The solution to your problem is that you need `mysql-server` on your local machine, see [docker exec ... mysql -h127.0.0.1 from utils shell script doesn't work](https://stackoverflow.com/a/70302203/11154841) which is caused by the same situation as this question here or [MySQL command line '-bash command not found'](https://stackoverflow.com/a/70302633/11154841). – questionto42 Dec 13 '21 at 09:47