Docker Containerization/zh CN: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Docker Containerization/zh CN
| Old | New | Line | |
|---|---|---|---|
| 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 | + | ||
| 14 | + | # Docker 容器化 | |
| 15 | + | ||
| 16 | + | ## Docker | |
| 17 | + | ||
| 18 | + | Docker 容器镜像是个轻量级、独立且可执行的软件包,应用程序运行所需的一切均包含其中:代码、运行时环境、系统工具、系统库和配置参数。在运行时,容器镜像会转换为容器。容器将软件与环境隔离开来,确保在不同环境下(例如开发环境和预生产环境)运行的一致性。 | |
| 19 | + | ||
| 20 | + | Docker 容器可在 Windows、Linux、macOS 和 Amazon Web Services、Microsoft Azure 之类的云服务中运行。 | |
| 21 | + | ||
| 22 | + | 以 Unix 守护进程或 Windows 服务这些传统形式运行的应用,如 Web 服务和数据库,正越来越多以容器化的形式运行。 | |
| 23 | + | ||
| 24 | + | ## Alpine Linux | |
| 25 | + | ||
| 26 | + | Alpine Linux 是一种注重安全、轻量级的 Linux。基于 Alpine Linux 的最小 Docker 容器镜像只有 5MB 大小。 | |
| 27 | + | ||
| 28 | + | ## fcl-web 示例 - HelloHeaders | |
| 29 | + | ||
| 30 | + | 以下例程能响应 web 请求,回送请求信息。 | |
| 31 | + | ``` | |
| 32 | + | program HelloHeadersPure; | |
| 33 | + | ||
| 34 | + | {$mode objfpc}{$H+} | |
| 35 | + | ||
| 36 | + | uses | |
| 37 | + | cthreads, httpdefs, httproute, webutil, | |
| 38 | + | fphttpapp; | |
| 39 | + | ||
| 40 | + | procedure doEchoRequest(aReq: TRequest; aResp: TResponse); | |
| 41 | + | begin | |
| 42 | + | DumpRequest(aReq, aResp.contents, true); | |
| 43 | + | end; | |
| 44 | + | ||
| 45 | + | begin | |
| 46 | + | HTTPRouter.registerRoute('*', @doEchoRequest); | |
| 47 | + | Application.Port:=8080; | |
| 48 | + | Application.Threaded:=True; | |
| 49 | + | Application.Initialize; | |
| 50 | + | Application.Run; | |
| 51 | + | end. | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | ## Docker 化 HelloHeaders | |
| 55 | + | ||
| 56 | + | 下面在 Ubuntu 下编译此例程。可执行文件命名为 `helloheaders`。用以下 Dockerfile 为 `helloheaders` 创建 Docker 镜像。 | |
| 57 | + | ||
| 58 | + | ### Dockerfile | |
| 59 | + | ``` | |
| 60 | + | # Start with the Alpine 3.12 Docker image. | |
| 61 | + | FROM alpine:3.12 | |
| 62 | + | ||
| 63 | + | # Install libc6-compat, required for executables generated by FPC. | |
| 64 | + | RUN apk --no-cache --update add libc6-compat | |
| 65 | + | ||
| 66 | + | # Set working directory in the container image. | |
| 67 | + | WORKDIR /app | |
| 68 | + | ||
| 69 | + | # Copy the executable from host into container image. | |
| 70 | + | COPY helloheaders /app/helloheaders | |
| 71 | + | ||
| 72 | + | # Add uid/gid to run the binary. Don't want to run as root. | |
| 73 | + | RUN addgroup -g 1099 apprunner \ | |
| 74 | + | && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner | |
| 75 | + | ||
| 76 | + | # Set uid/gid for the running program. | |
| 77 | + | USER apprunner:apprunner | |
| 78 | + | ||
| 79 | + | # Make the TCP port 8080 available to outside the container. | |
| 80 | + | EXPOSE 8080 | |
| 81 | + | ||
| 82 | + | # Run the program. | |
| 83 | + | CMD ["/app/helloheaders"] | |
| 84 | + | ``` | |
| 85 | + | ||
| 86 | + | ### Build Docker Image | |
| 87 | + | ``` | |
| 88 | + | % sudo docker build -t helloheaders:fpc . | |
| 89 | + | Sending build context to Docker daemon 1.331MB | |
| 90 | + | Step 1/8 : FROM alpine:3.12 | |
| 91 | + | ---> a24bb4013296 | |
| 92 | + | Step 2/8 : RUN apk --no-cache --update add libc6-compat | |
| 93 | + | ---> Using cache | |
| 94 | + | ---> 8bd9ae66e9fe | |
| 95 | + | Step 3/8 : WORKDIR /app | |
| 96 | + | ---> Using cache | |
| 97 | + | ---> 1c69cb1c14e3 | |
| 98 | + | Step 4/8 : COPY helloheaders /app/helloheaders | |
| 99 | + | ---> de5ffddf76bb | |
| 100 | + | Step 5/8 : RUN addgroup -g 1099 apprunner && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner | |
| 101 | + | ---> Running in 54199f0a8f4f | |
| 102 | + | Removing intermediate container 54199f0a8f4f | |
| 103 | + | ---> c7d18145bc02 | |
| 104 | + | Step 6/8 : USER apprunner:apprunner | |
| 105 | + | ---> Running in 0a544c5fc8f6 | |
| 106 | + | Removing intermediate container 0a544c5fc8f6 | |
| 107 | + | ---> a3868d1f2033 | |
| 108 | + | Step 7/8 : EXPOSE 8080 | |
| 109 | + | ---> Running in 829a1b804c0d | |
| 110 | + | Removing intermediate container 829a1b804c0d | |
| 111 | + | ---> 7389810c7acf | |
| 112 | + | Step 8/8 : CMD ["/app/helloheaders"] | |
| 113 | + | ---> Running in 882e002e08b0 | |
| 114 | + | Removing intermediate container 882e002e08b0 | |
| 115 | + | ---> 399269970984 | |
| 116 | + | Successfully built 399269970984 | |
| 117 | + | Successfully tagged helloheaders:fpc | |
| 118 | + | ``` | |
| 119 | + | ||
| 120 | + | ### 查看 Docker 镜像的大小 | |
| 121 | + | ``` | |
| 122 | + | % sudo docker images | |
| 123 | + | REPOSITORY TAG IMAGE ID CREATED SIZE | |
| 124 | + | helloheaders fpc 399269970984 2 minutes ago 7.51MB | |
| 125 | + | ``` | |
| 126 | + | ||
| 127 | + | ### 运行容器 | |
| 128 | + | ``` | |
| 129 | + | % sudo docker run --rm -p 8080:8080 helloheaders:fpc | |
| 130 | + | ``` | |
| 131 | + | ||
| 132 | + | 然后即可用浏览器访问 `http://127.0.0.1:8080/`。 | |
| 133 | + | ||
| 134 | + | ## 参见 | |
| 135 | + | ||
| 136 | + | * [[Qemu and other emulators|Qemu 等模拟器]] | |
| 137 | + | * [[Small Virtual Machines|小型虚拟机]] | |
| 138 | + | ||
| 139 | + | --- | |
| 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).* |
