我正在使用Docker容器作为我的节点应用程序,这是一个REST API服务器。它使用多个后端(目前是mongoDB和mysql)。我的mongoclient连接到我的docker-compose.yml中定义的端口,但我的mysql没有配置它,抛出ECONNREFUSED错误。另外我想添加在我的localhost中我在我的系统上安装了mongodb和mysql。下面是我的Dockerfile
和docker-compose.yml
的代码
FROM node:8
RUN mkdir -p /home/sharad/eapp
WORKDIR /home/sharad/eapp
COPY package.json /home/sharad/eapp/
RUN npm install
COPY . /home/sharad/eapp
EXPOSE 8016
CMD [ "npm","start" ]
version: '3.1'
services:
server:
image: sharadm20/eyantra-app
restart: always
ports:
- 8016:8016
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
links:
- mongo
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
adminer:
image: adminer
restart: always
ports:
- 8082:8080
以下是我遇到的错误:
Unable to connect to the mysql database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 }
它通过将mysql配置中的Hostname更改为类似于docker image name的名称(在我的情况下mysql称为db),因此主机名+端口看起来像db:3306。
感谢Marc Sances指出它。