Difference between revisions of "Docker Containerization"

From Lazarus wiki
(Add See Also section.)
m (Dockerizing HelloHeaders: Fix syntax highlighting errors)
 
(One intermediate revision by the same user not shown)
Line 36: Line 36:
 
== Dockerizing HelloHeaders ==
 
== Dockerizing HelloHeaders ==
  
Compile the example program on Ubuntu. Let's call the executable <syntaxhighlight inline>helloheaders</syntaxhighlight>. Below Dockerfile is used to create a Docker image for <syntaxhighlight inline>helloheaders</syntaxhighlight>.
+
Compile the example program on Ubuntu. Let's call the executable <syntaxhighlight lang=text inline>helloheaders</syntaxhighlight>. Below Dockerfile is used to create a Docker image for <syntaxhighlight lang=text inline>helloheaders</syntaxhighlight>.
  
 
=== Dockerfile ===
 
=== Dockerfile ===
  
<syntaxhighlight># Start with the Alpine 3.12 Docker image.
+
<pre>
FROM alpine:3.12
+
# Start with the Alpine 3.12 Docker image.
 +
FROM alpine:3.12
  
# Install libc6-compat, required for executables generated by FPC.
+
# Install libc6-compat, required for executables generated by FPC.
RUN apk --no-cache --update add libc6-compat
+
RUN apk --no-cache --update add libc6-compat
  
# Set working directory in the container image.
+
# Set working directory in the container image.
WORKDIR /app
+
WORKDIR /app
  
# Copy the executable from host into container image.
+
# Copy the executable from host into container image.
COPY helloheaders /app/helloheaders
+
COPY helloheaders /app/helloheaders
  
# Add uid/gid to run the binary. Don't want to run as root.
+
# Add uid/gid to run the binary. Don't want to run as root.
RUN addgroup -g 1099 apprunner \
+
RUN addgroup -g 1099 apprunner \
  && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
+
  && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
  
# Set uid/gid for the running program.
+
# Set uid/gid for the running program.
USER apprunner:apprunner
+
USER apprunner:apprunner
  
# Make the TCP port 8080 available to outside the container.
+
# Make the TCP port 8080 available to outside the container.
EXPOSE 8080
+
EXPOSE 8080
  
# Run the program.
+
# Run the program.
CMD ["/app/helloheaders"]</syntaxhighlight>
+
CMD ["/app/helloheaders"]
 +
</pre>
  
 
=== Build Docker Image ===
 
=== Build Docker Image ===
  
<syntaxhighlight>% sudo docker build -t helloheaders:fpc .
+
<pre>% sudo docker build -t helloheaders:fpc .
 
Sending build context to Docker daemon  1.331MB
 
Sending build context to Docker daemon  1.331MB
 
Step 1/8 : FROM alpine:3.12
 
Step 1/8 : FROM alpine:3.12
Line 96: Line 98:
 
  ---> 399269970984
 
  ---> 399269970984
 
Successfully built 399269970984
 
Successfully built 399269970984
Successfully tagged helloheaders:fpc</syntaxhighlight>
+
Successfully tagged helloheaders:fpc</pre>
  
 
=== Check Size of Docker Image ===
 
=== Check Size of Docker Image ===
  
<syntaxhighlight>% sudo docker images
+
<syntaxhighlight lang=bash>% sudo docker images
 
REPOSITORY                TAG              IMAGE ID            CREATED            SIZE
 
REPOSITORY                TAG              IMAGE ID            CREATED            SIZE
 
helloheaders              fpc              399269970984        2 minutes ago      7.51MB</syntaxhighlight>
 
helloheaders              fpc              399269970984        2 minutes ago      7.51MB</syntaxhighlight>
Line 106: Line 108:
 
=== 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>http://127.0.0.1:8080/</syntaxhighlight> with your web browser.
+
Visit <syntaxhighlight lang=text inline>http://127.0.0.1:8080/</syntaxhighlight> with your web browser.
  
 
== See Also ==
 
== See Also ==

Latest revision as of 05:39, 14 October 2020

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