NodeJS + Mysql与Docker Compose 2 [英] NodeJS + Mysql with Docker Compose 2

查看:127
本文介绍了NodeJS + Mysql与Docker Compose 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个docker-compose文件来部署本地连接到mysql服务器的NodeJS应用程序。我尝试了一切(在Stackoverflow中阅读了很多教程和一些问题),但是我不断得到ECONNREFUSED错误。
这是NodeJS的Docker文件:

  ## Nodejs 

FROM node:最新

RUN useradd --user-group --create-home --shell / bin / false app

ENV HOME = / home / app

COPY package.json npm-shrinkwrap.json $ HOME / playerground /
RUN chown -R app:app $ HOME / *

USER app
WORKDIR $ HOME / playerground
RUN npm cache clean&& npm install --silent --progress = false

USER root
COPY。 $ HOME / playerground
RUN chown -R app:app $ HOME / *
USER app

这是我的Mysql Docker文件:



  FROM mysql:latestENV MYSQL_ROOT_PASSWORD root ENV MYSQL_DATABASE playerground ENV MYSQL_USER root ENV MYSQL_PASSWORD根 



这是我的docker-compose:



  '2'services:db:build:./database ports: - 3307:3306playerground:build:context:。 dockerfile:Dockerfile命令:node_modules / .bin / nodemon --exec npm启动环境:ENVIRONMENT:开发端口: - 9090:9090链接: -  db卷: - 。:/ home / app / playerground  -  / home / app / playerground / node_modules  



另一件事是我的Nodejs的配置文件: / p>

  // Database'development':{'host':process.env。 HOSTNAME || 'localhost','user':'root','password':'root','database':'playerground'},// Server'development':{'host':process.env.HOSTNAME || '127.0.0.1','port':3000}, 



你可以帮我吗?
感谢

解决方案

在您的应用配置中,数据库主机没有指向您的MySql容器。您将其指向本地主机,这是应用程序容器。在撰写文件中,您可以使用以下方式明确命名db容器的链接:

 链接:
- db:db

然后您的数据库容器可以通过主机名 db 。您可以在应用配置中对该主机进行硬编码,因为对于所有环境,只要您使用相同的Compose文件,它将是相同的。



另外,如果您正在使用最新版本的Docker,您不需要在同一Docker网络中的容器之间发布端口,因此您可以从Compose文件中删除这些端口:

  ports:
- 3307:3306

然后db容器可以被应用程序容器访问,但不能从外部访问。


I´m trying to built a docker-compose file to deploy locally my NodeJS app that connects to a mysql server. I´ve tried everything ( read a lot of tutorials and some questions here in Stackoverflow ) but I keep getting the ECONNREFUSED error. This is my Dockerfile from NodeJS:

##Nodejs

FROM node:latest

RUN useradd --user-group --create-home --shell /bin/false app

ENV HOME=/home/app

COPY package.json npm-shrinkwrap.json $HOME/playerground/
RUN chown -R app:app $HOME/*

USER app 
WORKDIR $HOME/playerground
RUN npm cache clean && npm install --silent --progress=false

USER root
COPY . $HOME/playerground
RUN chown -R app:app $HOME/*
USER app

This is my Mysql Dockerfile:

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD root  
ENV MYSQL_DATABASE playerground  
ENV MYSQL_USER root  
ENV MYSQL_PASSWORD root

And this is my docker-compose:

version: '2'
services: 
  db:
    build: ./database
    ports:
      - "3307:3306"  
  playerground:
      build:
        context: .
        dockerfile: Dockerfile
      command: node_modules/.bin/nodemon --exec npm start
      environment:
        ENVIRONMENT: development
      ports:
        - "9090:9090"
      links:
        - db  
      volumes:      
        - .:/home/app/playerground
        - /home/app/playerground/node_modules

Another thing is my configuration file from Nodejs:

//Database
'development' : {
  'host' : process.env.HOSTNAME || 'localhost',
  'user' : 'root',
  'password' : 'root',
  'database' : 'playerground'
},
//Server
'development' : {
  'host' : process.env.HOSTNAME || '127.0.0.1',
  'port' : 3000
},

Can you help me? Thanks

解决方案

In your app config, the database host isn't pointing at your MySql container. You're pointing it at the local host, which is the app container. In the Compose file you can explicitly name the link to the db container using:

  links:
    - db:db  

And then your database container can be reached at the hostname db. You can hard-code that host in your app config, because it will be the same for all environments, as long as you use the same Compose file.

Also, if you're using a recent version of Docker you don't need to publish ports between containers in the same Docker network - so you can remove this from your Compose file:

ports:
  - "3307:3306"  

Then the db container will be accessible by the app container, but not externally.

这篇关于NodeJS + Mysql与Docker Compose 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