Difference between revisions of "Docker Containerization"

From Lazarus wiki
(Add See Also section.)
m (Run Container: Fix syntax highlighting errors)
Line 106: Line 106:
=== Run Container ===
=== Run Container ===
<syntaxhighlight>%  sudo docker run --rm -p 8080:8080 helloheaders:fpc</syntaxhighlight>
<syntaxhighlight lang=bash>%  sudo docker run --rm -p 8080:8080 helloheaders:fpc</syntaxhighlight>
Visit <syntaxhighlight inline></syntaxhighlight> with your web browser.
Visit <syntaxhighlight lang=text inline></syntaxhighlight> with your web browser.
== See Also ==
== See Also ==

Revision as of 05:34, 14 October 2020


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+}
  cthreads, httpdefs, httproute, webutil,
procedure doEchoRequest(aReq: TRequest; aResp: TResponse);
  DumpRequest(aReq, aResp.contents, true);
  HTTPRouter.registerRoute('*', @doEchoRequest);

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.


# 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.

# 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.

# 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 with your web browser.

See Also