risoflora / brookframework

Microframework which helps to develop web Pascal applications.
https://risoflora.github.io/brookframework
GNU Lesser General Public License v2.1
233 stars 54 forks source link

Dockerizing with Alpine Linux needs pseudo-tty allocation by '-it' option to 'docker run' #16

Closed PierceNg closed 4 years ago

PierceNg commented 4 years ago

Environment:

% ls -l
total 1968
-rw-r--r-- 1 pierce pierce     307 Aug 15 08:33 Dockerfile
-rwxrwxr-x 1 pierce pierce 1104056 Aug 15 08:53 hellohttpsrv*
-rwxr-xr-x 1 pierce pierce  901816 Aug 15 00:32 libsagui.so.3*

Program works on command line:

% LD_LIBRARY_PATH=`pwd` ./hellohttpsrv
Server running at http://localhost:8080

In another terminal window:

% curl http://127.0.0.1:8080
<html><head><title>Hello world</title></head><body>Hello world</body></html>%    

Dockerfile:

% cat Dockerfile
FROM alpine:3.12
RUN apk --no-cache --update add libc6-compat
WORKDIR /app
COPY hellohttpsrv /app/helloworld
COPY libsagui.so.3 /lib/libsagui.so.3
RUN addgroup -g 1099 apprunner \
  && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
USER apprunner:apprunner
EXPOSE 8080
CMD ["/app/helloworld"]

Build and tag as helloworld:brook4:

% sudo docker build -t helloworld:brook4 .
...
Successfully tagged helloworld:brook4

The resulting Docker container exits immediately though:

% sudo docker run --rm -p 8080:8080 helloworld:brook4 
Server running at http://localhost:8080
% 

But running the Docker container interactively (by using -it option and running sh) works:

% sudo docker run -it --rm -p 8080:8080 helloworld:brook4 sh
/app $ ls -l
total 1080
-rwxrwxr-x    1 root     root       1104056 Aug 15 00:53 helloworld
/app $ ./helloworld
Server running at http://localhost:8080

In another terminal window:

% curl http://127.0.0.1:8080            
<html><head><title>Hello world</title></head><body>Hello world</body></html>%

And running with -it option to docker run works too:

% sudo docker run -it --rm -p 8080:8080 helloworld:brook4
Server running at http://localhost:8080

In another terminal window:

% curl http://127.0.0.1:8080            
<html><head><title>Hello world</title></head><body>Hello world</body></html>%

Not sure if this counts as a bug; I've not encountered the need to use '-it' option when dockerizing other server-type programs.

Reference: My posts to Lazarus forum that also talk about fcl-web and BrookFreePascal that have pure Pascal HTTP servers.

silvioprog commented 4 years ago

Hi @PierceNg, thanks for providing such full steps! I have never tried Sagui/Brook on Alpine before, so I'm very happy to see them working fine in such a minimal system. :-)

Regarding the problem you are getting, it seems not related to Brook. In general, to run a console application in a container in non interactive mode, the application needs to avoid to exit prematurely after getting executed, so maybe using something like TSimpleEvent.WaitFor(INFINITE) would help you to do that for any platform. For example, something like this.

Let me know if the information above helps you to solve the problem.

PierceNg commented 4 years ago

Hi @silvioprog. I came to Brook Framework after doing the same Alpine Docker thing with fcl-web and BrookFreePascal. Based on your reply I now realize I made some assumptions without reading the code. :-P Now that I know the example is literally a console program, I will be able to daemonize it.