当它们在单独的Docker容器中运行时,如何将Node.js应用程序连接到数据库? [英] How to connect Node.js app to database when they are running in separate Docker containers?

查看:246
本文介绍了当它们在单独的Docker容器中运行时,如何将Node.js应用程序连接到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下容器:

我能够按预期访问localhost:49160上的节点应用程序和localhost:7474上的数据库.

I am able to visit node app at localhost:49160 and database at localhost:7474 as expected.

但是,节点应用似乎无法实际连接到数据库.尝试时出现此错误:

However, the node app can't seem to actually connect to the database. I get this error when I try:

Error: connect ECONNREFUSED 127.0.0.1:7474
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)

这通常意味着它无法在该主机:端口上找到数据库.

This normally means it can't find the database at that host:port.

同样,我能够访问127.0.0.1:7474并清楚地看到数据库正在运行.

Again, I am able to visit 127.0.0.1:7474 and clearly see the database is running.

我也尝试连接到0.0.0.0:7474,但这也不起作用.

I have also tried to connecting to 0.0.0.0:7474, but that didn't work either.

当我在docker容器中本地运行节点应用程序+数据库时没有问题,但是当我也在docker容器中运行节点应用程序时发生此问题.

应该在哪个主机和端口上提供数据库(我应该连接到什么?),我可能丢失了什么或做错了什么?

What host and port should be database be available at (what should I connect to?), and what am I probably missing or doing wrong?

用于节点应用的Dockerfile

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN yarn

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "yarn", "start" ]

docker neo4j运行cmd

docker run -d -e NEO4J_AUTH=none \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/logs:/logs \
    --volume=$HOME/neo4j/conf:/conf \
    --volume=$HOME/neo4j/plugins:/plugins \
    neo4j:3.1.4

推荐答案

我正在描述一种方法,目前我可能还不知道其他可能的方法.由于links现在已被弃用,因此我们可以使用网络将一个容器连接到另一个容器.

I am describing one way of doing it, and there might be other possible ways which I am currently unaware of. Since the links are deprecated now, we can use networks to connect one container to another.

首先,我们使用以下命令创建网络:

First of all we create a network with this command:

docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw

然后我们通过从创建的网络为其分配IP地址来连接mysql容器.是的,我们也指定了网络.该命令完成了所需的目的:

And then we connect the mysql container by assigning it an IP address from the network we created. And yeah, we specify the network too. This command does the required purpose:

docker run -itd --rm \
  --network=isolated_nw \
  --ip=172.25.3.3 \
  --name=mysql \
  --volume "$(pwd)/database/:/docker-entrypoint-initdb.d" mysql

现在,我们已经使我们的mysql容器运行并连接到我们创建的isolated_nw.是时候让我们的其他容器连接到同一网络并访问mysql容器了.运行容器时添加--network=isolated_nw标志会将容器添加到网络.并且在运行需要访问mysql的其他容器时,我们将使用此标志.

Now we have got our mysql container running and connected to the isolated_nw we created. Its time to get our other container connected to the same network and access the mysql container. Adding --network=isolated_nw flag while running a container adds the container to the network. And we shall use this flag while running our other container which requires to access mysql.

现在完成了!在这个其他容器中,我们可以访问我们分配的IP地址(即172.25.3.3)上的mysql容器.可以在此处看到这些东西的实现. >.

Now it is done! Inside this other container we can access mysql container at the IP address we allocated, i.e, at 172.25.3.3. An implementation of these things can be seen here.

这篇关于当它们在单独的Docker容器中运行时,如何将Node.js应用程序连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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