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 to run protractor end to end tests in docker container?
  • Use nginx as proxy with docker
  • GCloud: Copying Files from Local Machine into a Docker Container
  • Restarting machines in a docker swarm on AWS
  • Docker Entry Point with Script Arguments
  • Running migrations with Rails in a Docker container with multiple container instances
  • 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
    

  • Java library for Docker repository?
  • Using command output as environment variable value?
  • Docker in AWS Elastic Beanstalk: executable file not found in $PATH"
  • how I can execute scripts on docker-machine create
  • Seeding data in a Mongo db within a linked Docker container
  • How to run docker image as a non-root user?
  • 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.