connorcarnes / pwshCloudflare

MIT License
2 stars 1 forks source link

Improve Dockerfile #5

Open connorcarnes opened 7 months ago

connorcarnes commented 7 months ago

This is the current Dockerfile:

FROM mcr.microsoft.com/devcontainers/base:ubuntu
# https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.4
COPY bootstrap.ps1 bootstrap.ps1
RUN apt-get update \
    && apt-get install -y wget apt-transport-https software-properties-common \
    && wget -q https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && rm packages-microsoft-prod.deb \
    && apt-get update \
    && apt-get install -y powershell \
    && pwsh -File bootstrap.ps1
COPY profile.ps1 /opt/microsoft/powershell/7/profile.ps1

It works fine so far but the below pseudo Dockerfile may be better:

FROM mcr.microsoft.com/devcontainers/base:ubuntu
# https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.4
RUN apt-get update \
    && apt-get install -y wget apt-transport-https software-properties-common \
    && wget -q https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && rm packages-microsoft-prod.deb \
    && apt-get update 
RUN pwsh -File <path_to_.devcontainer/bootstrap.ps1>
COPY profile.ps1 /opt/microsoft/powershell/7/profile.ps1

Desired outcome is that first RUN statement gets cached. It takes the majority of the build time but changes the least. Bootstrap.ps1 in second RUN statement changes frequently. Need to test as I'm not sure if updating bootstrap.ps1 would trigger a rebuild of the layer. CMD or ENTRYPOINT may be more appropriate than second RUN statement.

Additionally, current Dockerfile executes bootstrap.ps1 as root user. It might be better to have it execute as vscode user as that's the user available in the devcontainer.

connorcarnes commented 7 months ago

Updated version of the dockerfile is below. It introduces a minor inconvenience that should be an easy fix but it's low priority atm.

Container build will fail unless user creates devCfConfig.xml. Normally you could use an ARG statement to prompt the user but I don't think it works like that in dev containers vscode extension. but I could be wrong. Might be able to do something in the devcontainer.json that only executes the COPY statement if the file is detected (if this is true I think the copy statement would be removed from the dockerfile and it'd all be handled in the devcontainer.json).

FROM mcr.microsoft.com/devcontainers/base:ubuntu
# https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.4
# Create an empty devCfConfig.xml in /.devcontainer directory even if you're not using it
# devCfConfig.xml contains secrets and should not be committed to source control (it's in .gitignore)
# https://github.com/connorcarnes/pwshCloudflare/issues/5
COPY devCfConfig.xml /home/vscode/.pwshCloudflare/config.xml
COPY bootstrap.ps1 bootstrap.ps1
RUN apt-get update \
    && apt-get install -y wget apt-transport-https software-properties-common \
    && wget -q https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && rm packages-microsoft-prod.deb \
    && apt-get update \
    && apt-get install -y powershell \
    && pwsh -File bootstrap.ps1
COPY profile.ps1 /opt/microsoft/powershell/7/profile.ps1