Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up docker build #685

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ ASPNETCORE_ENVIRONMENT=""
AzureAd__ClientID=""
AzureAd__ClientSecret=""
AzureAd__TenantID=""
AzureAd__RedirectUri=""
dneed-nimble marked this conversation as resolved.
Show resolved Hide resolved
TestOverride__CypressTestSecret=""
ConnectionStrings__AcademiesDb=""
ConnectionStrings__DefaultConnection=""
52 changes: 42 additions & 10 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,70 @@ ARG DOTNET_VERSION=8.0
# Set the major version of nodejs
ARG NODEJS_VERSION_MAJOR=22

# Build frontend assets using node js
# Note: For optimal Docker caching, ensure all steps within a build stage are arranged in order of "least likely to be impacted by changes"

### Build frontend assets using node js ###
FROM "node:${NODEJS_VERSION_MAJOR}-bullseye-slim" AS assets
WORKDIR /app
COPY ./DfE.FindInformationAcademiesTrusts/ /app
RUN npm install

# - Copy over and restore npm packages separately so they can be cached by Docker
COPY ./DfE.FindInformationAcademiesTrusts/package.json /app
COPY ./DfE.FindInformationAcademiesTrusts/package-lock.json /app
RUN npm ci

# - Only copy over frontend assets so this can be cached independently to other app changes
COPY ./DfE.FindInformationAcademiesTrusts/webpack.config.js /app
COPY ./DfE.FindInformationAcademiesTrusts/assets/ /app/assets/
RUN npm run build

# Build the app using the dotnet SDK
### Build the app using the dotnet SDK ###
FROM "mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}-azurelinux3.0" AS build
WORKDIR /build
COPY . /build

# - Copy over csprojs and restore nuget packages separately so they can be cached by Docker
COPY ./DfE.FindInformationAcademiesTrusts.Data.Hardcoded/DfE.FindInformationAcademiesTrusts.Data.Hardcoded.csproj /build/DfE.FindInformationAcademiesTrusts.Data.Hardcoded/
COPY ./DfE.FindInformationAcademiesTrusts.Data.FiatDb/DfE.FindInformationAcademiesTrusts.Data.FiatDb.csproj /build/DfE.FindInformationAcademiesTrusts.Data.FiatDb/
COPY ./DfE.FindInformationAcademiesTrusts.Data/DfE.FindInformationAcademiesTrusts.Data.csproj /build/DfE.FindInformationAcademiesTrusts.Data/
COPY ./DfE.FindInformationAcademiesTrusts.Data.AcademiesDb/DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.csproj /build/DfE.FindInformationAcademiesTrusts.Data.AcademiesDb/
COPY ./DfE.FindInformationAcademiesTrusts/DfE.FindInformationAcademiesTrusts.csproj /build/DfE.FindInformationAcademiesTrusts/
RUN ["dotnet", "restore", "DfE.FindInformationAcademiesTrusts"]

# - Only copy over relevant C# code so this can be cached independently to other app changes
COPY ./DfE.FindInformationAcademiesTrusts.Data.Hardcoded/ /build/DfE.FindInformationAcademiesTrusts.Data.Hardcoded/
COPY ./DfE.FindInformationAcademiesTrusts.Data.FiatDb/ /build/DfE.FindInformationAcademiesTrusts.Data.FiatDb/
COPY ./DfE.FindInformationAcademiesTrusts.Data/ /build/DfE.FindInformationAcademiesTrusts.Data/
COPY ./DfE.FindInformationAcademiesTrusts.Data.AcademiesDb/ /build/DfE.FindInformationAcademiesTrusts.Data.AcademiesDb/
COPY ./DfE.FindInformationAcademiesTrusts/ /build/DfE.FindInformationAcademiesTrusts/
RUN ["dotnet", "build", "DfE.FindInformationAcademiesTrusts", "--no-restore", "-c", "Release"]

RUN ["dotnet", "publish", "DfE.FindInformationAcademiesTrusts", "--no-build", "-o", "/app"]

# Install SQL tools to allow migrations to be run
### Install SQL tools to allow migrations to be run ###
FROM "mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}-azurelinux3.0" AS base
RUN curl "https://packages.microsoft.com/config/rhel/9/prod.repo" | tee /etc/yum.repos.d/mssql-release.repo
ENV ACCEPT_EULA=Y
RUN ["tdnf", "update"]
RUN ["tdnf", "install", "-y", "mssql-tools18"]
RUN ["tdnf", "clean", "all"]

# Build a runtime environment
### Build a runtime environment ###
FROM base AS runtime
WORKDIR /app
LABEL org.opencontainers.image.source="https://github.com/DFE-Digital/find-information-about-academies-and-trusts"
COPY --from=build /app /app
COPY --from=assets /app/wwwroot /app/wwwroot
COPY ./DfE.FindInformationAcademiesTrusts.Data.FiatDb/Migrations/FiatDbMigrationScript.sql /app/sql/FiatDbMigrationScript.sql

# - Copy and configure docker entrypoint script
COPY ./docker/web-docker-entrypoint.sh /app/docker-entrypoint.sh
RUN ["chmod", "+x", "/app/docker-entrypoint.sh"]

# - Copy and configure sql migration script
COPY ./DfE.FindInformationAcademiesTrusts.Data.FiatDb/Migrations/FiatDbMigrationScript.sql /app/sql/FiatDbMigrationScript.sql
RUN ["touch", "/app/sql/FiatDbMigrationScriptOutput.txt"]
RUN chown "$APP_UID" "/app/sql" -R

# - Copy frontend assets
COPY --from=assets /app/wwwroot /app/wwwroot

# - Copy dotnet app
COPY --from=build /app /app

USER $APP_UID
1 change: 0 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.8"
name: fiat
services:
webapp:
Expand Down
13 changes: 10 additions & 3 deletions docs/docker-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

## On Windows

There can be some issues with running Docker on Windows. If your container doesn't start or you see an error like this `/bin/bash^M: bad interpreter: No such file or directory` it may be caused by line endings in the bash shell script. To solve this you can run this command in git bash (or another shell of your choice) from the playwright directory:
There can be some issues with running Docker on Windows. If your container doesn't start or you see an error like this `/bin/bash^M: bad interpreter: No such file or directory` it may be caused by line ending differences in the bash shell script (which we've been unable to fix in the remote repository).

`sed -i -e 's/\r$//' ../../docker/web-docker-entrypoint.sh`
1. Open git bash
2. Navigate to the docker directory
3. Rewrite your line endings by running:

This will re write your line endings. The repo will ignore this so checking in the updated file will not fix it unfortunately.
```bash
sed -i -e 's/\r$//' web-docker-entrypoint.sh
```

4. Stage your changes (they will disappear after being staged)
5. Rebuild and run the Docker container - it should now work

## On Macs

Expand Down
Loading