PowerLove
PowerLove

Reputation: 303

configuring docker container ip for redis host

I'm trying to configure redis for docker using docker-compose. So the redis container is running, If I want to connect to the container that has redis running in nodeJs server, what ip address should i use?

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
aa3adc6041aa        redis               "docker-entrypoint..."   About an hour ago   Up 14 minutes       0.0.0.0:6379->6379/tcp   redis

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis
172.18.0.2

Does this mean that I should set the env redis host variable to 172.18.0.2? will the variable change on a different machine?

Upvotes: 1

Views: 6720

Answers (1)

larsks
larsks

Reputation: 312838

You want to avoid using ip addresses, because they will differ between machine and between different runs of your docker-compose environment.

If you are starting your containers using docker-compose, then you can refer to other containers in the same file by name. In other words, if you have:

version: '3'

services:
  redis:
    image: redis

  nodejs_app:
    image: myapp

Then you can just set your redis host variable to redis.

Upvotes: 3

Related Questions