Skip to content

Commit

Permalink
Merge pull request #1462 from activeshadow/docker-fix
Browse files Browse the repository at this point in the history
Don't rely on systemd to run minimega components
  • Loading branch information
glattercj authored Feb 15, 2022
2 parents c80bd73 + f07d596 commit 81ce8a9
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 64 deletions.
21 changes: 11 additions & 10 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY ./src ./src
RUN ./build.bash


FROM jrei/systemd-ubuntu:20.04
FROM ubuntu:20.04

RUN apt update && apt install -y \
dnsmasq iproute2 isc-dhcp-client \
Expand All @@ -31,19 +31,20 @@ COPY --from=gobuilder /minimega/bin/miniweb /opt/minimega/bin/miniweb
COPY --from=gobuilder /minimega/bin/miniccc /opt/minimega/bin/miniccc
COPY --from=gobuilder /minimega/bin/miniccc.exe /opt/minimega/bin/miniccc.exe

# For the sake of consistency, let's go ahead and include protonuke in the image
# too so we can easily grab a copy if/when necessary.
# For the sake of consistency, let's go ahead and include protonuke and
# minirouter in the image too so we can easily grab a copy if/when necessary.
COPY --from=gobuilder /minimega/bin/protonuke /opt/minimega/bin/protonuke
COPY --from=gobuilder /minimega/bin/protonuke.exe /opt/minimega/bin/protonuke.exe
COPY --from=gobuilder /minimega/bin/minirouter /opt/minimega/bin/minirouter

COPY ./misc/web /opt/minimega/misc/web
COPY ./misc/web /opt/minimega/misc/web
COPY ./docker/mm /usr/local/bin/mm

ADD docker/minimega.service /etc/systemd/system/minimega.service
ADD docker/miniweb.service /etc/systemd/system/miniweb.service
COPY ./docker/start-minimega.sh /start-minimega.sh

WORKDIR /etc/systemd/system/multi-user.target.wants

RUN ln -s ../minimega.service \
&& ln -s ../miniweb.service
RUN chmod +x /usr/local/bin/mm \
&& chmod +x /start-minimega.sh

WORKDIR /opt/minimega

CMD ["/start-minimega.sh"]
51 changes: 42 additions & 9 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,47 @@

### Build the minimega docker image

> NOTE: Currently, only minimega, miniweb, miniccc, and protonuke will exist in the minimega docker image. If you need additional binaries, add them to the Dockerfile using the `COPY --from=gobuilder …` directive.
> NOTE: Currently, only minimega, miniweb, miniccc, minirouter, and protonuke
> will exist in the minimega docker image. If you need additional binaries, add
> them to the Dockerfile using the `COPY --from=gobuilder …` directive.
> NOTE: The docker image needs to be built from the base directory of the minimega repository.
> NOTE: The docker image needs to be built from the base directory of the
> minimega repository.
```bash
$ docker build -t minimega -f docker/Dockerfile .
```

### Start the minimega docker container

> NOTE: The additional privileges and system mounts (e.g. /dev) are required for the openvswitch process to run inside the container and to allow minimega to perform file injections.
> NOTE: The additional privileges and system mounts (e.g. /dev) are required for
> the openvswitch process to run inside the container and to allow minimega to
> perform file injections.
> NOTE: If the `deploy launch` minimega command is used to initialize a
> multi-node minimega cluster, then a directory containing SSH keys will likely
> need to be mounted as a volume as well (and can be read-only). An example
> would be `-v /root/.ssh:/root/.ssh:ro`.
```bash
docker run -d -it \
docker run -d \
--name minimega \
--hostname minimega \
--privileged \
--cap-add ALL \
-p 9000:9000/udp \
-p 9001:9001 \
-v /tmp/minimega:/tmp/minimega \
-v /var/log/minimega:/var/log/minimega \
-v /dev:/dev \
-v /lib/modules:/lib/modules:ro \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
--health-cmd "minimega -e version" \
-v /var/log/minimega:/var/log/minimega \
-v /tmp/minimega:/tmp/minimega \
--health-cmd "mm version" \
minimega
```

The container runs systemd as PID 1, which takes care of starting openvswitch, minimega, and miniweb.
The container runs the `start-minimega.sh` script as PID 1, which takes care of
starting openvswitch, miniweb, and finally minimega. This means the minimega
logs will be available in the container logs via Docker.

---

Expand Down Expand Up @@ -75,3 +86,25 @@ $ source ~/.bash_aliases

miniweb gets started in the container automatically.

### minimega configuration

By default, the following values are set for minimega:

```
MM_BASE=/tmp/minimega
MM_FILEPATH=/tmp/minimega/files
MM_BROADCAST=255.255.255.255
MM_PORT=9000
MM_DEGREE=2
MM_CONTEXT=minimega
MM_LOGLEVEL=info
MM_LOGFILE=/var/log/minimega.log
```

These values can be overwritten either by passing environment variables to
Docker when starting the container or by binding a file to
`/etc/default/minimega` in the container that contains updated values.

> NOTE: If a value is specified both as an environment variable to Docker and in
> the file bound to `/etc/default/minimega`, the value in
> `/etc/default/minimega` will be used.
7 changes: 4 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
version: '3.7'
services:
minimega:
build:
context: ../
dockerfile: docker/Dockerfile
image: minimega
container_name: minimega
hostname: minimega
Expand All @@ -19,10 +22,8 @@ services:
volumes:
- /dev:/dev
- /lib/modules:/lib/modules:ro
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- /etc/localtime:/etc/localtime:ro
- /tmp/minimega:/tmp/minimega
- /var/log/minimega:/var/log/minimega
- /root/.ssh:/root/.ssh:ro
healthcheck:
test: minimega -e version
test: mm version
30 changes: 0 additions & 30 deletions docker/minimega.service

This file was deleted.

12 changes: 0 additions & 12 deletions docker/miniweb.service

This file was deleted.

3 changes: 3 additions & 0 deletions docker/mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

exec /opt/minimega/bin/minimega -e "$@"
28 changes: 28 additions & 0 deletions docker/start-minimega.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

/usr/share/openvswitch/scripts/ovs-ctl start

/opt/minimega/bin/miniweb -root=/opt/minimega/misc/web -addr=0.0.0.0:9001 &

: "${MM_BASE:=/tmp/minimega}"
: "${MM_FILEPATH:=/tmp/minimega/files}"
: "${MM_BROADCAST:=255.255.255.255}"
: "${MM_PORT:=9000}"
: "${MM_DEGREE:=2}"
: "${MM_CONTEXT:=minimega}"
: "${MM_LOGLEVEL:=info}"
: "${MM_LOGFILE:=/var/log/minimega.log}"

[[ -f "/etc/default/minimega" ]] && source "/etc/default/minimega"

/opt/minimega/bin/minimega \
-force \
-nostdin \
-base=${MM_BASE} \
-filepath=${MM_FILEPATH} \
-broadcast=${MM_BROADCAST} \
-port=${MM_PORT} \
-degree=${MM_DEGREE} \
-context=${MM_CONTEXT} \
-level=${MM_LOGLEVEL} \
-logfile=${MM_LOGFILE}

0 comments on commit 81ce8a9

Please sign in to comment.