Run multiple containers on same docker network localhost

I want to connect from my app to mongodb on localhost, so they need to have same localhost address.

So the question is: Can two containers share they localhost, or for each container the localhost ip must be different?

  • wercker.com connect to MySQL database
  • How to make Docker work on Debian Stretch
  • Docker DNS setup on VPS
  • Headless Chrome in Docker using Xvfb
  • My build does not include my web site directive
  • External Storage for Docker
  • I’m doing this for test environment purposes, so I don’t want in-memory database, changed mongo uri or any different solution. I just want to connect from A to B by localhost.

    To run my network and containers i type:

    docker network create --driver bridge isolated_nw
    docker run --name mongodb -d -p 27017:27017 --network=isolated_nw mongo:3.4.2
    docker run --name roomate-profiles --network=isolated_nw -d -p 8080:8080 sovas/roomate-profiles
    

    My custom docker network:

    [
        {
            "Name": "isolated_nw",
            "Id": "3efd6831784c2a8c9e9ea345144fcc6b9180e70c0e1b4b5d1a72219051b24e67",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "172.18.0.0/16",
                        "Gateway": "172.18.0.1/16"
                    }
                ]
            },
            "Internal": false,
            "Containers": {
                "57d4e2fb1f0c8d776329fd6ce82e5905df00e261ab6923595578dcb35913b03e": {
                    "Name": "roomate-profiles",
                    "EndpointID": "5a8158dc1aba6958218d1cca3c98ca911ab2cfa73be839ceece2e7819b244c91",
                    "MacAddress": "02:42:ac:12:00:03",
                    "IPv4Address": "172.18.0.3/16",
                    "IPv6Address": ""
                },
                "8fa815735d7ebb77434f8abf11e58f18faeb5d67e2743903d81f4600bd558c35": {
                    "Name": "mongodb",
                    "EndpointID": "7b7a7ed1ad08bbe381fb6d66c6e9fea66ee9b7c581f530bdf4d82f0741bff04b",
                    "MacAddress": "02:42:ac:12:00:02",
                    "IPv4Address": "172.18.0.2/16",
                    "IPv6Address": ""
                }
            },
            "Options": {},
            "Labels": {}
        }
    ]
    

    application.properties

    spring.data.mongodb.uri=mongodb://localhost:27017/admin
    

  • Docker Compose Error “TypeError: expected string or buffer” on docker:dind (ports)
  • Docker container can't connect to host application using IP whitelist
  • How to start redis-server and a node.js app on the same container?
  • Docker calling unit tests
  • docker image - centos 7 > ssh service not found
  • Accessing services on the Docker host
  • One Solution collect form web for “Run multiple containers on same docker network localhost”

    localhost won’t work since it refers to the roomate-profiles container. But you can do

    spring.data.mongodb.uri=mongodb://mongodb:27017/admin
    

    since both containers are connected to the same network. There is also no need to map the mongodb port to the host (unless you need it for something else).

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.