Hey… I am just a beginner to Docker and was exploring the various features. I want to run a simple client server program in docker within java.
So I just used a Client.java and Server.java and then using those I built the docker images.
The code within java files are as below:
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String msg = "Hello from Client";
String sendMessage = msg + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
The code for Server.java is:
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);
String returnMessage = "Hello from Server";
String sendreturnMessage = returnMessage + "\n";
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(sendreturnMessage);
bw.flush();
System.out.println("Message sent to the client is "+returnMessage);
System.out.println("\nProcess Complete\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
Then as the next step I built the image files for both of these using the Dockerfile as
Dockerfile for building serverimage
FROM java:8
COPY Server.java /
RUN javac Server.java
EXPOSE 25000
ENTRYPOINT ["java"]
CMD ["Server"]
Dockerfile for building clientimage
FROM java:8
COPY Client.java /
RUN javac Client.java
EXPOSE 25000
ENTRYPOINT [“java”]
CMD [“Client”]
Then I built is using
docker build -t serverimage:v1 .
and
docker build -t clientimage:v1 .
Then I tried to run these docker images however the serverimage started, but the client image threw an exception and failed to start up
docker run -d --name server serverimage:v1
docker run --name client clientimage:v1
I also tried to run the clientimage as:
docker run --name client --link server:jcs clientimage:v1
Could someone help me out to resolve this issue? I just want to try out the communication between two containers in the same docker for now?
Thanks