diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c82380 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,70 @@ + +#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.3 +ENV PLUGIN_VERSION=0.6.1 +ENV GO_VERSION=1.12.6 + +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 + + +# #Get the plugin. +# RUN wget https://github.com/iegomez/mosquitto-go-auth/archive/${PLUGIN_VERSION}.tar.gz \ +# && ls -l \ +# && tar xvf *.tar.gz --strip-components=1 \ +# && rm -Rf go*.tar.gz \ +# && ls -l + +#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" ] \ No newline at end of file diff --git a/README.md b/README.md index 82b0297..4da8cf1 100644 --- a/README.md +++ b/README.md @@ -1137,8 +1137,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 diff --git a/docker/Dockerfile b/docker/Dockerfile index f7a02df..94cf743 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -5,7 +5,7 @@ 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 PLUGIN_VERSION=0.6.1 ENV GO_VERSION=1.12.6 WORKDIR /app diff --git a/docker/README.md b/docker/README.md index e0fc932..b1f6ec5 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,9 +1,34 @@ -### 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 offers two seperate images for building the plug-in using either released or local source code. -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. +### 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 113 MB. -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. +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 will also copy `conf` files found in the `/docker/conf` directory. For your safety we have commented these instructions out, so you will have to uncomment the instructions for the files to be copied. + +### 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. \ No newline at end of file