Linking Docker Containers

mikeyseay

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?

Sohail

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Docker Not Linking Containers

分類Dev

How to list containers in Docker

分類Dev

Building Docker Containers in VSTS

分類Dev

Docker compose linking appears to not work

分類Dev

Log management of various docker containers

分類Dev

Multiple Docker containers, same image, different config

分類Dev

How to assign domain names to containers in Docker?

分類Dev

docker-compose up for only certain containers

分類Dev

Multiple Docker containers, same image, different config

分類Dev

Multiple Docker containers, same image, different config

分類Dev

Efficiently using multiple docker containers in a single host

分類Dev

Start Docker Containers In Specific Order After Reboot

分類Dev

Is it possible to launch privileged docker containers on Amazon elasticbeanstalk?

分類Dev

How to use local docker containers with Kubernetes

分類Dev

How to run docker containers in their network with an external gateway?

分類Dev

Connect to postgres in laravel using docker containers

分類Dev

Docker container DNS alias available in other containers

分類Dev

Link two docker containers on Windows 10

分類Dev

Communication between several php docker containers

分類Dev

How to cleanup docker containers and images on linux machines

分類Dev

Windows Server Core Docker Containers networking problem

分類Dev

Starting Docker containers with combined list for volumes with ansible

分類Dev

NGinx and multiple docker containers but one subdomain

分類Dev

permission denied in containers after moving docker home

分類Dev

Automatic provisioning of Open stack VM for Docker containers

分類Dev

Virtual serial port between docker containers

分類Dev

docker0、Docker Bridge Driver、Containersの関係

分類Dev

Docker Volume without linking. What is the use case?

分類Dev

Docker: Linking Spring Boot container with Mongo DB container

Related 関連記事

  1. 1

    Docker Not Linking Containers

  2. 2

    How to list containers in Docker

  3. 3

    Building Docker Containers in VSTS

  4. 4

    Docker compose linking appears to not work

  5. 5

    Log management of various docker containers

  6. 6

    Multiple Docker containers, same image, different config

  7. 7

    How to assign domain names to containers in Docker?

  8. 8

    docker-compose up for only certain containers

  9. 9

    Multiple Docker containers, same image, different config

  10. 10

    Multiple Docker containers, same image, different config

  11. 11

    Efficiently using multiple docker containers in a single host

  12. 12

    Start Docker Containers In Specific Order After Reboot

  13. 13

    Is it possible to launch privileged docker containers on Amazon elasticbeanstalk?

  14. 14

    How to use local docker containers with Kubernetes

  15. 15

    How to run docker containers in their network with an external gateway?

  16. 16

    Connect to postgres in laravel using docker containers

  17. 17

    Docker container DNS alias available in other containers

  18. 18

    Link two docker containers on Windows 10

  19. 19

    Communication between several php docker containers

  20. 20

    How to cleanup docker containers and images on linux machines

  21. 21

    Windows Server Core Docker Containers networking problem

  22. 22

    Starting Docker containers with combined list for volumes with ansible

  23. 23

    NGinx and multiple docker containers but one subdomain

  24. 24

    permission denied in containers after moving docker home

  25. 25

    Automatic provisioning of Open stack VM for Docker containers

  26. 26

    Virtual serial port between docker containers

  27. 27

    docker0、Docker Bridge Driver、Containersの関係

  28. 28

    Docker Volume without linking. What is the use case?

  29. 29

    Docker: Linking Spring Boot container with Mongo DB container

ホットタグ

アーカイブ