首先,这是我的文件夹结构:
数据库/ Dockerfile
FROM mysql:5.7
COPY script.sql /docker-entrypoint-initdb.d/
网络/ Dockerfile
FROM node:10.16.0
# Set the work directory
WORKDIR /app
# Install packages
COPY package.json /app
RUN npm install
# Add application files
COPY . /app
CMD ["npm", "start"]
docker-compose.yml
version: '3'
services:
db:
build: ./db
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=docker
- MYSQL_USER=root
- MYSQL_PASSWORD=root
web:
build: ./web
ports:
- "3333:3333"
depends_on:
- db
restart: on-failure
db / script.sql
CREATE DATABASE IF NOT EXISTS `docker` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `docker`;
节点应用
const connection = mysql.createConnection({
host: 'db',
user: 'root',
password: 'root',
database: 'docker'
});
只要我运行docker-compose up
,我总会得到
{ Error: connect ECONNREFUSED 192.168.80.3:3306
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
web_1 | --------------------
web_1 | at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
web_1 | at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
web_1 | at Connection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
web_1 | at Object.<anonymous> (/app/index.js:12:12)
web_1 | at Module._compile (internal/modules/cjs/loader.js:776:30)
web_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
web_1 | at Module.load (internal/modules/cjs/loader.js:653:32)
web_1 | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
web_1 | at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '192.168.80.3',
web_1 | port: 3306,
web_1 | fatal: true }
我在这里错过了什么吗?顺便说一下,我正在Windows 10上运行它。
任何帮助将不胜感激。