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

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

问题描述

我正在运行这些容器:

我可以按预期访问 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.

我在本地运行node应用+docker容器中的数据库没有问题,但是当我也在docker容器中运行node应用时会出现这个问题.

数据库应该在什么主机和端口上可用(我应该连接到什么?),我可能遗漏了什么或做错了什么?

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 地址的 mysql 容器,即 172.25.3.3.这些东西的实现可以在这里看到.

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.

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

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