title: Docker communication between two containers with Java
body: <p>There are two java files, Server.java and Client.java.
  Both are in separate containers.</p>
  
  <p><strong>DOCKER FILES:</strong>
  The dockerfile(in the folder named 'Server') that i use for server is:</p>
  
  <pre><code>FROM java:8
  COPY Server.java /
  RUN javac Server.java
  EXPOSE 25000
  ENTRYPOINT ["java"]
  CMD ["Server"]
  </code></pre>
  
  <p>The dockerfile(in the folder named 'Client') for client is:</p>
  
  <pre><code>FROM java:8
  COPY Client.java /
  RUN javac Client.java
  EXPOSE 25000
  ENTRYPOINT ["java"]
  CMD ["Client"]
  </code></pre>
  
  <p><strong>BUILDING THE CONTAINERS:</strong>
  Building the containers using <code>sudo docker build -t serverimage .</code></p>
  
  <p><strong>RUNNING THE CONTAINERS:</strong>
  I run the images using the command <code>sudo docker run &lt;IMAGE_NAME&gt;</code>.And I am running the serverimage first.</p>
  
  <p><strong>The error i am getting(When i run clientimage) is:</strong></p>
  
  <pre><code>java.net.ConnectException: Connection refused: connect
  </code></pre>
  
  <p><strong>Server.java File:</strong></p>
  
  <pre><code>import java.io.*;  
  import java.net.*;  
  public class MyServer {  
     public static void main(String[] args){  
         try{  
             ServerSocket ss=new ServerSocket(25000);  
             Socket s=ss.accept();//establishes connection   
             DataInputStream dis=new DataInputStream(s.getInputStream());  
             String  str=(String)dis.readUTF();  
             System.out.println("message= "+str);  
             ss.close();  
         }catch(Exception e){System.out.println(e);}  
     }  
  }  
  </code></pre>
  
  <p><strong>Client.java File:</strong></p>
  
  <pre><code>import java.io.*;  
  import java.net.*;  
  public class MyClient {  
     public static void main(String[] args) {  
        try{      
           Socket s=new Socket("localhost",25000);  
           DataOutputStream dout=new DataOutputStream(s.getOutputStream());  
           dout.writeUTF("Hello Server");  
           dout.flush();  
           dout.close();  
           s.close();  
        }catch(Exception e){System.out.println(e);}  
     }  
  }  
  </code></pre>
  
  <p>What am I possibly doing wrong?
  Thank you!</p>