title: Docker: Client Server Program each running as a Container Not Able to Communicate with each Other Using POSIX Socket API
body: <p>Issue Description with Steps to Reproduce:
  I have a Server program written in C++ ,using posix Socket API. The Dockerfile used for building the Server image is:</p>
  
  <pre><code>$ cat Dockerfile
  FROM gcc:4.9
  WORKDIR /home/vikrana
  COPY . .
  EXPOSE 1001
  RUN g++ -o testcplusplus testprogram.cpp
  ENV PATH /home/vikrana:$PATH
  ENTRYPOINT ["testcplusplus", "1001"]
  </code></pre>
  
  <p>Below is the server program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;cstring&gt;
  #include &lt;cstdlib&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;errno.h&gt;
  
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if(const char* env_p = std::getenv("PATH"))
        std::cout &lt;&lt; "Your PATH is: " &lt;&lt; env_p &lt;&lt; '\n';
    if(argc &lt; 2)
    {
        cout&lt;&lt;"Number of arguments not correct\n";
        exit(1);
    }
    cout&lt;&lt;argv[1]&lt;&lt;"\n";
    cout&lt;&lt;"Creating socket\n";
    struct protoent *proto;
    proto = getprotobyname("tcp");
    sockfd=socket(AF_INET, SOCK_STREAM, proto-&gt;p_proto);
    cout&lt;&lt;"Socket created successfully\n";
  
    portno=atoi(argv[1]);
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr("0.0.0.0");
    cout&lt;&lt;"Port No:"&lt;&lt;portno&lt;&lt;"\n";
    serv_addr.sin_port=htons(portno);
    cout&lt;&lt;"Starting binding socket to a port\n";
    if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
              sizeof(serv_addr)) &lt; 0)
    {
        cout&lt;&lt;"Error while binding\n";
        exit(1);
    }
    cout&lt;&lt;"Bind is successful\n";
    cout&lt;&lt;"Listening to port\n";
    int x =listen(sockfd,5);
    if(x&lt;0)
    {
     cout&lt;&lt;"Error on listening to port 1001\n";
     cout&lt;&lt;errno&lt;&lt;"\n";
     cout&lt;&lt;strerror(errno)&lt;&lt;"\n";
     close(sockfd);
     exit(1);
   }
   clilen=sizeof(cli_addr);
   cout&lt;&lt;"Accepting connection\n";
   newsockfd=accept(sockfd,(struct sockaddr *)&amp;cli_addr,&amp;clilen);
   if(newsockfd &lt;0)
   {
      cout&lt;&lt;"Error while accepting a new connection\n";
      close(sockfd);
      exit(1);
   }
   cout&lt;&lt;"Accepted the connection\n";
   while(true)
   {
      memset(&amp;buffer,0,sizeof(buffer));
      cout&lt;&lt;"Reading from socket to buffer\n";
      n=read(newsockfd,buffer,255);
      if(n&lt;0)
      {
          cout&lt;&lt;"Erro reading from socket\n";
          close(sockfd);
          close(newsockfd);
          exit(1);
      }
      cout&lt;&lt;"Message received to the server:"&lt;&lt;buffer&lt;&lt;"\n";
      n=write(newsockfd,"I got your message\n",19);
      if(n&lt;0)
      {
         cout&lt;&lt;"Error writing into socket\n";
         close(sockfd);
         close(newsockfd);
         exit(1);
      }
    }
   close(sockfd);
   close(newsockfd);
   return 0;
   }
  </code></pre>
  
  <p>Following is the Docker command I am using to run my server program in the container:</p>
  
  <pre><code> $ docker run --net=user_def_nw -ti -P --name server1 my-server-app
  </code></pre>
  
  <p>On running above command the container gets attached with user_def_nw and an IP address is assigned to the container.</p>
  
  <p>On the other hand following are the details of my Client program written in C++ using posix Socket API.
  DockerFile to create client image:</p>
  
  <pre><code>$ cat Dockerfile
  FROM gcc:4.9
  WORKDIR /home/vikrana
  COPY . .
  RUN g++ -o testcplusplus testclientprogram.cpp
  ENV PATH /home/vikrana:$PATH
  ENTRYPOINT ["testcplusplus", "0.0.0.0", "1001"]
  </code></pre>
  
  <p>Below is the code for the client program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;cstring&gt;
  #include &lt;cstdlib&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;netdb.h&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {
    int sockfd, newsockfd, portno;
    struct sockaddr_in server;
    char message[256], server_reply[2000];
    if(const char* env_p = std::getenv("PATH"))
            std::cout &lt;&lt; "Your PATH is: " &lt;&lt; env_p &lt;&lt; '\n';
    struct protoent *proto;
    proto = getprotobyname("tcp");
    sockfd=socket(AF_INET, SOCK_STREAM, proto-&gt;p_proto);
    cout&lt;&lt;"socket created\n";
  
    cout&lt;&lt;argv[1]&lt;&lt;"\n";
    cout&lt;&lt;argv[2]&lt;&lt;"\n";
    portno=atoi(argv[2]);
    cout&lt;&lt;portno&lt;&lt;"\n";
    server.sin_family=AF_INET;
    server.sin_addr.s_addr=inet_addr(argv[1]);
    server.sin_port=htons(portno);
    memset(server.sin_zero, '\0', sizeof server.sin_zero);
  
    cout&lt;&lt;"connecting to server\n";
     int x =connect(sockfd, (struct sockaddr *)&amp;server, sizeof(server));
     cout&lt;&lt;"Connect Return Code:"&lt;&lt;x&lt;&lt;"\n";
    if(x&lt;0)
    {
        cout&lt;&lt;"Connect Failed\n";
        cout&lt;&lt;errno&lt;&lt;"\n";
        cout&lt;&lt;strerror(errno)&lt;&lt;"\n";
        exit(1);
    }
    cout&lt;&lt;"Connected\n";
    while(1)
    {
        cout&lt;&lt;"Enter Message:";
        cin&gt;&gt;message;
  
        if(send(sockfd,message,strlen(message),0) &lt; 0)
        {
            cout&lt;&lt;"Send Failed\n";
            exit(1);
        }
        sleep(2);
  
        if(recv(sockfd,server_reply,2000,0) &lt; 0)
        {
           cout&lt;&lt;"Recv Failed\n";
           break;
        }
  
        cout&lt;&lt;"Server Reply:"&lt;&lt;server_reply&lt;&lt;"\n";
       }
    close(sockfd);
     return 0;
   }
  </code></pre>
  
  <p>Following is the Docker command I am using to run my client program in the container:</p>
  
  <pre><code> $ docker run --net=user_def_nw -ti --name cli1 my-client-app
  </code></pre>
  
  <p>On running the above command it is noticed Client is not able to connect to the server listening at port 1001.</p>
  
  <p>The error code return my the connect API is "111" which means, connection refused.</p>
  
  <p>My understanding is since both the containers namely server1 and cli1 are attached to the same user defined network "user_def_nw" bridge network, they should be able to communicate with each other over Socket. But here connect API itself is failing to establish connection with the Server.</p>
  
  <p>Also, I tried the following
  1) I ran my client server program as a normal process in a given Linux system, it works perfectly fine.</p>
  
  <p>2) I made my base image as ubuntu and top of it installed g++ compiler, and then ran my client and server program as independent containers, still the same error as reported above I am getting.</p>
  
  <p>Please help me in resolving this issue.</p>