Skip to content

Commit

Permalink
Merge pull request #54 from intolerance/docker-local-src
Browse files Browse the repository at this point in the history
Docker Local Source
  • Loading branch information
iegomez authored Feb 24, 2020
2 parents 88f612a + fb2eb96 commit d53b09a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 9 deletions.
62 changes: 62 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

#Use debian:stable-slim as a builder and then copy everything.
FROM debian:stable-slim as builder

#Set mosquitto and plugin versions.
#Change them for your needs.
ENV MOSQUITTO_VERSION=1.6.8
ENV PLUGIN_VERSION=0.6.1
ENV GO_VERSION=1.13.8

WORKDIR /app

#Get mosquitto build dependencies.
RUN apt-get update && apt-get install -y libwebsockets8 libwebsockets-dev libc-ares2 libc-ares-dev openssl uuid uuid-dev wget build-essential git
RUN mkdir -p mosquitto/auth mosquitto/conf.d

RUN wget http://mosquitto.org/files/source/mosquitto-${MOSQUITTO_VERSION}.tar.gz
RUN tar xzvf mosquitto-${MOSQUITTO_VERSION}.tar.gz && rm mosquitto-${MOSQUITTO_VERSION}.tar.gz

#Build mosquitto.
RUN cd mosquitto-${MOSQUITTO_VERSION} && make WITH_WEBSOCKETS=yes && make install && cd ..

#Get Go.
RUN wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
RUN export PATH=$PATH:/usr/local/go/bin && go version && rm go${GO_VERSION}.linux-amd64.tar.gz

#Build the plugin from local source
COPY ./ ./

#Build the plugin.
RUN export PATH=$PATH:/usr/local/go/bin && export CGO_CFLAGS="-I/usr/local/include -fPIC" && export CGO_LDFLAGS="-shared" && make

#Start from a new image.
FROM debian:stable-slim

#Get mosquitto dependencies.
RUN apt-get update && apt-get install -y libwebsockets8 libc-ares2 openssl uuid

#Setup mosquitto env.
RUN mkdir -p /var/lib/mosquitto /var/log/mosquitto
RUN groupadd mosquitto \
&& useradd -s /sbin/nologin mosquitto -g mosquitto -d /var/lib/mosquitto \
&& chown -R mosquitto:mosquitto /var/log/mosquitto/ \
&& chown -R mosquitto:mosquitto /var/lib/mosquitto/

#Copy confs, plugin so and mosquitto binary.
COPY --from=builder /app/mosquitto/ /mosquitto/
COPY --from=builder /app/go-auth.so /mosquitto/go-auth.so
COPY --from=builder /usr/local/sbin/mosquitto /usr/sbin/mosquitto

#Uncomment to copy your custom confs (change accordingly) directly when building the image.
#Leave commented if you want to mount a volume for these (see docker-compose.yml).

# COPY ./docker/conf/mosquitto.conf /etc/mosquitto/mosquitto.conf
# COPY ./docker/conf/conf.d/go-auth.conf /etc/mosquitto/conf.d/go-auth.conf
# COPY ./docker/conf/auth/acls /etc/mosquitto/auth/acls
# COPY ./docker/conf/auth/passwords /etc/mosquitto/auth/passwords

#Expose tcp and websocket ports as defined at mosquitto.conf (change accordingly).
EXPOSE 1883 1884

