简体   繁体   中英

Configuring application in docker container to access other containers

How can you configure an app deployed as a docker container to reference components running in other containers? I have a node app that requires postgresql. I have a node config file that contains all the connection information for postgres. In a non-docker deployment, you simply set the required config values (eg IP address of the db server etc), but how can this be done in a docker deployment?

I'm using Docker Compose, but can't see how I can find out the necessary IP address of the postgres docker container and then set that value in my node.js config.

You don need to get the IPs.

You can reach out a container from another container in the same environment (same Docker Compose) by using the "service" name instead of the ip.

Using domain name

To connect to your application you don't need the IP, you just need container name.

I will try to explain it on below example.

If you have eg docker-compose file like below:

version: '2'
services:
  web:
    build: docker/web
    ports:
      - "8080:8080"
    links:
      - dbpostgres 
    volumes:
      - .:/var/www/html   # I will share my code so I map this path
  dbpostgres: <-- CONTAINER NAME
    image: postgres
    volumes:
      - /private/var/lib/postgresql:/var/lib/postgresql
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: pguser
      POSTGRES_PASSWORD: pguser
      POSTGRES_DB: pgdb

and you want connect to postgres database, all you need to do is use container name instead of the IP address. Here dbpostgres

In your config file you should use in this case:

parameters:
    database_host: dbpostgres <-- CONTAINER NAME
    database_port: null
    database_name: dbname
    database_user: dbuser
    database_password: pass

Using IP

If you don't want to use domain name, and want use the IP (I don't know the reason), you can use command:

docker inspect project_dbpostgres_1

where project_dbpostgres_1 is your container name. You can check container name using docker ps .

docker-inspect in section NetworkSettings has key Networks. You should look for IPAddress. There is also Aliases where you can find alias name dbpostgres.

 ... "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "7005a7782180462e31e526aa9a5ad89e1943230d57c33930c9f7de26f3917157",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "9000/tcp": null
        },
        "SandboxKey": "/var/run/docker/netns/7005a7782180",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "MacAddress": "",
        "Networks": {
            "i2mv3_default": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "31b7550ecd14",
                    "dbpostgres"
                ],
                "NetworkID": "e1a954361a2a325cac15405135d67e716dff4c15ea10d402948bf7dff329c1fa",
                "EndpointID": "9b2ad968df4e55750fe1d1730e33fafa5512012b1669df97d838abf0f25fa506",
                "Gateway": "172.18.0.1",
                "IPAddress": "172.18.0.2", <---- YOUR IP
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:12:00:02"
            }
        }
    }  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM