12

I would like to run aweb server inside a Docker container that listens to a Unix socket, instead of to a port. I'm finding a lot of results on sharing the Docker socket, but I don't think that's what I want.

I want the host system to be able to connect to the Unix socket that's being listened to inside the container.

I'm using docker-compose, so the usual way of using -v doesn't work.

My ngix site config:

server {                                                                                             
   listen 80; listen [::]:80;                                                            
    server_name foo.com www.foo.com;

    location / {                                                                         
        proxy_pass http://unix:/var/lib/docker/volumes/app_shared/_data/app.https.sock:;       
        proxy_set_header Host $http_host;                                                            
        proxy_http_version 1.1;                                                                      
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                 
        proxy_set_header X-Forwarded-Proto https;                                                    
    } 

docker-compose.yml

version: '3'
services:      
  web:
    build: .
    volumes:
            - shared:/var/app
    command: "npm start"
volumes:
   shared:

Yet it says I can't connect to the socket, even though it exists at this location on the host.

Drazisil
  • 260
  • 1
  • 2
  • 8

1 Answers1

14

You can communicate the host machine or even two containers using the same socket. In the general case of two containers, you should start the two dockers from the host machine sharing a volume that should be created in the host machine.

docker run --name "containerA" -v /var/run/common_socket_dir:/var/run/common_socket_dir...
docker run --name "containerB" -v /var/run/common_socket_dir:/var/run/common_socket_dir...

Both, containerA and containerB can now use sockets as /var/run/common_socket_dir/socketX to interconnect. Of course, the host machine could use the sockets in this volume to communicate with containers

J.M. Robles
  • 865
  • 6
  • 9
  • I've been waiting to try your answer before accepting it or upvoting, but I forgot to mention I'm using docker-compose. I'll adjust my question, sorry. – Drazisil Nov 07 '17 at 01:55
  • 1
    Please, take a look to [this site](https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket/) – J.M. Robles Nov 07 '17 at 05:04
  • Here's an archive link https://web.archive.org/web/20210411145047/https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket/ – Drazisil Aug 07 '21 at 17:36