Skip to content

Commit

Permalink
dockerfile 和 dockerfile-arm64 合并为一个 (swoole#777)
Browse files Browse the repository at this point in the history
* 使用 tini 正确地回收其子进程

* format bash script

* 合并dockerfile dockerfile-arm64

* 自动指定构建容器的架构

* 构建容器指定架构
  • Loading branch information
jingjingxyk authored and maxiaozhi committed Sep 20, 2024
1 parent 0474d09 commit f73541b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 91 deletions.
4 changes: 3 additions & 1 deletion sapi/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM alpine:3.20
ARG BASE_IMAGE="alpine:3.20"
FROM ${BASE_IMAGE}
# FROM alpine:3.18

ARG MIRROR=""

Expand Down
28 changes: 0 additions & 28 deletions sapi/docker/Dockerfile-arm64

This file was deleted.

86 changes: 51 additions & 35 deletions sapi/docker/build-export-container.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/bash
#!/usr/bin/env bash

: <<'COMMENT'
从运行中的容器 将 /usr/local/swoole-cli/ 文件夹 拷贝出来 并生成新容器镜像 ,并把新容器镜像导出为文件
COMMENT

set -exu
__DIR__=$(
Expand All @@ -12,23 +18,58 @@ __PROJECT__=$(
cd ${__DIR__}
cd ${__PROJECT__}

# alpine 基础镜像 支持多架构

CONTAINER_BASE_IMAGE='docker.io/library/alpine:3.18'
CONTAIENR_NAME='swoole-cli-builder'
MIRROR=''
PLATFORM=''
ARCH=$(uname -m)
case $ARCH in
'x86_64')
PLATFORM='linux/amd64'
;;
'aarch64')
PLATFORM='linux/arm64'
;;
esac

while [ $# -gt 0 ]; do
case "$1" in
--platform)
PLATFORM="$2"
;;
--mirror)
MIRROR="$2"
;;
--*)
echo "Illegal option $1"
;;
esac
shift $(($# > 0 ? 1 : 0))
done


mkdir -p var/build-export-container/
cd ${__PROJECT__}/var/build-export-container/

test -d swoole-cli && rm -rf swoole-cli

container_id='swoole-cli-builder'
docker cp $container_id:/usr/local/swoole-cli/ .
docker cp ${CONTAIENR_NAME}:/usr/local/swoole-cli/ .

cat >Dockerfile <<'EOF'
ARG BASE_IMAGE="alpine:3.18"
FROM ${BASE_IMAGE}
# FROM alpine:3.18
cat > Dockerfile <<'EOF'
FROM alpine:3.18
ARG MIRROR=""
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV TZ=Etc/UTC
RUN test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN if [ "${MIRROR}" = "ustc" -o "${MIRROR}" = "china" ]; then { sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories ; } fi
RUN if [ "${MIRROR}" = "tuna" ]; then { sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories ; } fi
RUN apk add ca-certificates tini
ADD ./swoole-cli /usr/local/swoole-cli
Expand All @@ -42,48 +83,23 @@ ENTRYPOINT ["tini", "--"]
EOF



PLATFORM=''
ARCH=$(uname -m)
case $ARCH in
'x86_64')
PLATFORM='linux/amd64'
;;
'aarch64')
PLATFORM='linux/arm64'
;;
esac