ENTRYPOINT ["sh", "-c", "/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf" ]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,9 @@ See the official [MQTT authentication & authorization guide](https://www.loraser

### Docker

See the [docker](docker/) dir for an example image.
This project provides example Dockerfiles for building a Docker container that contains `mosquitto` and the `mosquitto-go-auth` plug-in.

Please read the [documentation](./docker/README.md) in the [docker](/docker) directory for more information.

### License

Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ FROM debian:stable-slim as builder

#Set mosquitto and plugin versions.
#Change them for your needs.
ENV MOSQUITTO_VERSION=1.6.3
ENV PLUGIN_VERSION=0.5.0
ENV GO_VERSION=1.12.6
ENV MOSQUITTO_VERSION=1.6.8
ENV PLUGIN_VERSION=0.6.1
ENV GO_VERSION=1.13.8

WORKDIR /app

Expand Down
67 changes: 62 additions & 5 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
### Docker image
## Docker Images

This is an attempt on building a *not so heavy* image given the impossibility of using `alpine` based ones (see https://github.com/iegomez/mosquitto-go-auth/issues/14, https://github.com/iegomez/mosquitto-go-auth/issues/15 and https://github.com/iegomez/mosquitto-go-auth/issues/20).
This project utilizes two `Dockerfiles` to give the option of building the `mosquitto-go-auth` plugin from either local or released source.

It uses an intermediate image based on `debian:stable-slim` to build both mosquitto and the plugin and later on copies the binaries to the final image, also absed in `debian:stable-slim`, which stands at 113 MB.
In both cases the resulting image contains a compiled and ready to run version of `mosquitto` with the `mosquitto-go-auth` plugin-in enabled.

The example `Dockerfile` will also copy `conf` files present at the current dir as well as set the versions for Go, mosquitto and the plugin. Please change values as needed.
### Base Image
Since there are several issues with using `alpine` based images we are using `debian:stable-slim` for both our build and final image. The final image size is about 128 MB.

Documented issues:
- https://github.com/iegomez/mosquitto-go-auth/issues/14
- https://github.com/iegomez/mosquitto-go-auth/issues/15
- https://github.com/iegomez/mosquitto-go-auth/issues/20

### Build method
The Dockerfiles utilize the [multi-stage](https://docs.docker.com/develop/develop-images/multistage-build/) build feature provided by the Docker CLI.

This feature allows you to optimize the final image output by copying select artifacts from the previous stage.

### mosquitto-go-auth Plug-in (Released Source)
The `Dockerfile` in the `/docker` directory compiles the plug-in using the specified `PLUGIN_VERSION` source code. The source code will come directly from our [GitHub Releases](https://github.com/iegomez/mosquitto-go-auth/releases).

### mosquitto-go-auth Plug-In (Local Source)
The `Dockerfile` located in the `root` (`/`) directory will compile the plug-in using your local source code.

### Mosquitto
Both Dockerfiles compile `mosquitto` using the source code from the version specified by `MOSQUITTO_VERSION`.

>Mosquitto released versions can be found at https://mosquitto.org/files/source/
#### Conf files
The Dockerfiles can also copy `conf` files found in the `/docker/conf` project directory.

>You will have to uncomment the instructions manually for the files to be copied.

### Docker Commands

In case you're not familiar with [Docker](https://docs.docker.com/), here are some basic commands for getting going.

#### Build Container:
```sh
# Ensure your PWD is either project root or /docker
docker build -t mosquitto-go-auth .
```

#### Run Container:
```sh
# This command will run the container and map the corresponding ports locally.
# You can access Mosquitto running inside the container on localhost:1883 and localhost:1884 (WebSockets)
docker run -it -p 1884:1884 -p 1883:1883 mosquitto-go-auth
```

#### Stop Container:
```sh
docker stop $(docker ps -q --filter ancestor=mosquitto-go-auth)
```

#### Remove Container locally:
```sh
docker rmi $(docker images -q --filter reference='mosquitto-go-auth:*')
```

### Docker Compose
This is just a working example of how a docker image could be built for this project and composed with other images such as a `redis` one for cache (check [docker-compose](docker-compose.yml)). Any contributions to make it better are very welcome.

This is just a working example of how a docker image could be built for this project and composed with other images such as a `redis` one for cache (check [docker-compose](docker-compose.yml)). Any contributions to make it better are very welcome.

0 comments on commit d53b09a

Please sign in to comment.