Skip to content

Commit bd2932f

Browse files
author
Jiyeon Baek
committed
feat: add script for adding container tag, and use lightweight alpine linux image
1 parent 515d199 commit bd2932f

2 files changed

Lines changed: 105 additions & 45 deletions

File tree

Dockerfile

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
FROM node:24.0.2-slim
1+
FROM alpine:3.22
22

3-
ENV DEBIAN_FRONTEND noninteractive
4-
ENV PNPM_VERSION 10.11.0
5-
ENV TURBO_VERSION 2.5.3
6-
ENV TSX_VERSION 4.19.4
7-
ENV TS_NODE 10.9.2
8-
ENV SWC_CORE 1.11.24
9-
ENV AWS_CLI 2.27.19
10-
ENV BUILDKIT_VERSION 0.21.1
3+
ENV AWS_CLI=2.27.19
114

12-
RUN apt-get update -y \
13-
&& apt-get install -y --no-install-recommends \
14-
software-properties-common \
5+
RUN apk update \
6+
&& apk add --no-cache \
157
ca-certificates \
16-
build-essential \
8+
build-base \
179
wget \
1810
jq \
1911
patch \
20-
python3 \
2112
curl \
22-
unzip \
23-
git \
24-
&& apt-get clean
13+
unzip
2514

2615
# install awscli v2. see https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
2716
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWS_CLI}.zip" -o /tmp/awscliv2.zip \
@@ -30,36 +19,9 @@ RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWS_CLI}.zip" -
3019
&& rm /tmp/awscliv2.zip \
3120
&& rm -rf /tmp/aws
3221

33-
# install node packages
34-
RUN npm i -g pnpm@${PNPM_VERSION} turbo@${TURBO_VERSION} tsx@${TSX_VERSION} ts-node@${TS_NODE} @swc/core@${SWC_CORE}
35-
36-
RUN curl -L "https://github.com/moby/buildkit/releases/download/v${BUILDKIT_VERSION}/buildkit-v${BUILDKIT_VERSION}.linux-amd64.tar.gz" -o /tmp/buildkit.tar.gz \
37-
&& mkdir -p /tmp/buildkit \
38-
&& tar -C /tmp/buildkit -xzf /tmp/buildkit.tar.gz \
39-
&& mv /tmp/buildkit/bin/buildctl /usr/bin/buildctl \
40-
&& chmod a+x /usr/bin/buildctl \
41-
&& rm -rf /tmp/buildkit \
42-
&& rm /tmp/buildkit.tar.gz
43-
44-
# install kubectl
45-
RUN curl -L "https://dl.k8s.io/release/v1.30.13/bin/linux/amd64/kubectl" -o "/usr/bin/kubectl-v1.30" \
46-
&& curl -L "https://dl.k8s.io/release/v1.31.9/bin/linux/amd64/kubectl" -o "/usr/bin/kubectl-v1.31" \
47-
&& curl -L "https://dl.k8s.io/release/v1.32.5/bin/linux/amd64/kubectl" -o "/usr/bin/kubectl-v1.32" \
48-
&& chmod a+x /usr/bin/kubectl*
49-
50-
RUN ln -s /usr/bin/kubectl-v1.32 /usr/bin/kubectl
51-
52-
# install golang
53-
COPY --from=golang:1.22.0 /usr/local/go/ /usr/local/go/
54-
ENV GOPATH /go
55-
ENV PATH $GOPATH/bin:/usr/local/go/bin:/usr/bin:${PATH}
56-
5722
# install amazon-ecr-credential-helper
5823
RUN curl -L "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/0.9.1/linux-amd64/docker-credential-ecr-login" -o "/usr/bin/docker-credential-ecr-login" \
5924
&& chmod a+x /usr/bin/docker-credential-ecr-login
6025

61-
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
6226

63-
# install uv
64-
# https://docs.astral.sh/uv/reference/installer/#unmanaged-installations
65-
RUN curl -LsSf https://astral.sh/uv/install.sh | env UV_UNMANAGED_INSTALL="/usr/bin" sh
27+
COPY script /script

