How to set fixed ip address for container using docker-compose? -


problem

i connecting java program mysql every time ip adress of mysql container keep on changing. if changes, have update ip address in java program connection.(i have mentioned 172.17.0.2 in java program mysql container ip).

below simple jdbc java program

import java.sql.*; import java.sql.connection; import java.lang.*;  public class sample {     public static void main(string[] args)     {         connection conn = null;         statement stmt = null;          string sql= "select * student1;";    //insert student1 values(2,'kalam');          try         {             class.forname("com.mysql.jdbc.driver");         }         catch(classnotfoundexception e)         {             system.out.println(e);         }           try{             conn = drivermanager.getconnection("jdbc:mysql://172.17.0.2:3306/university", "root", "root");             stmt = conn.createstatement();              //stmt.execute(sql);             resultset rs= stmt.executequery(sql);             while(rs.next())             {                 system.out.println(rs.getint(1)+" "+rs.getstring(2));              }             system.out.println("value inserted");          }         catch(exception e)         {             system.out.println(e+"driver man");         }                 {              try{                 if(stmt!=null)                     stmt.close();                  if(conn!=null)                     conn.close();             }             catch(sqlexception e)             {                 system.out.println(" conn problem");                 e.printstacktrace();             }             }     } } 

below docker-compose

 mysql:    image: mysqlrep    container_name: mysqlcompose    environment:       - mysql_root_password= root    ports:       - "0.0.0.0:3306:3306"    command: bash -c "/etc/init.d/mysql start && cd /var/lib/mysql && mysql -u root && sleep 10"  java:    image: java:9    container_name: javacompose    links:       - mysql    command: bash -c "javac /compose/sample.java && java -cp "./compose:./compose/mysql-connector-java-5.1.5-bin.jar" sample"    volumes:        - ~/compose:/compose 

i using own image called mysqlrep in mysql service inside docker compose.(i builded mysqlrep image using dockerfile contains- mysql-server , created database university, table called student1, inserted values through my.sql file).

now have set fixed ip address mysql container can provide in java program connection(bcz reduces task client). how can set ?

note:

  • i using docker toolbox on windows 7

  • docker version:

    • client version: 1.11.1, os/arch: windows/amd64

    • server version: 1.11.2, os/arch: linux/amd64

  • docker-compose version: 1.7.1

please suggest...

you can use container alias mysql form connection url java container

drivermanager.getconnection("jdbc:mysql://mysql:3306/university", ... 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -