Unable to achive the IPC socket communication between containers on rcar h3 bboard

Hello,
I am trying to achive the IPC socket communication between containers on rcar h3 bboard. I have created crosscompile containers using buildx. When I try running it containers created but communication not happening.

here is the dockerfile :

  1. client:
FROM alpine
RUN apk add build-base 
WORKDIR /app
RUN apk add --no-cache g++
COPY client.cpp .
#RUN g++ client.cpp -o client
RUN g++ "-DARCH=\"`uname -a`\"" client.cpp -o client
CMD ["./client"]

2.server

FROM alpine
RUN apk add build-base 
WORKDIR /app
RUN apk add --no-cache g++
COPY server.cpp .
#RUN g++ server.cpp -o server
RUN g++ "-DARCH=\"`uname -a`\"" server.cpp -o server
CMD ["./server"]

I have used opensource available sample code for client and server :

client.cpp

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>

#define PORT 8080

int main() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    struct hostent* server;

    // Get server address using hostname
    server = gethostbyname("container2");
    if (!server) {
        std::cerr << "No such host found" << std::endl;
        return 1;
    }

    // Create socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return 1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    memcpy(&serv_addr.sin_addr, server->h_addr_list[0], server->h_length);

    // Connect to server
    if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection failed" << std::endl;
        return 1;
    }

    // Send message
    send(sock, "Hi, I'm container1", 18, 0);
    
    char buffer[1024] = {0};
    read(sock, buffer, 1024);
    std::cout << "Received from server: " << buffer << std::endl;

    close(sock);
    return 0;
}


server.cpp :

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#define PORT 8080

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    // Create socket
    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == 0) {
        std::cerr << "Socket failed" << std::endl;
        return 1;
    }

    // Bind to port 8080
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);
    
    if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
        std::cerr << "Bind failed" << std::endl;
        return 1;
    }

    // Listen for incoming connections
    if (listen(server_fd, 3) < 0) {
        std::cerr << "Listen failed" << std::endl;
        return 1;
    }

    std::cout << "Server listening on port 8080..." << std::endl;

    // Accept connection
    new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
    if (new_socket < 0) {
        std::cerr << "Accept failed" << std::endl;
        return 1;
    }

    read(new_socket, buffer, 1024);
    std::cout << "Received: " << buffer << std::endl;

 // Send response
    send(new_socket, "Hello", 5, 0);
    std::cout << "Response sent" << std::endl;

    close(new_socket);
    close(server_fd);
    return 0;
}

Using below command to build the containers :

sudo docker buildx build --platform linux/arm64 -t server:arm64 --load -f server/server.Dockerfile ./server
sudo docker buildx build --platform linux/arm64 -t client:arm64 --load -f client/client.Dockerfile ./client

After shifting these images to board, I am loading and using below commands to run them on board ":

docker run -d  --name container1 --network mybridge client:arm64
docker run -d  --name container2 --network mybridge server:arm64

also I used below commands :

docker run -d  --name container2 --network mybridge -p 8080:80 server:arm64
docker run -d  --name container1 --network mybridge client:arm64

but none of them is helping me to achive communication.
On runnig client both server and client will be closed/stopped.

As we test on host it is correct once comm is done they stop. Issue here is we are not able to see logs that we are passing i.e printf stats from .cpp files.

for example on linux host we see below message on server part :
image

and below on client :
image

we are missing this part here on board.

Can anybody help me on this as I am trying this ofr the first time.Any suggestion would help me a lot.

regards,
Siddhartha V

1 Like

It seems you are trying to create a TCP/IP connection between containers. I would create an explicit Docker network, attach both containers to it. Then address container via the container name, internal Docker DNS will translate to appropriate IP.

Hi @ bluepuma77 ,

I have created a bridge “mybridge” and attaching the container while running it. by passing --network=mybridge.

by checking docker inspect mybridge it shows container2 i.e server is attached to bridge:

root@h3ulcb:~# docker inspect mybridge
[
    {
        "Name": "mybridge",
        "Id": "e32f41d52124062f49c6fa74e985210a45d3c708abe39dab2efd56d0e0c613c2",
        "Created": "2022-04-28T20:18:24.317931859Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "7fe85f4148a11836f7f9159532fcb1da21fe0bb9ecbf2deb19d64c4767ca2d13": {
                "Name": "**container2**",
                "EndpointID": "e99fff3a8eaa909ea1b659e67725cc1fd8719aff1a50fcc7947fe4d0ba1b2e87",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

On runnig client both server and client will be closed/stopped.

As we test on host it is correct once comm is done they stop. Issue here is we are not able to see logs that we are passing i.e printf stats from .cpp files.

for example on host we see below message on server part :
image

and below on client :
image

we are missing this part here on board.

If the containers should be able to talk to each other, you should attach both to the Docker network. It seems only one is attached.

For logs you can use docker logs -f <container-name> (doc).

Hi,

Actually communication was happening but I was expecting the printf on console which didn’t happen. Later I ran containers in such a way that docker logs are written into a file. Later on inspecting the file I saw the clear communication logs.

Below is the run command :slight_smile:

docker run -v /$(pwd):/home/root/logs/outputlogs.log -d --name container2 --network mybridge server_binary:arm64 -c "while true; do date > /home/root/logs/outputlogs.log; sleep 1; done"

docker run -v /$(pwd):/home/root/logs/outputlogs.log -d --name container1 --network mybridge client_binary:arm64 -c "while true; do date > /home/root/logs/outputlogs.log; sleep 1; done"

The logs will be stored in the /var/lib/docker/containers/container_id/container_id-json.log file on the host, where container_id is the unique ID of your container.

Thanks for your time @bluepuma77 .

best regards,
Siddhartha V

docker logs should work, you should not need to mess with internal Docker files.

One issue might be that you need to flush the output in your program, sometimes small output is stored in memory before being written to disk.

In Python you would use something like:

print("Hello, world!", flush=True)

ok. will check that option too.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.