ossrs / srs

SRS is a simple, high-efficiency, real-time video server supporting RTMP, WebRTC, HLS, HTTP-FLV, SRT, MPEG-DASH, and GB28181.
https://ossrs.io
MIT License
24.7k stars 5.28k forks source link

http_hooks always connect to localhost on Docker compose #4048

Closed jimmydrinkscoffee closed 1 month ago

jimmydrinkscoffee commented 1 month ago

Version: ossrs/srs:5 Docker image

I run SRS locally via docker-compose for testing but the my http_hooks seems not to work.

http_hooks callbacks to my local server listening port 3000. However, there were no requests my server when I started to stream from OBS. And I was still able to do this even when I shutdowned my server.

Screenshot 2024-05-01 at 14 15 33

srs.conf

listen              1935;
max_connections     1000;
daemon              off;
srs_log_tank        console;
vhost __defaultVhost__ {
    http_hooks {
        enabled         on;
        on_publish      http://localhost:3000/live/start;
        on_unpublish    http://localhost:3000/live/end;
        on_play         http://localhost:3000/live/play;
        on_stop         http://localhost:3000/live/stop;
    }
}

compose.yaml

name: dev

services:
    redis:
        container_name: cache
        image: redis
        ports:
            - 6379:6379
        volumes:
            - redis:/data

    rtmp:
        container_name: rtmp
        image: ossrs/srs:5
        volumes:
            - ./srs.conf:/usr/local/srs/conf/srs.conf
        network_mode: host

volumes:
    redis:
        driver: local

I placed the srs.conf file in the same directory of compose.yaml and it's definitely mounted to the container.

$ docker exec -it rtmp /bin/bash    
$ root@orbstack:/usr/local/srs# cat /usr/local/srs/conf/srs.conf
listen              1935;
max_connections     1000;
daemon              off;
srs_log_tank        console;
vhost __defaultVhost__ {
    http_hooks {
        enabled         on;
        on_publish      http://localhost:3000/live/start;
        on_unpublish    http://localhost:3000/live/end;
        on_play         http://localhost:3000/live/play;
        on_stop         http://localhost:3000/live/stop;
    }
}
caesar-ibrahimovic commented 1 month ago

I just have solved similar problem, maybe you have the same. Check srs container logs with: docker container logs <container-id> There was red error lines like "connection refused" and there was ip 172.x.x.x. I made a conclusion that srs server in docker on windows has another ip sub net and it is trying send callback request to localhost backend server (127.0.0.1). So it can not to connect. I solved it with hosting backend on local network ip (for example 192.168.1.5) and srs server on VM with Ubuntu (with ip in same subnet 192.168.1.6) . I'm not an expert so maybe there is more simple decision

jimmydrinkscoffee commented 1 month ago

@caesar-ibrahimovic Very appreciate your response. However, you can see network_mode: host in my srs Docker compose config, which mean srs now is in the same net of my localhost server.

winlinvip commented 1 month ago

You should know what localhost means, it's the container itself, not your callback server.

jimmydrinkscoffee commented 1 month ago

I know what localhost is, fortunately and my problem is that even though my callback server isn't there (localhost:3000), I can still stream to the RTMP container. IIRC, it should fail unless the callback API responses with statusCode 2xx. In other words, my RTMP container didn't apply the config at path /usr/local/srs/conf/srs.conf as written in document.