Skip to content

Commit e333fa6

Browse files
nfsecGiteaBot
authored andcommitted
Dockerfile small refactor (go-gitea#27757)
- Size and layer optimization, - Maintaining consistency in definitions (comments, apk etc.),
1 parent 2d2a565 commit e333fa6

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

Dockerfile

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Build stage
1+
# Build stage
22
FROM docker.io/library/golang:1.21-alpine3.18 AS build-env
33

44
ARG GOPROXY
@@ -9,20 +9,38 @@ ARG TAGS="sqlite sqlite_unlock_notify"
99
ENV TAGS "bindata timetzdata $TAGS"
1010
ARG CGO_EXTRA_CFLAGS
1111

12-
#Build deps
13-
RUN apk --no-cache add build-base git nodejs npm
12+
# Build deps
13+
RUN apk --no-cache add \
14+
build-base \
15+
git \
16+
nodejs \
17+
npm \
18+
&& rm -rf /var/cache/apk/*
1419

15-
#Setup repo
20+
# Setup repo
1621
COPY . ${GOPATH}/src/code.gitea.io/gitea
1722
WORKDIR ${GOPATH}/src/code.gitea.io/gitea
1823

19-
#Checkout version if set
24+
# Checkout version if set
2025
RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
2126
&& make clean-all build
2227

2328
# Begin env-to-ini build
2429
RUN go build contrib/environment-to-ini/environment-to-ini.go
2530

31+
# Copy local files
32+
COPY docker/root /tmp/local
33+
34+
# Set permissions
35+
RUN chmod 755 /tmp/local/usr/bin/entrypoint \
36+
/tmp/local/usr/local/bin/gitea \
37+
/tmp/local/etc/s6/gitea/* \
38+
/tmp/local/etc/s6/openssh/* \
39+
/tmp/local/etc/s6/.s6-svscan/* \
40+
/go/src/code.gitea.io/gitea/gitea \
41+
/go/src/code.gitea.io/gitea/environment-to-ini
42+
RUN chmod 644 /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete
43+
2644
FROM docker.io/library/alpine:3.18
2745
LABEL maintainer="[email protected]"
2846

@@ -39,7 +57,8 @@ RUN apk --no-cache add \
3957
s6 \
4058
sqlite \
4159
su-exec \
42-
gnupg
60+
gnupg \
61+
&& rm -rf /var/cache/apk/*
4362

4463
RUN addgroup \
4564
-S -g 1000 \
@@ -61,10 +80,7 @@ VOLUME ["/data"]
6180
ENTRYPOINT ["/usr/bin/entrypoint"]
6281
CMD ["/bin/s6-svscan", "/etc/s6"]
6382

64-
COPY docker/root /
83+
COPY --from=build-env /tmp/local /
6584
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
6685
COPY --from=build-env /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
6786
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh
68-
RUN chmod 755 /usr/bin/entrypoint /app/gitea/gitea /usr/local/bin/gitea /usr/local/bin/environment-to-ini
69-
RUN chmod 755 /etc/s6/gitea/* /etc/s6/openssh/* /etc/s6/.s6-svscan/*
70-
RUN chmod 644 /etc/profile.d/gitea_bash_autocomplete.sh

Dockerfile.rootless

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Build stage
1+
# Build stage
22
FROM docker.io/library/golang:1.21-alpine3.18 AS build-env
33

44
ARG GOPROXY
@@ -10,19 +10,35 @@ ENV TAGS "bindata timetzdata $TAGS"
1010
ARG CGO_EXTRA_CFLAGS
1111

1212
#Build deps
13-
RUN apk --no-cache add build-base git nodejs npm
13+
RUN apk --no-cache add \
14+
build-base \
15+
git \
16+
nodejs \
17+
npm \
18+
&& rm -rf /var/cache/apk/*
1419

15-
#Setup repo
20+
# Setup repo
1621
COPY . ${GOPATH}/src/code.gitea.io/gitea
1722
WORKDIR ${GOPATH}/src/code.gitea.io/gitea
1823

19-
#Checkout version if set
24+
# Checkout version if set
2025
RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
2126
&& make clean-all build
2227

2328
# Begin env-to-ini build
2429
RUN go build contrib/environment-to-ini/environment-to-ini.go
2530

31+
# Copy local files
32+
COPY docker/rootless /tmp/local
33+
34+
# Set permissions
35+
RUN chmod 755 /tmp/local/usr/local/bin/docker-entrypoint.sh \
36+
/tmp/local/usr/local/bin/docker-setup.sh \
37+
/tmp/local/usr/local/bin/gitea \
38+
/go/src/code.gitea.io/gitea/gitea \
39+
/go/src/code.gitea.io/gitea/environment-to-ini
40+
RUN chmod 644 /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete
41+
2642
FROM docker.io/library/alpine:3.18
2743
LABEL maintainer="[email protected]"
2844

@@ -35,7 +51,8 @@ RUN apk --no-cache add \
3551
gettext \
3652
git \
3753
curl \
38-
gnupg
54+
gnupg \
55+
&& rm -rf /var/cache/apk/*
3956

4057
RUN addgroup \
4158
-S -g 1000 \
@@ -51,26 +68,23 @@ RUN addgroup \
5168
RUN mkdir -p /var/lib/gitea /etc/gitea
5269
RUN chown git:git /var/lib/gitea /etc/gitea
5370

54-
COPY docker/rootless /
71+
COPY --from=build-env /tmp/local /
5572
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
5673
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
5774
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh
58-
RUN chmod 755 /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-setup.sh /app/gitea/gitea /usr/local/bin/gitea /usr/local/bin/environment-to-ini
59-
RUN chmod 644 /etc/profile.d/gitea_bash_autocomplete.sh
6075

61-
#git:git
76+
# git:git
6277
USER 1000:1000
6378
ENV GITEA_WORK_DIR /var/lib/gitea
6479
ENV GITEA_CUSTOM /var/lib/gitea/custom
6580
ENV GITEA_TEMP /tmp/gitea
6681
ENV TMPDIR /tmp/gitea
6782

68-
#TODO add to docs the ability to define the ini to load (useful to test and revert a config)
83+
# TODO add to docs the ability to define the ini to load (useful to test and revert a config)
6984
ENV GITEA_APP_INI /etc/gitea/app.ini
7085
ENV HOME "/var/lib/gitea/git"
7186
VOLUME ["/var/lib/gitea", "/etc/gitea"]
7287
WORKDIR /var/lib/gitea
7388

7489
ENTRYPOINT ["/usr/bin/dumb-init", "--", "/usr/local/bin/docker-entrypoint.sh"]
7590
CMD []
76-

docker/root/usr/bin/entrypoint

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if [ ! -x /bin/sh ]; then
77
fi
88

99
if [ "${USER}" != "git" ]; then
10-
# rename user
10+
# Rename user
1111
sed -i -e "s/^git\:/${USER}\:/g" /etc/passwd
1212
fi
1313

@@ -19,13 +19,13 @@ if [ -z "${USER_UID}" ]; then
1919
USER_UID="`id -u ${USER}`"
2020
fi
2121

22-
## Change GID for USER?
22+
# Change GID for USER?
2323
if [ -n "${USER_GID}" ] && [ "${USER_GID}" != "`id -g ${USER}`" ]; then
2424
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*/${USER}:\1:${USER_GID}/" /etc/group
2525
sed -i -e "s/^${USER}:\([^:]*\):\([0-9]*\):[0-9]*/${USER}:\1:\2:${USER_GID}/" /etc/passwd
2626
fi
2727

28-
## Change UID for USER?
28+
# Change UID for USER?
2929
if [ -n "${USER_UID}" ] && [ "${USER_UID}" != "`id -u ${USER}`" ]; then
3030
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*:\([0-9]*\)/${USER}:\1:${USER_UID}:\2/" /etc/passwd
3131
fi

0 commit comments

Comments
 (0)