while [ $# -gt 0 ]; do
case "$1" in
--platform)
PLATFORM="$2"
;;
--*)
echo "Illegal option $1"
;;
esac
shift $(($# > 0 ? 1 : 0))
done

ARCH=$(uname -m)
TIME=$(date -u '+%Y%m%dT%H%M%SZ')
VERSION="1.6"
TAG="v${VERSION}-${ARCH}-${TIME}"

IMAGE="docker.io/phpswoole/swoole-cli-builder:${TAG}"

docker build -t ${IMAGE} -f ./Dockerfile . --progress=plain --platform ${PLATFORM}
echo "MIRROR=${MIRROR}"
echo "BASE_IMAGE=${CONTAINER_BASE_IMAGE}"
docker build --no-cache -t ${IMAGE} -f ./Dockerfile . --progress=plain --platform ${PLATFORM} --build-arg="MIRROR=${MIRROR}" --build-arg="BASE_IMAGE=${CONTAINER_BASE_IMAGE}"

echo ${IMAGE}
echo ${IMAGE} > container-image.txt
echo ${IMAGE} >container-image.txt

IMAGE_FILE="swoole-cli-builder.tar"
docker save -o ${IMAGE_FILE} ${IMAGE}

tar -cJvf "${IMAGE_FILE}.xz" ${IMAGE_FILE}


# docker load -i "swoole-cli-builder-image.tar"

9 changes: 4 additions & 5 deletions sapi/quickstart/linux/alpine-init-minimal.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

set -exu
__DIR__=$(
Expand All @@ -10,7 +10,6 @@ cd ${__DIR__}
# use china mirror
# sh sapi/quickstart/linux/alpine-init-mini.sh --mirror [ china | ustc | tuna | tencentyun | huaweicloud ]


MIRROR=''
while [ $# -gt 0 ]; do
case "$1" in
Expand All @@ -28,8 +27,8 @@ case "$MIRROR" in
china | tuna | ustc)
test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
test "$MIRROR" = "china" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "tuna" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "ustc" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "tuna" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "ustc" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
;;
tencentyun | huaweicloud) # 云服务的内网镜像源
test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
Expand All @@ -41,4 +40,4 @@ esac

apk update

apk add bash git curl wget xz zip unzip ca-certificates
apk add bash git curl wget xz zip unzip ca-certificates
11 changes: 4 additions & 7 deletions sapi/quickstart/linux/alpine-init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

set -exu
__DIR__=$(
Expand All @@ -10,7 +10,6 @@ cd ${__DIR__}
# use china mirror
# sh sapi/quickstart/linux/alpine-init.sh --mirror [ china | ustc | tuna | tencentyun | huaweicloud ]


MIRROR=''
while [ $# -gt 0 ]; do
case "$1" in
Expand All @@ -28,8 +27,8 @@ case "$MIRROR" in
china | tuna | ustc)
test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
test "$MIRROR" = "china" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "tuna" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "ustc" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "tuna" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
test "$MIRROR" = "ustc" && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
;;
tencentyun | huaweicloud) # 云服务的内网镜像源
test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
Expand Down Expand Up @@ -66,10 +65,8 @@ china | tuna | ustc)
tencentyun | huaweicloud)
test "$MIRROR" = "tencentyun" && pip3 config set global.index-url https://mirrors.tencentyun.com/pypi/simple/
test "$MIRROR" = "huaweicloud" && pip3 config set global.index-url https://repo.huaweicloud.com/pypi/simple/
;;
esac


# pip3 install meson
apk add meson


2 changes: 1 addition & 1 deletion sapi/quickstart/linux/run-alpine-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ cd ${__DIR__}
IMAGE=alpine:3.18

cd ${__DIR__}
docker run --rm --name swoole-cli-alpine-dev -d -v ${__PROJECT__}:/work -w /work $IMAGE tail -f /dev/null
docker run --rm --name swoole-cli-alpine-dev -d -v ${__PROJECT__}:/work -w /work --init $IMAGE tail -f /dev/null
2 changes: 1 addition & 1 deletion sapi/quickstart/linux/run-debian-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ IMAGE=debian:11
IMAGE=debian:12

cd ${__DIR__}
docker run --rm --name swoole-cli-debian-dev -d -v ${__PROJECT__}:/work -w /work $IMAGE tail -f /dev/null
docker run --rm --name swoole-cli-debian-dev -d -v ${__PROJECT__}:/work -w /work --init $IMAGE tail -f /dev/null
11 changes: 0 additions & 11 deletions sapi/src/Preprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,6 @@ public function getBaseImageTag(): string
return 'base' . '-' . $arch;
}
}

public function getBaseImageDockerFile(): string
{
$arch = $this->getSystemArch();
if ($arch == 'x64') {
return 'Dockerfile';
} else {
return 'Dockerfile' . '-' . $arch;
}
}

public function setPhpSrcDir(string $phpSrcDir)
{
$this->phpSrcDir = $phpSrcDir;
Expand Down
14 changes: 12 additions & 2 deletions sapi/src/template/make.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
}

help() {
echo "./make.sh docker-build [china|ustc|tuna]"
echo "./make.sh docker-build [ china | ustc | tuna ]"
echo "./make.sh docker-bash"
echo "./make.sh docker-commit"
echo "./make.sh docker-push"
Expand Down Expand Up @@ -320,8 +320,18 @@
if [ -n "$2" ]; then
MIRROR=$2
fi
PLATFORM=''
ARCH=$(uname -m)
case $ARCH in
'x86_64')
PLATFORM='linux/amd64'
;;
'aarch64')
PLATFORM='linux/arm64'
;;
esac
cd ${__PROJECT_DIR__}/sapi/docker
docker build -t <?= Preprocessor::IMAGE_NAME ?>:<?= $this->getBaseImageTag() ?> -f <?= $this->getBaseImageDockerFile() ?> . --build-arg="MIRROR=${MIRROR}"
docker build --no-cache -t <?= Preprocessor::IMAGE_NAME ?>:<?= $this->getBaseImageTag() ?> -f Dockerfile . --build-arg="MIRROR=${MIRROR}" --progress=plain --platform=${PLATFORM}
exit 0
elif [ "$1" = "docker-bash" ] ;then
container=$(docker ps -a -f name=<?= Preprocessor::CONTAINER_NAME ?> | tail -n +2 2> /dev/null)
Expand Down

0 comments on commit f73541b

Please sign in to comment.