やりたいこと
node(フレームワーク: loopback4)での環境をapp, データベースをdbとしてdocker-composeで構築し疎通したい
現状
docker-compose up時にappがエラーで停止する(ローカルからdbへの接続は確認済み)
エラー内容
Error: connect ECONNREFUSED 127.0.0.1:3306 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16) app_1 | errno: -111, app_1 | code: 'ECONNREFUSED', app_1 | syscall: 'connect', app_1 | address: '127.0.0.1', app_1 | port: 3306, app_1 | fatal: true
やったこと
- dbとの接続のために指定するhostをdocker-composeのサービス名(db)へ変更
- my.cnfの外部ホストからの接続設定をbind-address=127.0.0.1からbind-address=0.0.0.0へ変更
- デバッグ方法などご教授いただけるととても嬉しいです。
現在のDockerfile, docker-compose.yml
yml
1version: "3.8" 2volumes: 3 mydata: 4 db-store: 5services: 6 app: 7 build: 8 context: . 9 dockerfile: Dockerfile 10 volumes: 11 - type: volume 12 source: mydata 13 target: /data 14 volume: 15 nocopy: true 16 - type: bind 17 source: ./src 18 target: /work/src 19 environment: 20 - DB_CONNECTION=mysql 21 - DB_HOST=db 22 - DB_PORT=3306 23 - DB_DATABASE=${DB_NAME} 24 - DB_USERNAME=${DB_USER} 25 - DB_PASSWORD=${DB_PASS} 26 depends_on: 27 - db 28 - db-testing 29 ports: 30 - "3000:3000" 31 db: 32 image: mysql:8.0 33 volumes: 34 - db-store:/var/lib/mysql 35 - ./logs:/var/log/mysql 36 - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf 37 environment: 38 - MYSQL_DATABASE=${DB_NAME} 39 - MYSQL_USER=${DB_USER} 40 - MYSQL_PASSWORD=${DB_PASS} 41 - MYSQL_ROOT_PASSWORD=${DB_PASS} 42 - TZ=${TZ} 43 ports: 44 - ${DB_PORT}:3306 45 46 db-testing: 47 image: mysql:8.0 48 volumes: 49 - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf 50 tmpfs: 51 - /var/lib/mysql 52 - /var/log/mysql 53 environment: 54 - MYSQL_DATABASE=${DB_NAME} 55 - MYSQL_USER=${DB_USER} 56 - MYSQL_PASSWORD=${DB_PASS} 57 - MYSQL_ROOT_PASSWORD=${DB_PASS} 58 - TZ=${TZ}
- dockerfile
# Check out https://hub.docker.com/_/node to select a new base image FROM node:16-slim # Set to a non-root built-in user `node` USER node # Create app directory (with user `node`) RUN mkdir -p /home/node/app WORKDIR /home/node/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY --chown=node package*.json ./ RUN npm install # Bundle app source code COPY --chown=node . . RUN npm run build # Bind to all network interfaces so that it can be mapped to the host OS ENV HOST=0.0.0.0 PORT=3000 EXPOSE ${PORT} CMD [ "node", "." ]
mysql起動時に読み込ませるmy.cnf
[client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 [mysqldump] default-character-set=utf8mb4 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_bin lower_case_table_names=1 # Enable access from the host machine. bind-address=0.0.0.0
mysqlとの疎通のためにapp側へ入れているライブラリ
"loopback-connector-mysql": "^6.0.2",
- package.json全体
{ "name": "mysql-tutorial", "version": "0.0.1", "description": "sample", "keywords": [ "loopback-application", "loopback" ], "main": "dist/index.js", "types": "dist/index.d.ts", "engines": { "node": ">=10.16" }, "scripts": { "build": "lb-tsc", "build:watch": "lb-tsc --watch", "lint": "yarn run eslint && yarn run prettier:check", "lint:fix": "yarn run eslint:fix && yarn run prettier:fix", "prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"", "prettier:check": "yarn run prettier:cli -l", "prettier:fix": "yarn run prettier:cli --write", "eslint": "lb-eslint --report-unused-disable-directives .", "eslint:fix": "yarn run eslint --fix", "pretest": "yarn run rebuild", "test": "lb-mocha --allow-console-logs \"dist/__tests__\"", "posttest": "yarn run lint", "test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && yarn run posttest", "docker:build": "docker build -t mysql-tutorial .", "docker:run": "docker run -p 3000:3000 -d mysql-tutorial", "premigrate": "yarn run build", "migrate": "node ./dist/migrate", "preopenapi-spec": "yarn run build", "openapi-spec": "node ./dist/openapi-spec", "prestart": "yarn run rebuild", "start": "node -r source-map-support/register .", "clean": "lb-clean dist *.tsbuildinfo .eslintcache", "rebuild": "yarn run clean && yarn run build" }, "repository": { "type": "git", "url": "" }, "author": "yosuke0517 <yosuke.takeuchi.dev@gmail.com>", "license": "", "files": [ "README.md", "dist", "src", "!*/__tests__" ], "dependencies": { "@loopback/boot": "^3.4.3", "@loopback/core": "^2.17.0", "@loopback/repository": "^3.7.2", "@loopback/rest": "^10.0.1", "@loopback/rest-explorer": "^3.3.3", "@loopback/service-proxy": "^3.2.3", "loopback-connector-mysql": "^6.0.2", "tslib": "^2.0.0" }, "devDependencies": { "@loopback/build": "^7.0.1", "@loopback/eslint-config": "^11.0.1", "@loopback/testlab": "^3.4.3", "@types/node": "^10.17.60", "eslint": "^7.32.0", "source-map-support": "^0.5.20", "typescript": "~4.3.5" } }
DB接続の設定ファイル(db.datasource.ts)
const config = { name: 'db', connector: 'mysql', url: '', host: 'db', port: 3306, user: 'loopback', password: 'password', database: 'demo' };
回答1件
あなたの回答
tips
プレビュー