Docker Containerization: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Docker Containerization
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Docker | |
| 2 | + | ||
| 3 | + | 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. | |
| 4 | + | ||
| 5 | + | Docker containers run on Windows, Linux, macOS and cloud service providers like Amazon Web Services and Microsoft Azure. | |
| 6 | + | ||
| 7 | + | Applications that traditionally run as Unix daemons or Windows services, such as web servers and databases, increasingly run containerized. | |
| 8 | + | ||
| 9 | + | ## Alpine Linux | |
| 10 | + | ||
| 11 | + | Alpine Linux is a security-oriented, lightweight Linux distribution. The minimal Docker container image based on Alpine Linux is 5MB in size. | |
| 12 | + | ||
| 13 | + | ## fcl-web example - HelloHeaders | |
| 14 | + | ||
| 15 | + | This example program responds to a web request by sending back information about the request. | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | program HelloHeadersPure; | |
| 19 | + | ||
| 20 | + | {$mode objfpc}{$H+} | |
| 21 | + | ||
| 22 | + | uses | |
| 23 | + | cthreads, httpdefs, httproute, webutil, | |
| 24 | + | fphttpapp; | |
| 25 | + | ||
| 26 | + | procedure doEchoRequest(aReq: TRequest; aResp: TResponse); | |
| 27 | + | begin | |
| 28 | + | DumpRequest(aReq, aResp.contents, true); | |
| 29 | + | end; | |
| 30 | + | ||
| 31 | + | begin | |
| 32 | + | HTTPRouter.registerRoute('*', @doEchoRequest); | |
| 33 | + | Application.Port:=8080; | |
| 34 | + | Application.Threaded:=True; | |
| 35 | + | Application.Initialize; | |
| 36 | + | Application.Run; | |
| 37 | + | end. | |
| 38 | + | ``` | |
| 39 | + | ||
| 40 | + | ## Dockerizing HelloHeaders | |
| 41 | + | ||
| 42 | + | Compile the example program on Ubuntu. Let's call the executable `helloheaders`. Below Dockerfile is used to create a Docker image for `helloheaders`. | |
| 43 | + | ||
| 44 | + | ### Dockerfile | |
| 45 | + | ||
| 46 | + | # Start with the Alpine 3.12 Docker image. | |
| 47 | + | FROM alpine:3.12 | |
| 48 | + | ||
| 49 | + | # Install libc6-compat, required for executables generated by FPC. | |
| 50 | + | RUN apk --no-cache --update add libc6-compat | |
| 51 | + | ||
| 52 | + | # Set working directory in the container image. | |
| 53 | + | WORKDIR /app | |
| 54 | + | ||
| 55 | + | # Copy the executable from host into container image. | |
| 56 | + | COPY helloheaders /app/helloheaders | |
| 57 | + | ||
| 58 | + | # Add uid/gid to run the binary. Don't want to run as root. | |
| 59 | + | RUN addgroup -g 1099 apprunner \ | |
| 60 | + | && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner | |
| 61 | + | ||
| 62 | + | # Set uid/gid for the running program. | |
| 63 | + | USER apprunner:apprunner | |
| 64 | + | ||
| 65 | + | # Make the TCP port 8080 available to outside the container. | |
| 66 | + | EXPOSE 8080 | |
| 67 | + | ||
| 68 | + | # Run the program. | |
| 69 | + | CMD ["/app/helloheaders"] | |
| 70 | + | ||
| 71 | + | ### Build Docker Image | |
| 72 | + | ||
| 73 | + | % sudo docker build -t helloheaders:fpc . | |
| 74 | + | Sending build context to Docker daemon 1.331MB | |
| 75 | + | Step 1/8 : FROM alpine:3.12 | |
| 76 | + | ---> a24bb4013296 | |
| 77 | + | Step 2/8 : RUN apk --no-cache --update add libc6-compat | |
| 78 | + | ---> Using cache | |
| 79 | + | ---> 8bd9ae66e9fe | |
| 80 | + | Step 3/8 : WORKDIR /app | |
| 81 | + | ---> Using cache | |
| 82 | + | ---> 1c69cb1c14e3 | |
| 83 | + | Step 4/8 : COPY helloheaders /app/helloheaders | |
| 84 | + | ---> de5ffddf76bb | |
| 85 | + | Step 5/8 : RUN addgroup -g 1099 apprunner && adduser -D -u 1099 -G apprunner -h /home/apprunner apprunner | |
| 86 | + | ---> Running in 54199f0a8f4f | |
| 87 | + | Removing intermediate container 54199f0a8f4f | |
| 88 | + | ---> c7d18145bc02 | |
| 89 | + | Step 6/8 : USER apprunner:apprunner | |
| 90 | + | ---> Running in 0a544c5fc8f6 | |
| 91 | + | Removing intermediate container 0a544c5fc8f6 | |
| 92 | + | ---> a3868d1f2033 | |
| 93 | + | Step 7/8 : EXPOSE 8080 | |
| 94 | + | ---> Running in 829a1b804c0d | |
| 95 | + | Removing intermediate container 829a1b804c0d | |
| 96 | + | ---> 7389810c7acf | |
| 97 | + | Step 8/8 : CMD ["/app/helloheaders"] | |
| 98 | + | ---> Running in 882e002e08b0 | |
| 99 | + | Removing intermediate container 882e002e08b0 | |
| 100 | + | ---> 399269970984 | |
| 101 | + | Successfully built 399269970984 | |
| 102 | + | Successfully tagged helloheaders:fpc | |
| 103 | + | ||
| 104 | + | ### Check Size of Docker Image | |
| 105 | + | ||
| 106 | + | ``` bash | |
| 107 | + | % sudo docker images | |
| 108 | + | REPOSITORY TAG IMAGE ID CREATED SIZE | |
| 109 | + | helloheaders fpc 399269970984 2 minutes ago 7.51MB | |
| 110 | + | ``` | |
| 111 | + | ||
| 112 | + | ### Run Container | |
| 113 | + | ||
| 114 | + | ``` bash | |
| 115 | + | % sudo docker run --rm -p 8080:8080 helloheaders:fpc | |
| 116 | + | ``` | |
| 117 | + | ||
| 118 | + | Visit [`http://127.0.0.1:8080/`](http://127.0.0.1:8080/) with your web browser. | |
| 119 | + | ||
| 120 | + | ## See Also | |
| 121 | + | ||
| 122 | + | - [[Qemu and other emulators]] | |
| 123 | + | - [[Small Virtual Machines]] | |
| 124 | + | ||
| 125 | + | --- | |
| 126 | + | *From the Free Pascal wiki, page "Docker Containerization", as it stood on 2022-08-16 after a change by Skalogryz, out of the wiki's dump of 2024-05-26. 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).* | |
| 127 | + | Category: Docker | |
| 128 | + | Category: Emulators |
