Search code examples
javamongodbdockercontainersibm-cloud

Bluemix create container group linking to another container


We have a Java application running with MongoDB, each one in a different Bluemix container. Both are SINGLE Bluemix containers.

We want to serve the Java app using one of our subdomains: https://subdomain.mydomain.com, which is already pointing to Bluemix. How can we do it?

OUR APPROACH

Because the Java container needs to link to the Mongo container, we created both containers programmatically (we didn't find in the UI a way to link a container to another container) like this:

sudo bluemix ic run --name mongo-container -p 27017 -m 128 registry.eu-gb.bluemix.net/mycompany/mongo

sudo bluemix ic run --name java-container --link mongo-container:mongo -p 8080 -m 128 registry.eu-gb.bluemix.net/mycompany/java

This works well, but the Java app is only accessible through an ugly Blumix IP, not through https://subdomain.mydomain.com as we want.

What about using a Bluemix container GROUP (SCALABLE container in the UI)?

Again, we don't know how to link containers from the UI, so it should be something like

sudo bluemix ic group-create --auto --name java-scalable -p 8080 -m 128 --hostname subdomain --domain mydomain.com registry.eu-gb.bluemix.net/mycompany/java

BUT according to the documentation we cannot link a container group to a container, since there is no --link parameter.

Back to the original question. How can we serve the Java app using https://subdomain.mydomain.com?


Solution

  • The link option basically creates environment variables in one container to reach the other one.

    You can do the same with your scalable container in Bluemix.

    Here are the steps I did:

    1) Create your MongoDB container:

    bx ic run --name ads-mongo -p 27017 -m 128 registry.ng.bluemix.net/namespace/mongo 
    

    2) Inspect the MongoDB container to find the private IP address:

    bx ic inspect ads-mongo
    

    The private IP will be at end of the output, I am adding just part of the output below for brevity:

      "Ports": {
                    "27017/tcp": [
                        {
                            "HostIp": "172.31.0.235",
                            "HostPort": "27017"
                        }
                    ]
                },
                "PublicIpAddress": ""
    

    3) Run your scalable container and include one or more environment variables with your MongoDB credentials. Make sure you change your Java code to get the credentials from the environment variables you are passing to the scalable container:

    bx ic group-create --name ads-node -e "MONGO_URI=mongodb://172.31.0.235:27017" -p 3000 -m 128 --hostname ads-node --domain mybluemix.net registry.ng.bluemix.net/namespace/ads-nodebx
    

    In my test I used a Node.js application and it reads the MONGO_URI environment variable for the MongoDB credentials.

    You can assign a public IP for your MongoDB container as well if desired, the end result should be similar. The only difference I see is that you can then access your db using mongo command line or other tools to connect to the database.