Sign in Join
Wiki copytemporary

Docker Containerization/zh CN: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-## Contents
2-
3- * 1 Docker 容器化
4- * 1.1 Docker
5- * 1.2 Alpine Linux
6- * 1.3 fcl-web 示例 - HelloHeaders
7- * 1.4 Docker 化 HelloHeaders
8- * 1.4.1 Dockerfile
9- * 1.4.2 Build Docker Image
10- * 1.4.3 查看 Docker 镜像的大小
11- * 1.4.4 运行容器
12- * 1.5 参见
13-
141# Docker 容器化
152
163## Docker
174
185Docker 容器镜像是个轻量级、独立且可执行的软件包,应用程序运行所需的一切均包含其中:代码、运行时环境、系统工具、系统库和配置参数。在运行时,容器镜像会转换为容器。容器将软件与环境隔离开来,确保在不同环境下(例如开发环境和预生产环境)运行的一致性。
196
207Docker 容器可在 Windows、Linux、macOS 和 Amazon Web Services、Microsoft Azure 之类的云服务中运行。
218
229以 Unix 守护进程或 Windows 服务这些传统形式运行的应用,如 Web 服务和数据库,正越来越多以容器化的形式运行。
2310
2411## Alpine Linux
2512
2613Alpine Linux 是一种注重安全、轻量级的 Linux。基于 Alpine Linux 的最小 Docker 容器镜像只有 5MB 大小。
2714
2815## fcl-web 示例 - HelloHeaders
2916
3017以下例程能响应 web 请求,回送请求信息。
3118```
3219program HelloHeadersPure;
3320
3421 {$mode objfpc}{$H+}
3522
3623 uses
3724 cthreads, httpdefs, httproute, webutil,
3825 fphttpapp;
3926
4027 procedure doEchoRequest(aReq: TRequest; aResp: TResponse);
4128 begin
4229 DumpRequest(aReq, aResp.contents, true);
4330 end;
4431
4532 begin
4633 HTTPRouter.registerRoute('*', @doEchoRequest);
4734 Application.Port:=8080;
4835 Application.Threaded:=True;
4936 Application.Initialize;
5037 Application.Run;
5138 end.
5239```
5340
5441## Docker 化 HelloHeaders
5542
5643下面在 Ubuntu 下编译此例程。可执行文件命名为 `helloheaders`。用以下 Dockerfile 为 `helloheaders` 创建 Docker 镜像。
5744
5845### Dockerfile
5946```
6047# Start with the Alpine 3.12 Docker image.
6148 FROM alpine:3.12
6249
6350 # Install libc6-compat, required for executables generated by FPC.
6451 RUN apk --no-cache --update add libc6-compat
6552
6653 # Set working directory in the container image.
6754 WORKDIR /app
6855
6956 # Copy the executable from host into container image.
7057 COPY helloheaders /app/helloheaders
7158
7259 # Add uid/gid to run the binary. Don't want to run as root.
7360 RUN addgroup -g 1099 apprunner \
7461 && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
7562
7663 # Set uid/gid for the running program.
7764 USER apprunner:apprunner
7865
7966 # Make the TCP port 8080 available to outside the container.
8067 EXPOSE 8080
8168
8269 # Run the program.
8370 CMD ["/app/helloheaders"]
8471```
8572
8673### Build Docker Image
8774```
8875% sudo docker build -t helloheaders:fpc .
8976 Sending build context to Docker daemon 1.331MB
9077 Step 1/8 : FROM alpine:3.12
9178 ---> a24bb4013296
9279 Step 2/8 : RUN apk --no-cache --update add libc6-compat
9380 ---> Using cache
9481 ---> 8bd9ae66e9fe
9582 Step 3/8 : WORKDIR /app
9683 ---> Using cache
9784 ---> 1c69cb1c14e3
9885 Step 4/8 : COPY helloheaders /app/helloheaders
9986 ---> de5ffddf76bb
10087 Step 5/8 : RUN addgroup -g 1099 apprunner && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
10188 ---> Running in 54199f0a8f4f
10289 Removing intermediate container 54199f0a8f4f
10390 ---> c7d18145bc02
10491 Step 6/8 : USER apprunner:apprunner
10592 ---> Running in 0a544c5fc8f6
10693 Removing intermediate container 0a544c5fc8f6
10794 ---> a3868d1f2033
10895 Step 7/8 : EXPOSE 8080
10996 ---> Running in 829a1b804c0d
11097 Removing intermediate container 829a1b804c0d
11198 ---> 7389810c7acf
11299 Step 8/8 : CMD ["/app/helloheaders"]
113100 ---> Running in 882e002e08b0
114101 Removing intermediate container 882e002e08b0
115102 ---> 399269970984
116103 Successfully built 399269970984
117104 Successfully tagged helloheaders:fpc
118105```
119106
120107### 查看 Docker 镜像的大小
121108```
122109% sudo docker images
123110 REPOSITORY TAG IMAGE ID CREATED SIZE
124111 helloheaders fpc 399269970984 2 minutes ago 7.51MB
125112```
126113
127114### 运行容器
128115```
129116% sudo docker run --rm -p 8080:8080 helloheaders:fpc
130117```
131118
132119然后即可用浏览器访问 `http://127.0.0.1:8080/`。
133120
134121## 参见
135122
136123 * [[Qemu and other emulators|Qemu 等模拟器]]
137124 * [[Small Virtual Machines|小型虚拟机]]
138125
139126---
140127*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](/wiki-contact).*

Back to history