gsscoder
gsscoder

Reputation: 3392

BATCH script doesn't work in Dockerfile build process

I've a simple BATCH script script that perform a SHA256 checksum test.

@echo off
setlocal enabledelayedexpansion

::Batch script to compare a file SHA256 checksum to a given one.
::Usage: checksum [FILE] [VALUE]

set filepath=%1
set checksum=%2
set idx=0

for /f %%F in ('certutil -hashfile %filepath% SHA256') do (
    set "out!idx!=%%F"
    set /a idx += 1
)
set filechecksum=%out1%

if /i %checksum%==%filechecksum% (
    echo %checksum% validated.
) else (
    echo Checksum validation falied.
    exit 1
)

I invoke it in a Dockerfile to validate itself:

FROM mcr.microsoft.com/windows/servercore:1909-amd64

COPY checksum.bat /
RUN /checksum.bat /checksum.bat 2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec

This is the output I get:

$ docker image build -t gsscoder/test:v0_0 .

Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM mcr.microsoft.com/windows/servercore:1909-amd64
 ---> 9c2075893694
Step 2/3 : COPY checksum.bat /
 ---> 2224010db712
Step 3/3 : RUN /checksum.bat /checksum.bat 2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec
 ---> Running in eb60dd5fe468
Checksum validation falied.
The command 'cmd /S /C /checksum.bat /checksum.bat 2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec' returned a non-zero code: 1

If I remove the RUN command and try it directly in the container or the host OS I've the exepcted correct result:

.\checksum.bat .\checksum.bat 2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec

2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec validated.

Why string comparison or something else fail during the build process?

I'm very curious to know.

Thanks in advance for any help!

P.S.: I know I can do by other means (like using PowerShell), but I want know why in this way it's not working.

Upvotes: 3

Views: 1470

Answers (1)

jeb
jeb

Reputation: 82287

If your code fails, it's always a good idea to add some debug output.

Then you see that a RUN /checksum.bat ... results into a slightly different output.

out1 isn't set, because crtutil ... /checksum.bat failed with

CertUtil: Unknown arg: /checksum.bat

All you have to change:

RUN /checksum.bat C:\checksum.bat 2b9dadc0b74dc25bd94552817b68c57df9c324ba28656c10ec787adf5611f3ec

Upvotes: 4

Related Questions