loentar / ngrest

Fast and easy C++ RESTful WebServices framework
Apache License 2.0
465 stars 94 forks source link

Help: Setup inside alpine image #72

Closed Nordes closed 3 years ago

Nordes commented 3 years ago

Hi there,

Thanks for all your work. It's very a nice project that you've got there.

I was wondering if it was possible to compile from within an Alpine image? I am asking this question because I want to build the NGINX module and then copy it (the .so) to nginx residing in my final docker image.

So far, I did the following:

FROM nginx:alpine AS builder
# nginx:alpine contains NGINX_VERSION environment variable, like so:
# ENV NGINX_VERSION 1.15.0

# For latest build deps, see https://github.com/nginxinc/docker-nginx/blob/master/mainline/alpine/Dockerfile
RUN apk add --no-cache --virtual .build-deps \
  gcc \
  libc-dev \
  make \
  openssl-dev \
  pcre-dev \
  zlib-dev \
  linux-headers \
  curl \
  gnupg \
  libxslt-dev \
  gd-dev \
  geoip-dev \
  git \
  bash \ 
  cmake \
  g++ \
  inotify-tools

# Download sources
RUN wget "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -O nginx.tar.gz && \
    wget -qO- http://bit.ly/ngrest | sh

This fails at some point:

#6 0.546 Connecting to nginx.org (3.125.197.172:80)                                                                                                                                   
#6 0.758 saving to 'nginx.tar.gz'                                                                                                                                                     
#6 1.486 nginx.tar.gz         100% |********************************| 1035k  0:00:00 ETA                                                                                              
#6 1.486 'nginx.tar.gz' saved                                                                                                                                                         
#6 1.778 Cloning into 'ngrest'...
#6 2.681 Configuring ngrest for the build...
#6 3.252 Skipping Nginx module compilation: no Nginx source path provided
#6 3.293 Building ngrest. It may take few minutes...
#6 12.11 /root/.ngrest/ngrest/core/server/src/ClientHandler.cpp:25:10: fatal error: error.h: No such file or directory
#6 12.11    25 | #include <error.h>
#6 12.11       |          ^~~~~~~~~
#6 12.11 compilation terminated.
#6 12.11 make[2]: *** [core/server/CMakeFiles/ngrestserver.dir/build.make:82: core/server/CMakeFiles/ngrestserver.dir/src/ClientHandler.cpp.o] Error 1
#6 12.11 make[1]: *** [CMakeFiles/Makefile2:557: core/server/CMakeFiles/ngrestserver.dir/all] Error 2
#6 12.11 make: *** [Makefile:160: all] Error 2

Can you help me with this?

For now, I will go back to a more traditional ubuntu based image, but it would be very nice to make it work on alpine based image.

loentar commented 3 years ago

Well you need to know which package provides the error.h header file. Usually it's included into libc6-dev but looks like this distro doesn't contain it.

Nordes commented 3 years ago

In the end, I used a nginx image based on buster and the result look like this:

FROM debian:buster-slim as Base # Store this as a base image so we don't need to do this anymore
WORKDIR /build

# Missing clean-up, but it's ok (it's a build after all)
RUN apt update && apt install wget build-essential cmake sudo libjsoncpp-dev -y && \
    wget -qO- http://bit.ly/ngrest | bash

FROM Base as Build
WORKDIR /build

COPY [".", "."]
RUN chmod +x build.sh && ./build.sh # Build.sh contains the stuff to build the whole thing (see later below)

#=====================================
# this nginx is a buster based image
FROM nginx:1.19.8 as final
WORKDIR /app

RUN apt update && apt install libjsoncpp-dev -y && ln -s /usr/include/jsoncpp/json/ /usr/include/json

# Add the lib
COPY --from=Build ["/build/publish", "."]
COPY --from=Build ["/build/build-assets/mod_ngrest.so", "/etc/nginx/modules/mod_ngrest.so"]
COPY --from=Build ["/build/lib/", "/lib/x86_64-linux-gnu/"]
RUN chmod 655 /etc/nginx/modules/mod_ngrest.so
COPY --from=Build ["/build/build-assets/nginx/", "/etc/nginx/"]
RUN mkdir -p /app/logs  && chmod 0777 /app/logs

expose 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon **off;"]**

The build.sh is like this:

❯ cat build.sh
#!/bin/bash

# Cleanup
rm -f  ngrest-server-bundle.tar.xz && rm -f mystuff-ngrest.tar.xz
# Cp libs
cp -R ./lib/libfwlib32* /lib/x86_64-linux-gnu/
ln -s /usr/include/jsoncpp/json/ /usr/include/json

# Bundle the server part (mandatory)
# http://localhost:1984/ngrest/services
ngrest bundle-server
mv ngrest-server-bundle*.tar.xz ngrest-server-bundle.tar.xz

# Bundle mystuff API
mkdir -p publish
ngrest build && ngrest bundle && mv mystuff-ngrest*.tar.xz mystuff-ngrest.tar.xz

# Prepare published folder.
mkdir -p publish
mv *.tar.xz publish
cd publish
tar -xf ngrest-server-bundle.tar.xz
rm -f ngrest-server-bundle.tar.xz
mv mystuff-ngrest.tar.xz ngrest

cd ngrest
tar -xf mystuff-ngrest.tar.xz
rm -f mystuff-ngrest.tar.xz

rm -f **/build/publish/ngrest/share/ngrest/services/favicon.so**