Sign in Join
Wiki copytemporary

Docker Containerization/zh CN: what changed

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

OldNewLine
11## Contents
22
33 * 1 Docker 容器化
44 * 1.1 Docker
55 * 1.2 Alpine Linux
66 * 1.3 fcl-web 示例 - HelloHeaders
77 * 1.4 Docker 化 HelloHeaders
88 * 1.4.1 Dockerfile
99 * 1.4.2 Build Docker Image
1010 * 1.4.3 查看 Docker 镜像的大小
1111 * 1.4.4 运行容器
1212 * 1.5 参见
1313
1414# Docker 容器化
1515
1616## Docker
1717
1818Docker 容器镜像是个轻量级、独立且可执行的软件包,应用程序运行所需的一切均包含其中:代码、运行时环境、系统工具、系统库和配置参数。在运行时,容器镜像会转换为容器。容器将软件与环境隔离开来,确保在不同环境下(例如开发环境和预生产环境)运行的一致性。
1919
2020Docker 容器可在 Windows、Linux、macOS 和 Amazon Web Services、Microsoft Azure 之类的云服务中运行。
2121
2222以 Unix 守护进程或 Windows 服务这些传统形式运行的应用,如 Web 服务和数据库,正越来越多以容器化的形式运行。
2323
2424## Alpine Linux
2525
2626Alpine Linux 是一种注重安全、轻量级的 Linux。基于 Alpine Linux 的最小 Docker 容器镜像只有 5MB 大小。
2727
2828## fcl-web 示例 - HelloHeaders
2929
3030以下例程能响应 web 请求,回送请求信息。
3131```
3232program HelloHeadersPure;
3333
3434 {$mode objfpc}{$H+}
3535
3636 uses
3737 cthreads, httpdefs, httproute, webutil,
3838 fphttpapp;
3939
4040 procedure doEchoRequest(aReq: TRequest; aResp: TResponse);
4141 begin
4242 DumpRequest(aReq, aResp.contents, true);
4343 end;
4444
4545 begin
4646 HTTPRouter.registerRoute('*', @doEchoRequest);
4747 Application.Port:=8080;
4848 Application.Threaded:=True;
4949 Application.Initialize;
5050 Application.Run;
5151 end.
5252```
5353
5454## Docker 化 HelloHeaders
5555
5656下面在 Ubuntu 下编译此例程。可执行文件命名为 `helloheaders`。用以下 Dockerfile 为 `helloheaders` 创建 Docker 镜像。
5757
5858### Dockerfile
5959```
6060# Start with the Alpine 3.12 Docker image.
6161 FROM alpine:3.12
6262
6363 # Install libc6-compat, required for executables generated by FPC.
6464 RUN apk --no-cache --update add libc6-compat
6565
6666 # Set working directory in the container image.
6767 WORKDIR /app
6868
6969 # Copy the executable from host into container image.
7070 COPY helloheaders /app/helloheaders
7171
7272 # Add uid/gid to run the binary. Don't want to run as root.
7373 RUN addgroup -g 1099 apprunner \
7474 && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
7575
7676 # Set uid/gid for the running program.
7777 USER apprunner:apprunner
7878
7979 # Make the TCP port 8080 available to outside the container.
8080 EXPOSE 8080
8181
8282 # Run the program.
8383 CMD ["/app/helloheaders"]
8484```
8585
8686### Build Docker Image
8787```
8888% sudo docker build -t helloheaders:fpc .
8989 Sending build context to Docker daemon 1.331MB
9090 Step 1/8 : FROM alpine:3.12
9191 ---> a24bb4013296
9292 Step 2/8 : RUN apk --no-cache --update add libc6-compat
9393 ---> Using cache
9494 ---> 8bd9ae66e9fe
9595 Step 3/8 : WORKDIR /app
9696 ---> Using cache
9797 ---> 1c69cb1c14e3
9898 Step 4/8 : COPY helloheaders /app/helloheaders
9999 ---> de5ffddf76bb
100100 Step 5/8 : RUN addgroup -g 1099 apprunner && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner
101101 ---> Running in 54199f0a8f4f
102102 Removing intermediate container 54199f0a8f4f
103103 ---> c7d18145bc02
104104 Step 6/8 : USER apprunner:apprunner
105105 ---> Running in 0a544c5fc8f6
106106 Removing intermediate container 0a544c5fc8f6
107107 ---> a3868d1f2033
108108 Step 7/8 : EXPOSE 8080
109109 ---> Running in 829a1b804c0d
110110 Removing intermediate container 829a1b804c0d
111111 ---> 7389810c7acf
112112 Step 8/8 : CMD ["/app/helloheaders"]
113113 ---> Running in 882e002e08b0
114114 Removing intermediate container 882e002e08b0
115115 ---> 399269970984
116116 Successfully built 399269970984
117117 Successfully tagged helloheaders:fpc
118118```
119119
120120### 查看 Docker 镜像的大小
121121```
122122% sudo docker images
123123 REPOSITORY TAG IMAGE ID CREATED SIZE
124124 helloheaders fpc 399269970984 2 minutes ago 7.51MB
125125```
126126
127127### 运行容器
128128```
129129% sudo docker run --rm -p 8080:8080 helloheaders:fpc
130130```
131131
132132然后即可用浏览器访问 `http://127.0.0.1:8080/`。
133133
134134## 参见
135135
136136 * [[Qemu and other emulators|Qemu 等模拟器]]
137137 * [[Small Virtual Machines|小型虚拟机]]
138138
139139---
140-*From the Free Pascal wiki, page "Docker Containerization/zh CN", as the Internet Archive kept it on 2025-04-21. 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).*
140+*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