Skip to content

Commit 6653ce8

Browse files
committed
Docker documentation
1 parent 07f002e commit 6653ce8

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

Diff for: Dockerfile

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
1-
####################################################
2-
# build stage
3-
####################################################
1+
# ---------------------------------------------------
2+
# Build Stage
3+
# ---------------------------------------------------
44

5+
# Use Alpine Linux 3.18 as the base image for the build stage
56
FROM alpine:3.18 AS build
7+
8+
# Update package list and install build dependencies
69
RUN apk update && \
710
apk add --no-cache \
811
build-base zlib-dev
9-
12+
13+
# Set the working directory inside the container
1014
WORKDIR /civetweb
15+
16+
# Copy source code and other necessary files into the container
1117
COPY src ./src/
1218
COPY include ./include/
1319
COPY Makefile ./
1420
COPY resources ./resources/
1521
COPY *.md ./
1622

23+
# Build Civetweb with all features and install it into /app directory
1724
RUN make build && \
1825
make WITH_ALL=1 && \
1926
make install PREFIX=/app
2027

21-
####################################################
22-
# image stage
23-
####################################################
28+
# ---------------------------------------------------
29+
# Image Stage
30+
# ---------------------------------------------------
2431

32+
# Use Alpine Linux 3.18 as the base image for the final stage
2533
FROM alpine:3.18
34+
35+
# Update package list and install runtime dependencies
2636
RUN apk update && \
2737
apk add --no-cache \
2838
libstdc++ zlib
2939

40+
# Create a non-root user and group for running Civetweb
3041
RUN addgroup -S civetweb && adduser -S civetweb -G civetweb
42+
43+
# Switch to the non-root user
3144
USER civetweb
3245

46+
# Copy the built application from the build stage into this stage
3347
COPY --chown=civetweb:civetweb --from=build /app/ /app/
3448

49+
# Expose port 8080 for the application
50+
EXPOSE 8080
51+
52+
# Set the entry point for the container
3553
ENTRYPOINT [ "/app/bin/civetweb", "/app/etc/civetweb.conf" ]

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Quick start documentation
7575
- [docs/Building.md](https://github.com/civetweb/civetweb/blob/master/docs/Building.md) - Building the Server (quick start guide)
7676
- [docs/Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) - Embedding (how to add HTTP support to an existing application)
7777
- [docs/OpenSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/OpenSSL.md) - Adding HTTPS (SSL/TLS) support using OpenSSL.
78+
- [docs/Docker.md](https://github.com/civetweb/civetweb/blob/master/docs/Docker.md) - Use civetweb in a Docker container.
7879
- [API documentation](https://github.com/civetweb/civetweb/tree/master/docs/api) - Additional documentation on the civetweb application programming interface ([civetweb.h](https://github.com/civetweb/civetweb/blob/master/include/civetweb.h)).
7980
- [RELEASE_NOTES.md](https://github.com/civetweb/civetweb/blob/master/RELEASE_NOTES.md) - Release Notes
8081
- [SECURITY.md](https://github.com/civetweb/civetweb/blob/master/SECURITY.md) - Security Policy

Diff for: docs/Docker.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)