大网数据广告活动
服务器运维问答

Expose a Unix socket to the host system from inside from a Docker container

· 52CE 我爱测

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 syste…

问题原文

原帖链接 · Drazisil · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · J.M. Robles · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-11-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。