script/add-container-tag

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/sh -e
2+
3+
add_docker_tag() {
4+
local IMAGE_PATH="$1"
5+
local NEW_TAG="$2"
6+
local USERNAME="$3"
7+
local PASSWORD="$4"
8+
9+
local REGISTRY_URL="https://$(echo $IMAGE_PATH | cut -d'/' -f1)"
10+
local REPO_PATH=$(echo $IMAGE_PATH | cut -d'/' -f2- | cut -d':' -f1)
11+
local CURRENT_TAG=$(echo $IMAGE_PATH | cut -d':' -f2)
12+
13+
# 붙일 태그가 없으면 에러
14+
if [[ -z "$CURRENT_TAG" ]]; then
15+
echo "Error: Current tag not found in image path '$IMAGE_PATH'"
16+
return 1
17+
fi
18+
19+
if [[ -z "$NEW_TAG" ]]; then
20+
echo "Error: New tag is empty"
21+
return 1
22+
fi
23+
24+
echo "Start tagging: $REGISTRY_URL/$REPO_PATH, $CURRENT_TAG -> $NEW_TAG"
25+
26+
# 인증 헤더 설정
27+
local AUTH_HEADER=""
28+
if [[ -n "$USERNAME" && -n "$PASSWORD" ]]; then
29+
AUTH_HEADER="Authorization: Basic $(echo -n "$USERNAME:$PASSWORD" | base64)"
30+
fi
31+
32+
local MANIFEST=$(curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "$AUTH_HEADER" "$REGISTRY_URL/v2/$REPO_PATH/manifests/$CURRENT_TAG")
33+
34+
if [[ $? -ne 0 ]]; then
35+
echo "Error: Failed to fetch manifest for tag '$CURRENT_TAG'"
36+
return 1
37+
fi
38+
39+
echo "Adding new tag '$NEW_TAG'..."
40+
local RESULT=$(curl -s -X PUT \
41+
-H "Content-Type: application/vnd.docker.distribution.manifest.v2+json" \
42+
-H "$AUTH_HEADER" \
43+
-d "$MANIFEST" \
44+
"$REGISTRY_URL/v2/$REPO_PATH/manifests/$NEW_TAG")
45+
46+
if [[ $? -ne 0 ]]; then
47+
echo "Error: Failed to add tag '$NEW_TAG': $RESULT"
48+
return 1
49+
fi
50+
51+
echo "Successfully added tag '$NEW_TAG' to $IMAGE_PATH"
52+
}
53+
54+
55+
TAG_TO_ADD=$1
56+
DOCKER_REGISTRY=$2
57+
TOKEN=$3
58+
59+
if [ -z "$TAG_TO_ADD" ] || [ -z "$DOCKER_REGISTRY" ]; then
60+
echo "Usage: echo -e 'image1\\nimage2\\nimage3' | $0 <tag_to_add> <docker_registry> [token]"
61+
exit 1
62+
fi
63+
64+
# stdin에서 이미지 목록을 배열로 읽기
65+
mapfile -t IMAGES
66+
67+
if [ ${#IMAGES[@]} -eq 0 ]; then
68+
echo "Error: No images found"
69+
exit 1
70+
fi
71+
72+
# if ECR
73+
if [[ "$DOCKER_REGISTRY" == *".dkr.ecr."* ]]; then
74+
TOKEN=$(aws ecr get-login-password --region us-east-1)
75+
fi
76+
77+
if [[ -z "$TOKEN" && "$DOCKER_REGISTRY" == *".dkr.ecr."* ]]; then
78+
echo "Token is empty"
79+
exit 1
80+
fi
81+
82+
for IMAGE in "${IMAGES[@]}"; do
83+
if [[ $IMAGE != *"$DOCKER_REGISTRY"* ]]; then
84+
echo "Skipping: $IMAGE (not from $DOCKER_REGISTRY)"
85+
continue
86+
fi
87+
88+
if [[ $IMAGE == *".dkr.ecr."* ]]; then
89+
USERNAME="AWS"
90+
else
91+
USERNAME=$(echo "$IMAGE" | cut -d'/' -f1)
92+
fi
93+
94+
echo "add_docker_tag $IMAGE $TAG_TO_ADD $USERNAME ****"
95+
add_docker_tag "$IMAGE" "$TAG_TO_ADD" "$USERNAME" "$TOKEN" &
96+
done
97+
wait
98+

0 commit comments

Comments
 (0)