Docker Community Forums

Share and learn in the Docker community.

Port mapping in docker compose

I don’t know if this is the correct approach but I can access only one of the cameras even when I mapped to two different ports.

services:
  octoprint:
    image: octoprint/octoprint
    container_name: octoprint
    restart: always
    ports:
      - 8030:80
      - 8031:8080
      - 8032:8080
    devices:
      - /dev/ttyACM0:/dev/ttyACM1
    #  - /dev/CAMGP:/dev/CAMGP
    volumes:
      - /home/pi/containersdata:/octoprint
      - /home/pi/mynas/octoprint/uploads:/octoprint/octoprint/uploads
      - /home/pi/mynas/octoprint/timelapse:/octoprint/octoprint/timelapse
    # uncomment the lines below to ensure camera streaming is enabled when
    # you add a video device
    #environment:
    #  - CAMERA_DEV=/dev/CAMGP
    #  - ENABLE_MJPG_STREAMER=ture
    #  - MJPG_STREAMER_INPUT=-n -r 1280x720
     
  mjpg-streamergp:
    restart: always
    image: badsmoke/mjpg-streamer:1.0.1
    network_mode: service:octoprint
    container_name: webcamgp
    environment:
      - ENV_RESOLUTION=1920x1080
      - ENV_FPS=15
    devices:
      - /dev/CAMGP:/dev/video0
    depends_on:
      - octoprint  

  mjpg-streamern:
    restart: always
    image: badsmoke/mjpg-streamer:1.0.1
    network_mode: service:octoprint
    container_name: webcamnozle
    environment:
      - ENV_RESOLUTION=1280x720
      - ENV_FPS=20
    devices:
      - /dev/CAMN:/dev/video0
    depends_on:
      - octoprint```

That publishes/maps the container’s port 8080 as both 8031 and 8032 on the host. I didn’t know that this was even possible (assuming you’re not seeing any errors). I assume 8032 should be mapped to something else in the container?

Yep, possible, you can map as many host ports to a single container port as you want, thought each mapped port will delay the container start. Shouldn’t be a problem for a couple of ports, but definitly gets painfull when you try to map a range of 100 ports.

Let me make some assumptions: badsmoke/mjpg-streamer:1.0.1 binds port 8080 and since you run it twice and both hook into the network of service:octoprint, there is a colision and only one of the mjpg-streamer containers actualy is available. You will want to check if mjpg-streamer has a variable to modify the service port, change it and then modify one of the published ports accordingly.

All container use the same network namespace and thus the very same network interface and container ip. What is true for any sort of network interface ip remains true in this case: a port can only be bound once on a network ip.

1 Like

This should work just fine:

services:
  octoprint:
    image: octoprint/octoprint
    container_name: octoprint
    restart: always
    ports:
      - 8030:80
      - 8031:8031
      - 8032:8032
    devices:
      - /dev/ttyACM0:/dev/ttyACM1
    #  - /dev/CAMGP:/dev/CAMGP
    volumes:
      - /home/pi/containersdata:/octoprint
      - /home/pi/mynas/octoprint/uploads:/octoprint/octoprint/uploads
      - /home/pi/mynas/octoprint/timelapse:/octoprint/octoprint/timelapse
    # uncomment the lines below to ensure camera streaming is enabled when
    # you add a video device
    #environment:
    #  - CAMERA_DEV=/dev/CAMGP
    #  - ENABLE_MJPG_STREAMER=ture
    #  - MJPG_STREAMER_INPUT=-n -r 1280x720
     
  mjpg-streamergp:
    restart: always
    image: badsmoke/mjpg-streamer:1.0.1
    network_mode: service:octoprint
    container_name: webcamgp
    environment:
      - ENV_RESOLUTION=1920x1080
      - ENV_FPS=15
      - ENV_PORT=8031
    devices:
      - /dev/CAMGP:/dev/video0
    depends_on:
      - octoprint  

  mjpg-streamern:
    restart: always
    image: badsmoke/mjpg-streamer:1.0.1
    network_mode: service:octoprint
    container_name: webcamnozle
    environment:
      - ENV_RESOLUTION=1280x720
      - ENV_FPS=20
      - ENV_PORT=8032
    devices:
      - /dev/CAMN:/dev/video0
    depends_on:
      - octoprint
1 Like

Awesome Thank you very much it work!!!