Pass container IP across containers for config file

I’m new to Docker, and am attempting to create two containers, one for MySQL and one for my Node.js app, based on images from Docker’s HUB.

I’d like to connect my node app to the MySQL host.

  • How is “lxd” different from lxc/docker?
  • Containers can not connect after docker update
  • memory and cpu management on docker containers
  • Running VirtualBox in Ubuntu in Mac OSX, won't start up
  • My docker was stuck by down whalesay, what should I do?
  • automation script for postgresql
  • For this, I’m planning to store informations about this host through environment variables in my config.yaml (used by node-config) for later use.

    My question is : how could I pass the IP address of the MySQL container to my node.js app?

    It is dynamically attributed, I vaguely know I can retrieve it with a command as docker inspect $(docker ps -q) | grep '"IPAddress"', it may be a clue?


    Docker-compose.yml :

    mysql:
      image: mysql:5.6
      environment:   
        - MYSQL_ROOT_PASSWORD=****
        - MYSQL_DATABASE=database
        - MYSQL_USER=user
        - MYSQL_PASSWORD=****
      volumes:
        - /data/mysql:/var/lib/mysql
    
    nodeapp:
      build: .
      environment:
        - MYSQL_USER=^^^^^ // will mirror the value up there, and so on
        - ...
      ports:
        - "80:3000"
      links:
        - mysql
    

    Config.yaml :

    app:
    
      port: 3000
    
      db:
        host: "MYSQL_HOST" // How can this be dynamic ?
        port:"MYSQL_PORT"
        database: "MYSQL_DATABASE"
        user: "MYSQL_USER"
        password: "MYSQL_PASSWORD"
    

    Dockerfile :

    FROM    node:0.10
    
    ADD package.json /usr/src/package.json
    # Install app dependencies
    RUN cd /usr/src && npm install
    
    ADD . /usr/src
    WORKDIR /usr/src
    
    EXPOSE  3000
    
    CMD npm start
    

  • Setting Team City Build Agent Port Number in Marathon
  • Docker build, how to show progress?
  • Need to know how to use Groovy to automate a Docker build & runtime
  • Nginx: preserve port during redirect
  • GitLab CI runner - can't access other repository
  • Setting up CD for a Ruby on Rails project with Bitbucket Pipelines and Docker
  • One Solution collect form web for “Pass container IP across containers for config file”

    When you run containers with docker-compose, it adds in every container in /etc/hosts a dynamic line with the format

    <dynamic ip> container_name
    

    Thus, when you run docker-compose up every container knows any other container by its name.

    So, in your confing.yaml you have to change this line

     host: "MYSQL_HOST" // How can this be dynamic ?
    

    with

    host: mysql
    

    since in /etc/hosts you have the dynamic association between the hostname mysql and the dynamic ip assigned by docker the the MySQL container.

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