Tools, FAQ, Tutorials:
Run CMD Commands in Dockerfile
How to run CMD Commands in Dockerfile to change Windows Docker images?
✍: FYIcenter.com
When building a new Windows image, you can only run executable programs
that are already installed on the image.
A Windows image usually provides programs in the C:\Windows\System32 directory, including two commonly used programs CMD.exe and PowerShell.exe:
C:\Windows\System32\cmd.exe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
So if you are good at using CMD.exe, you can use it to change the Windows image by using the "RUN CMD ..." instruction in the Dockerfile.
Since you can not run CMD.exe interactively the Dockerfile, you must run CMD.exe with its sub-command using the /S switch in a single command line, like:
RUN CMD /C DIR
1. Enter the following Dockerfile, CmdImage, to modify image with the CMD.exe program.
C:\fyicenter> type CmdImage FROM microsoft/dotnet-samples ENV HOME="C:\\fyicenter" RUN CMD /C ECHO %HOME% RUN CMD /C ECHO %PATH% RUN CMD /C DIR ENTRYPOINT CMD /S /C ECHO "Hello world!"
2. Build image with the Dockerfile:
C:\fyicenter> docker build --file CmdImage --tag cmd .
Sending build context to Docker daemon 12.88MB
Step 1/6 : FROM microsoft/dotnet-samples
---> afca1083bf22
Step 2/6 : ENV HOME="C:\\fyicenter"
---> Running in b4d172dfde41
Removing intermediate container b4d172dfde41
---> bec3045ef04c
Step 3/6 : RUN CMD /C ECHO %HOME%
---> Running in 2e563e51cef5
C:\fyicenter
Removing intermediate container 2e563e51cef5
---> fe17238a0ace
Step 4/6 : RUN CMD /C ECHO %PATH%
---> Running in 8d568a470f7f
C:\Windows\system32;C:\Windows;C:\Windows\System32\WindowsPowerShell\v1.0\;...
Removing intermediate container 8d568a470f7f
---> fcd2e65963a9
Step 5/6 : RUN CMD /C DIR
---> Running in cb030c9d9f77
725 dotnetapp.deps.json
9,216 dotnetapp.dll
744 dotnetapp.pdb
154 dotnetapp.runtimeconfig.json
4,608 utils.dll
712 utils.pdb
Removing intermediate container cb030c9d9f77
---> ce83f3018986
Step 6/6 : ENTRYPOINT CMD /S /C ECHO "Hello world!"
---> Running in 6c3b3acc754f
Removing intermediate container 6c3b3acc754f
---> 222e202c2f9d
Successfully built 222e202c2f9d
Successfully tagged cmd:latest
3. Run the new image:
C:\fyicenter> docker run --name cmd cmd Hello world!
3. Remove container and image, so we can build it again with same name:
C:\fyicenter>docker rm cmd C:\fyicenter>docker image rm cmd
Ok, we are able to use CMD.exe program to modify the Windows image.
⇒ Run PowerShell Commands in Dockerfile
2022-01-24, ∼6335🔥, 0💬
Popular Posts:
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
How to create Hello-3.1.epub with WinRAR? I have all required files to create Hello-3.1.epub. To cre...
How to read RSS validation errors at w3.org? If your RSS feed has errors, the RSS validator at w3.or...
How to add request query string Parameters to my Azure API operation 2017 version to make it more us...