Skip to content

Commit

Permalink
Allow running multiple instances.
Browse files Browse the repository at this point in the history
Address #3
  • Loading branch information
AmedeeBulle committed Nov 29, 2020
1 parent 278e69b commit e0f701c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .env-distr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ WEBCAM_INPUT=input_raspicam.so -fps 5

# USB Webcam in 16:9 example
# WEBCAM_INPUT=input_uvc.so -d /dev/video0 -r 640x360 -fps 5

# Listen to the following ports:
# HTTP_PORT=80
# HTTPS_PORT=443
23 changes: 23 additions & 0 deletions .env-extra-distr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sample file for driving an extra printer
#
# Copy this file to .env-<printer name> -- e.g. .env.extra
# To start an additional instance run:
# docker-compose --project-name extra --env-file .env-extra up -d
# or use the convenience script:
# docker-compose-multi.sh extra up -d

# Architecture -- same as in your main .env file
OP_MACHINE_NAME=raspberrypi3

# Webcam configuration
# Ensure you use only one Raspberry Pi Camera for all instances.
# Supported configurations:
# - One Raspberry Pi Camera, One USB Camera
# - Two USB cameras
# If you do not have a camera, set "WEBCAM_START=false"
WEBCAM_START=false
WEBCAM_INPUT=input_uvc.so -d /dev/video0 -r 640x480 -fps 5

# Listen to the following ports -- Must be different for every instances!
HTTP_PORT=8080
HTTPS_PORT=8443
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Updates](#updates)
- [First run](#first-run)
- [Note about persistence](#note-about-persistence)
- [Multiple printers](#multiple-printers)

<!-- TOC END -->
# Introduction
Expand Down Expand Up @@ -234,3 +235,64 @@ docker-compose up -d
```

By doing this, you will loose any change made to the code, in particular if you installed plugins you will have to re-install them (but their configuration will be preserved).

# Multiple printers

Although driving multiple printers from the same Raspberry Pi is possible, it might lead to performance issues.
This setup is nevertheless easy to achieve with the _plain Docker_ setup.
It is a good solution if:

- You have a powerful Raspberry Pi
- You have multiple printers but use only one at a time

You can run any number of instances of this _container stack_ by using a different _project name_ and a different `.env` file.
The `docker-compose-multi.sh` convenience script is provided to simplify operations.

Assuming you already have a running instance, to add a new one simply copy the `.env-extra-distr` sample file to `.env-<printer name>`,
review the configuration parameters and start the instance with `./docker-compose-multi.sh <printer name> up -d`.

Main points of attention:

- Do not configure more than one Raspberry Pi camera!
(There is no limitation on the number of USB cameras)
- Ensure all instances have their own unique HTTP and HTTPS ports.

Sample session:

```shell
# Start the main instance
$ docker-compose up -d
Creating network "octoprint_default" with the default driver
Creating octoprint_octoprint_1 ... done
Creating octoprint_webcam_1 ... done
Creating octoprint_haproxy_1 ... done
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------
octoprint_haproxy_1 /usr/bin/entry.sh /opt/hap ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
octoprint_octoprint_1 /usr/bin/entry.sh /opt/oct ... Up 5000/tcp
octoprint_webcam_1 /usr/bin/entry.sh /opt/web ... Up 5200/tcp, 8080/tcp

# Start an additional instance for the "extra" printer:
$ cp .env-extra-distr .env-extra
$ vi .env-extra
$ ./docker-compose-multi.sh extra up -d
Creating network "extra_default" with the default driver
Creating volume "extra_octoprint_vol" with default driver
Creating extra_webcam_1 ... done
Creating extra_octoprint_1 ... done
Creating extra_haproxy_1 ... done
$ ./docker-compose-multi.sh extra ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------
extra_haproxy_1 /usr/bin/entry.sh /opt/hap ... Up 0.0.0.0:8443->443/tcp, 0.0.0.0:8080->80/tcp
extra_octoprint_1 /usr/bin/entry.sh /opt/oct ... Up 5000/tcp
extra_webcam_1 /usr/bin/entry.sh /opt/web ... Up 5200/tcp, 8080/tcp
```

Notes:

- As you can see in the above output, instances use a different network namespace as well as a different volume for storing data,
so they are completely separated and independent.
- Depending on when your printers / cameras are detected they might get different device names on your Raspberry Pi...
Always check that you are driving the right printer!
18 changes: 18 additions & 0 deletions docker-compose-multi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Wrapper to docker-compose for multiple environemnts

if [[ $# -lt 1 ]]; then
echo "Usage: $0 <env> <docker-compose args>" >&2
exit 1
fi

environ="$1"
shift
env_file=".env-${environ}"

if [[ ! -f "${env_file}" ]]; then
echo "$0: environment file ${env_file} does not exist" >&2
exit 1
fi

docker-compose --project-name "${environ}" --env-file "${env_file}" "$@"
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version: '2.1'

volumes:
octoprint_vol: {}
octoprint_vol:

services:
# Octoprint itself
Expand Down Expand Up @@ -49,5 +49,5 @@ services:
volumes:
- octoprint_vol:/opt/haproxy/data
ports:
- "80:80"
- "443:443"
- "${HTTP_PORT:-80}:80"
- "${HTTPS_PORT:-443}:443"
6 changes: 3 additions & 3 deletions webcam/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ FROM balenalib/${OP_MACHINE_NAME:-%%RESIN_MACHINE_NAME%%}-debian:buster

RUN apt-get update && \
apt-get install python3 python3-flask git build-essential subversion \
libjpeg62-turbo-dev imagemagick ffmpeg libv4l-dev cmake \
libjpeg62-turbo-dev imagemagick ffmpeg libv4l-dev cmake \
libraspberrypi-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

WORKDIR /opt/webcam

RUN git clone https://github.com/jacksonliam/mjpg-streamer.git /opt/webcam/mjpg-streamer && \
cd mjpg-streamer/mjpg-streamer-experimental && \
LD_LIBRARY_PATH=. make
cd mjpg-streamer/mjpg-streamer-experimental && \
LD_LIBRARY_PATH=. make

COPY run.py run.py

Expand Down

0 comments on commit e0f701c

Please sign in to comment.