Skip to content

Commit

Permalink
Replace build arguments with docker/compose secrets
Browse files Browse the repository at this point in the history
Docker's documentation [suggests](https://docs.docker.com/reference/dockerfile/#arg) not to use build arguments to pass secrets, so this change updates the `Dockerfile` to use [secret mounts](https://docs.docker.com/build/building/secrets/#secret-mounts), and the recommended way to [manage secrets in docker compose](https://docs.docker.com/compose/how-tos/use-secrets/).
  • Loading branch information
spikeheap committed Oct 8, 2024
1 parent ca17837 commit 3c4169c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Hackney.Shared.Tenure.Tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0
# disable microsoft telematry
ENV DOTNET_CLI_TELEMETRY_OPTOUT='true'

ARG LBHPACKAGESTOKEN
ENV LBHPACKAGESTOKEN=$LBHPACKAGESTOKEN
WORKDIR /app

# Copy csproj and restore as distinct layers
Expand All @@ -13,8 +11,16 @@ COPY ./Hackney.Shared.Tenure/Hackney.Shared.Tenure.csproj ./Hackney.Shared.Tenur
COPY ./Hackney.Shared.Tenure.Tests/Hackney.Shared.Tenure.Tests.csproj ./Hackney.Shared.Tenure.Tests/
COPY /nuget.config /root/.nuget/NuGet/NuGet.Config

RUN dotnet restore ./Hackney.Shared.Tenure/Hackney.Shared.Tenure.csproj
RUN dotnet restore ./Hackney.Shared.Tenure.Tests/Hackney.Shared.Tenure.Tests.csproj
# We mount secrets so they can't end up in logs or build layers.
# We chain both restore commands so we only make the token available
# once and don't store it elsewhere.
# see:
# - https://docs.docker.com/reference/dockerfile/#arg
# - https://docs.docker.com/compose/how-tos/use-secrets/
RUN --mount=type=secret,id=LBHPACKAGESTOKEN \
export LBHPACKAGESTOKEN=$(cat /run/secrets/LBHPACKAGESTOKEN) && \
dotnet restore ./Hackney.Shared.Tenure/Hackney.Shared.Tenure.csproj && \
dotnet restore ./Hackney.Shared.Tenure.Tests/Hackney.Shared.Tenure.Tests.csproj

# Copy everything else and build
COPY . .
Expand Down
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ services:
build:
context: .
dockerfile: Hackney.Shared.Tenure.Tests/Dockerfile
args:
- LBHPACKAGESTOKEN=${LBHPACKAGESTOKEN}
secrets:
- LBHPACKAGESTOKEN

# see https://docs.docker.com/compose/how-tos/use-secrets/#build-secrets
# Combines with a "secrets" block in each service to expose it as a file in
# /run/secrets/, e.g. /run/secrets/LBHPACKAGESTOKEN
secrets:
LBHPACKAGESTOKEN:
environment: LBHPACKAGESTOKEN

0 comments on commit 3c4169c

Please sign in to comment.