Docker Containerization

From Free Pascal wiki

Docker

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

Docker containers run on Windows, Linux, macOS and cloud service providers like Amazon Web Services and Microsoft Azure.

Alpine Linux

Alpine Linux is a security-oriented, lightweight Linux distribution. The minimal Docker container image based on Alpine Linux is 5MB in size.

fcl-web example - HelloHeaders

This example program responds to a web request by sending back information about the request.

program HelloHeadersPure;
 
{$mode objfpc}{$H+}
 
uses
  cthreads, httpdefs, httproute, webutil,
  fphttpapp;
 
procedure doEchoRequest(aReq: TRequest; aResp: TResponse);
begin
  DumpRequest(aReq, aResp.contents, true);
end;
 
begin
  HTTPRouter.registerRoute('*', @doEchoRequest);
  Application.Port:=8080;
  Application.Threaded:=True;
  Application.Initialize;
  Application.Run;
end.

Dockerizing HelloHeaders

Compile the example program on Ubuntu. Let's call the executable helloheaders. Below Dockerfile is used to create a Docker image for helloheaders.

Dockerfile

 # Start with the Alpine 3.12 Docker image.
 FROM alpine:3.12

 # Install libc6-compat, required for executables generated by FPC.
 RUN apk --no-cache --update add libc6-compat

 # Set working directory in the container image.
 WORKDIR /app

 # Copy the executable from host into container image.
 COPY helloheaders /app/helloheaders

 # Add uid/gid to run the binary. Don't want to run as root.
 RUN addgroup -g 1099 apprunner \
   && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner

 # Set uid/gid for the running program.
 USER apprunner:apprunner

 # Make the TCP port 8080 available to outside the container.
 EXPOSE 8080

 # Run the program.
 CMD ["/app/helloheaders"]

Build Docker Image

% sudo docker build -t helloheaders:fpc .
Sending build context to Docker daemon  1.331MB
Step 1/8 : FROM alpine:3.12
 ---> a24bb4013296
Step 2/8 : RUN apk --no-cache --update add libc6-compat
 ---> Using cache
 ---> 8bd9ae66e9fe
Step 3/8 : WORKDIR /app
 ---> Using cache
 ---> 1c69cb1c14e3
Step 4/8 : COPY helloheaders /app/helloheaders
 ---> de5ffddf76bb
Step 5/8 : RUN addgroup -g 1099 apprunner   && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
 ---> Running in 54199f0a8f4f
Removing intermediate container 54199f0a8f4f
 ---> c7d18145bc02
Step 6/8 : USER apprunner:apprunner
 ---> Running in 0a544c5fc8f6
Removing intermediate container 0a544c5fc8f6
 ---> a3868d1f2033
Step 7/8 : EXPOSE 8080
 ---> Running in 829a1b804c0d
Removing intermediate container 829a1b804c0d
 ---> 7389810c7acf
Step 8/8 : CMD ["/app/helloheaders"]
 ---> Running in 882e002e08b0
Removing intermediate container 882e002e08b0
 ---> 399269970984
Successfully built 399269970984
Successfully tagged helloheaders:fpc

Check Size of Docker Image

% sudo docker images
REPOSITORY                TAG               IMAGE ID            CREATED             SIZE
helloheaders              fpc               399269970984        2 minutes ago       7.51MB

Run Container

%  sudo docker run --rm -p 8080:8080 helloheaders:fpc

Visit http://127.0.0.1:8080/ with your web browser.

See Also