连接码头容器 [英] Linking Docker Containers
问题描述
我已经拉下了一个mongo和redis容器,并对我的应用程序进行了dockerized。
我启动了我的mongo和redis容器,如:
docker run -i -t --name redis -d redis
docker run -i -t --name mongo -d mongo
现在,我链接我的nodejs应用程序容器到这两个并运行应用程序:
docker run -i -t --name myapp --link mongo:mongo --link redis:redis mseay / myapp node /myapp/server.js
当我运行我的应用程序,它失败并出现错误
错误:Redis连接到本地主机:6379失败 - 连接ECONNREFUSED
我的应用程序无法连接到我的redis容器或mongo,即使它们都在运行。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8709c818014a redis:最新的/entrypoint.sh redi 12分钟前Up 12分钟6379 / tcp redis
6d87364ad4c9 mongo:最新/entrypoint.sh mong 12分钟前Up 12分钟27017 / tcp mongo
任何想法?
确保您正在连接到您的mongodb和redis实例:
请注意,我已经做了一些更改如何链接您的容器。这些名称很重要,因为它们将在后面提及。
docker run -i -t --name myapp --link mongo:MONGODB --link redis:REDIS mseay / myapp node /myapp/server.js
为了连接到MongoDB:
IP = process.env.MONGODB_PORT_27017_TCP_ADDR
PORT = process.env.MONGODB_PORT_27017_TCP_PORT
var mongoUrl ='mongodb://'+ IP +':'+ PORT +'/';
或者您可以简单地使用:
var mongoUrl ='mongodb://'+ MONGODB +':27017 /';
同样通过使用其作为REDIS的ip连接到redis数据库。
说明:
当您创建码头货柜并链接其他码头货柜通过链接
参数,docker会修改您的容器主机文件,并将链接的容器的IP插入其名称(您选择为 - link = container_name:NAME_OF_YOUR_CHOICE
)。
因此,如果您在新容器中打开一个bash并尝试运行
ping MONGODB
ping REDIS
您可以看到两者都可以访问,因此如果您尝试连接到它们,它将工作(假设您的mongodb和redis安装在新容器中,并且您的redis和mongodb实例正在运行默认端口)
mongo --host = MONGODB
redis-cli -h REDIS
I have a nodejs app i'm trying to run in a docker container. My app uses mongodb and redis.
I've pulled down both a mongo and redis container and dockerized my app.
I started up my mongo and redis containers like:
docker run -i -t --name redis -d redis
docker run -i -t --name mongo -d mongo
Now, I link my nodejs app container to both of these and run the app:
docker run -i -t --name myapp --link mongo:mongo --link redis:redis mseay/myapp node /myapp/server.js
When I run my app, it fails with the error
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED
My app cannot connect to either my redis container or mongo even though they're both running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8709c818014a redis:latest "/entrypoint.sh redi 12 minutes ago Up 12 minutes 6379/tcp redis
6d87364ad4c9 mongo:latest "/entrypoint.sh mong 12 minutes ago Up 12 minutes 27017/tcp mongo
Any ideas?
Make sure that you are connecting to your mongodb and redis instance as so:
Note that I have made some changes how you link your containers. The names are important as they are referred later.
docker run -i -t --name myapp --link mongo:MONGODB --link redis:REDIS mseay/myapp node /myapp/server.js
For connecting to MongoDB:
IP = process.env.MONGODB_PORT_27017_TCP_ADDR
PORT = process.env.MONGODB_PORT_27017_TCP_PORT
var mongoUrl = 'mongodb://' + IP + ':' + PORT + '/';
or you can simply use:
var mongoUrl = 'mongodb://' + MONGODB + ':27017/';
Similarly connect to redis database by using its ip as REDIS.
Explanation:
When you create a docker container and link other docker containers via the --link
parameter, docker modifies your containers hosts file and inserts the IP of the linked containers against their names (that you choose as --link=container_name:NAME_OF_YOUR_CHOICE
).
Hence, if you open a bash in your new container and try to run
ping MONGODB
ping REDIS
you can see that both are reachable, and hence if you try connecting to them, it works (assuming your have mongodb and redis installed in the new container, and that your redis and mongodb instances are running on default ports)
mongo --host=MONGODB
redis-cli -h REDIS
这篇关于连接码头容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!