Community Tutorials Alpine Linux كيفية تشغيل Alpine Linux في حاوية Docker
كيفية تشغيل Alpine Linux في حاوية Docker
ALPINE LINUX

كيفية تشغيل Alpine Linux في حاوية Docker

SKYLINE Knowledge Base
Photo by Praveen Thirumurugan on Unsplash

دليل عملي خطوة بخطوة لـ كيفية تشغيل Alpine Linux في حاوية Docker. أوامر مختبرة في الإنتاج، متطلبات مسبقة، تحقق نهائي وروابط لمواضيع ذات صلة.

صورة Alpine الأساسية ~7 MB هي السبب في بدء كثير من Dockerfiles بـ FROM alpine:3.20. هذا الدليل يكتب صورة Alpine معقولة ويشرح فخاخ musl-vs-glibc.

المتطلبات المسبقة

الخطوة 1: Dockerfile بسيط

FROM alpine:3.20
RUN apk add --no-cache python3 py3-flask
WORKDIR /app
COPY app.py .
EXPOSE 5000
USER nobody
CMD ["python3", "app.py"]

app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello from Alpine\n"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
docker build -t skyline/hello-alpine .
docker run --rm -p 5000:5000 skyline/hello-alpine
curl http://localhost:5000/

الخطوة 2: musl مقابل glibc — الفخ الذي يجب معرفته

Alpine يستخدم musl libc وليس glibc. الثنائيات المُجمَّعة مسبقًا لـ glibc ستفشل. الحلان:

  1. ابنِ داخل Alpine عبر multi-stage build.
  2. استخدم قاعدة glibcdebian:slim.
docker run --rm -v $(pwd):/check alpine sh -c 'apk add --no-cache file && file /check/your-binary'

الخطوة 3: --no-cache في كل مكان

RUN apk add --no-cache curl jq

RUN apk add --no-cache --virtual .build-deps gcc musl-dev make \
 && pip install --no-cache-dir uvicorn \
 && apk del .build-deps

الخطوة 4: Multi-stage builds للغات المُجمَّعة

FROM golang:1.22-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags='-s -w' -o /out/app ./cmd/api

FROM alpine:3.20
RUN apk add --no-cache ca-certificates
COPY --from=build /out/app /usr/local/bin/app
USER nobody
ENTRYPOINT ["/usr/local/bin/app"]

الخطوة 5: خصوصيات Alpine المفيدة

  • /bin/sh هو ash وليس bash.
  • لا useradd افتراضيًا — استخدم adduser:
RUN addgroup -S app && adduser -S -G app app
USER app
  • بيانات المنطقة الزمنية:
RUN apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Riyadh /etc/localtime && \
    echo "Asia/Riyadh" > /etc/timezone

الخطوة 6: تثبيت الصورة الأساسية

FROM alpine:3.20@sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef9ab5fbe9f5

التحقق

docker images skyline/hello-alpine
docker run --rm skyline/hello-alpine id
docker scout cves skyline/hello-alpine

الخاتمة

Alpine افتراضي ممتاز للحاويات بلا حالة. استخدم debian:slim عند الحاجة لـ glibc.

الخطوات التالية

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Alpine Linux for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.