Skip to content

Commit eae3c0b

Browse files
committed
Add build arguments for conditional non-root user creation
Updated Dockerfile and Dockerfile.local.wsl to introduce build arguments for user creation, enabling the conditional creation of a non-root user. The default user is set to `root` with a user ID of `0`, which can be overridden during the build process. User context is switched to the specified user after installations. The `run-local-build-using-docker-image.sh` script has also been modified to support these changes.
1 parent e836fbf commit eae3c0b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

Dockerfile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
1+
# Use build arguments to control user creation
2+
ARG NON_ROOT_USER=false
3+
ARG USERNAME=root
4+
ARG USERID=0
5+
26
FROM ubuntu:20.04
37

4-
#install .net
8+
# Install .NET
59
RUN apt-get update && apt-get install -y wget apt-transport-https && \
610
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb && \
711
dpkg -i packages-microsoft-prod.deb && \
@@ -16,14 +20,23 @@ RUN set -xe \
1620
&& apt-get purge --auto-remove \
1721
&& apt-get clean
1822

23+
# Conditionally create non-root user and set permissions
24+
RUN if [ "$NON_ROOT_USER" = "true" ]; then \
25+
adduser --disabled-password --gecos '' --uid $USERID $USERNAME && \
26+
mkdir -p /workdir /app && \
27+
chown -R $USERNAME:$USERNAME /workdir /app; \
28+
fi
1929

30+
# Switch to the appropriate user
31+
USER $USERNAME
2032

2133
# Install SDKMAN
2234
RUN curl -s "https://get.sdkman.io" | bash && \
2335
echo "source $HOME/.sdkman/bin/sdkman-init.sh" >> $HOME/.bashrc && \
2436
bash -c "source $HOME/.sdkman/bin/sdkman-init.sh && sdk install java 17.0.9-oracle && sdk install scala 3.3.0 && sdk install sbt 1.9.0"
2537

26-
# Install GNAT and SPARK from AdaCore
38+
# Install GNAT and SPARK (temporarily switch back to root)
39+
USER root
2740
WORKDIR /gnat_tmp/
2841
RUN wget -O gnat-2021-x86_64-linux-bin https://community.download.adacore.com/v1/f3a99d283f7b3d07293b2e1d07de00e31e332325?filename=gnat-2021-20210519-x86_64-linux-bin \
2942
&& git clone https://github.com/AdaCore/gnat_community_install_script.git \
@@ -32,5 +45,7 @@ RUN wget -O gnat-2021-x86_64-linux-bin https://community.download.adacore.com/v1
3245
&& gnat_community_install_script/install_package.sh ./gnat-2021-x86_64-linux-bin /opt/GNAT/gnat-x86-2021 \
3346
&& rm -rf /gnat_tmp/
3447

48+
# Set back to the appropriate user
49+
USER $USERNAME
3550
WORKDIR /app/
3651
ENV PATH="/opt/GNAT/gnat-x86-2021/bin:${PATH}"

Dockerfile.local.wsl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
1+
# Use build arguments to control user creation
2+
ARG NON_ROOT_USER=false
3+
ARG USERNAME=root
4+
ARG USERID=0
5+
26
FROM ubuntu:20.04
37

4-
#install .net
8+
# Install .NET
59
RUN apt-get update && apt-get install -y wget apt-transport-https && \
610
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb && \
711
dpkg -i packages-microsoft-prod.deb && \
@@ -16,21 +20,22 @@ RUN set -xe \
1620
&& apt-get purge --auto-remove \
1721
&& apt-get clean
1822

19-
# Create a non-root user
20-
RUN adduser --disabled-password --gecos '' --uid 1000 myuser
21-
22-
# Adjust permissions for volumes
23-
RUN mkdir -p /workdir /app && chown -R myuser:myuser /workdir /app
23+
# Conditionally create non-root user and set permissions
24+
RUN if [ "$NON_ROOT_USER" = "true" ]; then \
25+
adduser --disabled-password --gecos '' --uid $USERID $USERNAME && \
26+
mkdir -p /workdir /app && \
27+
chown -R $USERNAME:$USERNAME /workdir /app; \
28+
fi
2429

25-
# Switch to the non-root user
26-
USER myuser
30+
# Switch to the appropriate user
31+
USER $USERNAME
2732

2833
# Install SDKMAN
2934
RUN curl -s "https://get.sdkman.io" | bash && \
3035
echo "source $HOME/.sdkman/bin/sdkman-init.sh" >> $HOME/.bashrc && \
3136
bash -c "source $HOME/.sdkman/bin/sdkman-init.sh && sdk install java 17.0.9-oracle && sdk install scala 3.3.0 && sdk install sbt 1.9.0"
3237

33-
# Install GNAT and SPARK from AdaCore (still as root since no SDKMAN required here)
38+
# Install GNAT and SPARK (temporarily switch back to root)
3439
USER root
3540
WORKDIR /gnat_tmp/
3641
RUN wget -O gnat-2021-x86_64-linux-bin https://community.download.adacore.com/v1/f3a99d283f7b3d07293b2e1d07de00e31e332325?filename=gnat-2021-20210519-x86_64-linux-bin \
@@ -40,7 +45,7 @@ RUN wget -O gnat-2021-x86_64-linux-bin https://community.download.adacore.com/v1
4045
&& gnat_community_install_script/install_package.sh ./gnat-2021-x86_64-linux-bin /opt/GNAT/gnat-x86-2021 \
4146
&& rm -rf /gnat_tmp/
4247

43-
# Set back to the non-root user for remaining tasks
44-
USER myuser
48+
# Set back to the appropriate user
49+
USER $USERNAME
4550
WORKDIR /app/
46-
ENV PATH="/opt/GNAT/gnat-x86-2021/bin:${PATH}"
51+
ENV PATH="/opt/GNAT/gnat-x86-2021/bin:${PATH}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
2-
#docker build -f Dockerfile.local.wsl -t asn1scc .
2+
#docker build -f Dockerfile.local.wsl --build-arg NON_ROOT_USER=true --build-arg USERNAME=myuser --build-arg USERID=1000 -t myimage .
33
docker run -ti --rm -v .:/app -v asn1scc_workdir:/workdir asn1scc bash -c "./local-build.sh $(git rev-parse --abbrev-ref HEAD)"

0 commit comments

Comments
 (0)