How can I make my local client-server application communicate on a local socket using docker compose?

I have my client-side code like this where the constant SOCKET_PATH is “/tmp/my_socket”:

#define SOCKET_PATH "/tmp/my_socket"
//rest of the code

int main(int argc, char *argv[])
{
    struct sockaddr_un server_addr;

    server_addr.sun_family = AF_LOCAL; /*socket locali*/
    strcpy(server_addr.sun_path, SOCKET_PATH);

    if ((socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
    {
        perror("Errore creazione welcoming socket");
        return 1;
    }

    if ((connect(socket_fd,
                 (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0))
    {
        perror("Errore connect");
        return 1;
    }

    clientActions();

    /*Pulizia prima della terminazione*/
    close(socket_fd);
    return 0;
}

My server-side is like this:

#define SOCKET_PATH "/tmp/my_socket"
//rest of the code

int main(int argc, char* argv[]) {
    int socket_fd, conn_fd;
    struct sockaddr_un server_addr, client_addr;

    server_addr.sun_family=AF_LOCAL; /*socket locali*/
    unlink(SOCKET_PATH); /*Rimuove la socket se già esiste*/

    strcpy(server_addr.sun_path, SOCKET_PATH);

    printf("Server in esecuzione...\n");
    sleep(1);

    if ((socket_fd=socket(PF_LOCAL,SOCK_STREAM,0)) < 0){
        perror("Errore creazione welcoming socket");
        return 1;
    }
    
    if ((bind(socket_fd,
    (struct sockaddr*)&server_addr,sizeof(server_addr))<0)) {
        perror("Errore bind strano");
        return 1;
    }

    if ((listen(socket_fd, 5)) < 0){ /*5 è la dim della coda di attesa*/
        perror("Errore nel listen");
        return 1;
    }

    while (1) {
        if ((conn_fd = accept(socket_fd,NULL,NULL)) < 0) {
            perror("Errore accept");
            return 1;
        }
        addClientToList(conn_fd);
        pthread_t tid;
        int* conn_fd_ptr = malloc(sizeof(int));
        *conn_fd_ptr = conn_fd; //Per evitare problemi con var condivise
        // TODO: gestione thread
        if (pthread_create(&tid, NULL, connection_handler, conn_fd_ptr) != 0) {
            perror("Errore creazione thread");
            free(conn_fd_ptr);
            close(conn_fd);
            removeClientFromList(conn_fd);
            continue;
        }

        pthread_detach(tid);
    }

    /*Pulizia prima della terminazione*/
    close(socket_fd);
    unlink(SOCKET_PATH);
    return 0;
}

Executing the code without docker using gcc compiler on my Ubuntu Virtual Machine works fine. But when I have to use docker compose something goes wrong about the server side binding. This is the Dockerfile.server:

FROM gcc:11.4

# Crea una cartella per l'app
RUN mkdir -p /opt/app
WORKDIR /opt/app

# Copia i file del server
COPY server.c /opt/app

# Compila il server e collega la libreria pthread
RUN gcc -o server.out server.c -lpthread

# EXPOSE 9200

CMD ["./server.out"]

This is the Dockerfile.client:

FROM gcc:11.4

# Crea una cartella per l'app
RUN mkdir -p /opt/app
WORKDIR /opt/app

# Copia i file del client
COPY client.c /opt/app

# Compila il client e collega la libreria pthread
RUN gcc -o client.out client.c -lpthread

CMD ["./client.out"]

And finally my docker compose is this:

version: '3'
services:
  server:
    build:
      context: .
      dockerfile: Dockerfile.server
    container_name: my_server
    # ports:
    #   - "9000:9100"  # Espone la porta del server sulla macchina host
    volumes:
      - /tmp/my_socket:/tmp/my_socket  # Condivide il socket file

  client:
    build:
      context: .
      dockerfile: Dockerfile.client
    container_name: my_client
    depends_on:
      - server  # Il client parte solo dopo che il server è pronto
    volumes:
      - /tmp/my_socket:/tmp/my_socket  # Usa lo stesso socket file

The error that docker gives me when I’m trying to do docker compose up --build is like this:

[+] Running 3/3
 ✔ Network progettotrislso_default  Created                                                                          0.1s 
 ✔ Container my_server              Created                                                                          0.2s 
 ✔ Container my_client              Created                                                                          0.3s 
Attaching to my_client, my_server
my_client  | Errore connect: Connection refused
my_client exited with code 1
my_server  | Server in esecuzione...
my_server  | Errore bind strano: Address already in use
my_server exited with code 1

I can understand the client error, but why the server error? On my ubuntu it works just fine, what is happening? Does that have to do with the docker volumes?

A unix socket is a special “file”. There is no way to mount it into a virtual machine and Docker Desktop runs everything in a virtual machine.

Why you see “already in used” error, I don’t know either as you don’t have port mapping in your shared compose file only the one that you commented out.