Running two different services on the same port is generally not possible because a port can only be bound to one service at a time. However, there are a few strategies you can use to achieve similar functionality:
-
Reverse Proxy: Use a reverse proxy like Nginx or Traefik to route traffic to different services based on the request path or subdomain. This way, you can have both services listening on different ports internally, but externally they appear to be on the same port.
-
Multiplexing: Use a multiplexer that can inspect the incoming traffic and route it to the appropriate service based on the protocol or other criteria. This is more complex and less common.
Here's an example of how you can use Nginx as a reverse proxy to achieve this:
Step 1: Configure Docker Compose
Update your docker-compose.yml to include Nginx as a reverse proxy:
version: '3.8'
services:
soketi:
image: 'quay.io/soketi/soketi:1.6.1-16-alpine'
container_name: dev-soketi
environment:
SOKETI_DEBUG: '0'
SOKETI_DEFAULT_APP_ID: '${PUSHER_APP_ID}'
SOKETI_DEFAULT_APP_KEY: '${PUSHER_APP_KEY}'
SOKETI_DEFAULT_APP_SECRET: '${PUSHER_APP_SECRET}'
SOKETI_PORT: '6001'
ports:
- 6001:6001
networks:
- sail
web:
image: 'your-web-service-image'
container_name: dev-web
ports:
- 8080:80
networks:
- sail
nginx:
image: nginx:latest
container_name: dev-nginx
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- sail
networks:
sail:
driver: bridge
Step 2: Configure Nginx
Create an nginx.conf file in the same directory as your docker-compose.yml:
events {}
http {
upstream soketi {
server dev-soketi:6001;
}
upstream web {
server dev-web:80;
}
server {
listen 80;
location /soketi/ {
proxy_pass http://soketi;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Explanation
-
Nginx Configuration: The Nginx configuration defines two upstream services:
soketiandweb. It listens on port 80 and routes requests based on the path.- Requests to
/soketi/are proxied to the Soketi service. - All other requests are proxied to the web service.
- Requests to
-
Docker Compose: The
nginxservice is added to the Docker Compose file, and it shares the same network (sail) with the other services. This allows Nginx to communicate with the other containers using their service names.
Running the Setup
- Ensure your
nginx.conffile is in the same directory as yourdocker-compose.yml. - Run
docker-compose up -dto start the services.
With this setup, you can access your web service at http://localhost and your Soketi service at http://localhost/soketi/.
This way, you effectively have two services running on the same external port (80), but they are differentiated by the request path.