|
| 1 | +# Running Civetweb in Docker |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This guide explains how to build and run Civetweb using a multi-stage Dockerfile. The Dockerfile uses Alpine Linux for both the build and runtime stages, making the final image lightweight. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- Docker installed on your machine |
| 10 | +- Basic understanding of Docker commands |
| 11 | + |
| 12 | +## Dockerfile Explained |
| 13 | + |
| 14 | +### Build Stage |
| 15 | + |
| 16 | +The build stage uses Alpine Linux 3.18 and installs the necessary build tools and libraries. |
| 17 | + |
| 18 | +```Dockerfile |
| 19 | +FROM alpine:3.18 AS build |
| 20 | +RUN apk update && \ |
| 21 | + apk add --no-cache \ |
| 22 | + build-base zlib-dev |
| 23 | +``` |
| 24 | + |
| 25 | +The source code and other necessary files are copied into the `/civetweb` directory in the container. |
| 26 | + |
| 27 | +```Dockerfile |
| 28 | +WORKDIR /civetweb |
| 29 | +COPY src ./src/ |
| 30 | +COPY include ./include/ |
| 31 | +COPY Makefile ./ |
| 32 | +COPY resources ./resources/ |
| 33 | +COPY *.md ./ |
| 34 | +``` |
| 35 | + |
| 36 | +The Civetweb server is then built and installed into the `/app` directory. |
| 37 | + |
| 38 | +```Dockerfile |
| 39 | +RUN make build && \ |
| 40 | + make WITH_ALL=1 && \ |
| 41 | + make install PREFIX=/app |
| 42 | +``` |
| 43 | + |
| 44 | +### Image Stage |
| 45 | + |
| 46 | +The image stage also uses Alpine Linux 3.18 but installs only the runtime dependencies. |
| 47 | + |
| 48 | +```Dockerfile |
| 49 | +FROM alpine:3.18 |
| 50 | +RUN apk update && \ |
| 51 | + apk add --no-cache \ |
| 52 | + libstdc++ zlib |
| 53 | +``` |
| 54 | + |
| 55 | +A non-root user `civetweb` is created for security reasons. |
| 56 | + |
| 57 | +```Dockerfile |
| 58 | +RUN addgroup -S civetweb && adduser -S civetweb -G civetweb |
| 59 | +USER civetweb |
| 60 | +``` |
| 61 | + |
| 62 | +The built application from the build stage is copied into this stage. |
| 63 | + |
| 64 | +```Dockerfile |
| 65 | +COPY --chown=civetweb:civetweb --from=build /app/ /app/ |
| 66 | +``` |
| 67 | + |
| 68 | +The container will listen on port 8080 at runtime. |
| 69 | + |
| 70 | +```Dockerfile |
| 71 | +EXPOSE 8080 |
| 72 | +``` |
| 73 | + |
| 74 | +Finally, the entry point for the container is set. |
| 75 | + |
| 76 | +```Dockerfile |
| 77 | +ENTRYPOINT [ "/app/bin/civetweb", "/app/etc/civetweb.conf" ] |
| 78 | +``` |
| 79 | + |
| 80 | +## Build and Run |
| 81 | + |
| 82 | +To build the Docker image, run: |
| 83 | + |
| 84 | +```bash |
| 85 | +docker build -t civetweb:latest . |
| 86 | +``` |
| 87 | + |
| 88 | +To run the container, execute: |
| 89 | + |
| 90 | +```bash |
| 91 | +docker run -p 8080:8080 civetweb:latest |
| 92 | +``` |
| 93 | + |
| 94 | +## Conclusion |
| 95 | + |
| 96 | +This Dockerfile provides a secure and efficient way to build and run Civetweb. The use of multi-stage builds ensures that the final image is as small as possible. The `EXPOSE` directive informs that the application will listen on port 8080, making it easier to map ports when running the container. |
0 commit comments