Docker Containerization/zh CN
English · 中文(中国)
Docker 容器化
Docker
Docker 容器镜像是个轻量级、独立且可执行的软件包,应用程序运行所需的一切均包含其中:代码、运行时环境、系统工具、系统库和配置参数。在运行时,容器镜像会转换为容器。容器将软件与环境隔离开来,确保在不同环境下(例如开发环境和预生产环境)运行的一致性。
Docker 容器可在 Windows、Linux、macOS 和 Amazon Web Services、Microsoft Azure 之类的云服务中运行。
以 Unix 守护进程或 Windows 服务这些传统形式运行的应用,如 Web 服务和数据库,正越来越多以容器化的形式运行。
Alpine Linux
Alpine Linux 是一种注重安全、轻量级的 Linux。基于 Alpine Linux 的最小 Docker 容器镜像只有 5MB 大小。
fcl-web 示例 - HelloHeaders
以下例程能响应 web 请求,回送请求信息。
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.Docker 化 HelloHeaders
下面在 Ubuntu 下编译此例程。可执行文件命名为 helloheaders。用以下 Dockerfile 为 helloheaders 创建 Docker 镜像。
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查看 Docker 镜像的大小
% sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
helloheaders fpc 399269970984 2 minutes ago 7.51MB运行容器
% sudo docker run --rm -p 8080:8080 helloheaders:fpc然后即可用浏览器访问 http://127.0.0.1:8080/。
参见
From the Free Pascal wiki, page "Docker Containerization/zh CN", as the Internet Archive kept it on 2025-04-21, last changed on 2024-12-25. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.