"shell" Format vs. "exec" Format

Q

What is the differences between "shell" format and "exec" format when writing instructions in Dockerfile for Windows images?

✍: FYIcenter.com

A

There are several differences between "shell" format and "exec" format when writing instructions in Dockerfile for Windows images.

1. The "shell" format is simple to use. But it does support well if any argument has a space character, like path name with spaces in it.

2. The "exec" format can protect spaces in arguments, but it uses the JSON array syntax and it is harder to use.

3. When "shell" format is used in a RUN instruction, the specified program will actually be executed with the CMD program. For example:

RUN program arg1 arg2
# Will be executed as:
#    Powershell -Command program arg1 arg2

4. When "exec" format is used in a RUN instruction, the specified program will actually be executed directly. For example:

RUN ["program", "arg1", "arg2"]
# Will be executed as:
#    program arg1 arg2

Let's watch the build history of a Dockerfile in "exec" format:

C:\fyicenter> type ExecImage

FROM openjdk
RUN ["PowerShell", "mkdir", "\"C:\\FYI Center\""]
COPY ["Hello.java", "C:/FYI Center/Hello.java"]
RUN ["javac", "C:\\FYI Center\\Hello.java"]
ENTRYPOINT ["java", "-cp", "C:\\FYI Center", "Hello"]

C:\fyicenter> docker build --file ExecImage --tag exec .
...

C:\fyicenter> docker history exec

IMAGE         CREATED BY                                      SIZE

2bffa2d267d3  powershell -Command $ErrorActionPreference =…   41kB
   -- Resulted from ENTRYPOINT ["java", "-cp", "C:\\FYI Center", "Hello"]

a30f5dd0735a  javac C:\FYI Center\Hello.java                  58.7MB
   -- Resulted from - RUN ["javac", "C:\\FYI Center\\Hello.java"]

3937cb41e4cf  powershell -Command $ErrorActionPreference =…   41.1kB
   -- Resulted from - COPY ["Hello.java", "C:/FYI Center/Hello.java"]

d3757bcbd35f  PowerShell mkdir "C:\FYI Center"                40.5MB
   -- Resulted from - RUN ["PowerShell", "mkdir", "\"C:\\FYI Center\""]
...

Now watch the build history of a Dockerfile in "shell" format:

C:\fyicenter> type ShellImage

FROM openjdk
RUN PowerShell mkdir C:\fyicenter
COPY Hello.java C:/fyicenter/Hello.java
RUN javac C:\fyicenter\Hello.java
ENTRYPOINT java -cp C:\fyicenter Hello

C:\fyicenter> docker build --file ShellImage --tag shell .
...

C:\fyicenter> docker history shell

IMAGE         CREATED BY                                      SIZE

d818dc169bd2  powershell -Command $ErrorActionPreference =…   41kB
   -- Resulted from - ENTRYPOINT java -cp C:\fyicenter Hello

fdf64e942a8d  powershell -Command $ErrorActionPreference =…   58.9MB
   -- Resulted from - RUN javac C:\fyicenter\Hello.java

719d9171fefc  powershell -Command $ErrorActionPreference =…   41.1kB
   -- Resulted from - COPY Hello.java C:/fyicenter/Hello.java

ab736679b461  powershell -Command $ErrorActionPreference =…   40.5MB
   -- Resulted from - RUN PowerShell mkdir C:\fyicenter
...

Ok, we are able to see the differences between "shell" format and "exec" format used in Dockerfile.

 

Use "ENV" Instruction on Windows Image

Spaces in Path Name on Windows Images

Building Docker Images for Windows

⇑⇑ Docker Container Platform - Tutorials

2021-11-30, 342👍, 0💬