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, 5519🔥, 0💬
Popular Posts:
How To Use an Array as a Queue in PHP? A queue is a simple data structure that manages data elements...
How To Open Standard Output as a File Handle in PHP? If you want to open the standard output as a fi...
How To Get the Minimum or Maximum Value of an Array in PHP? If you want to get the minimum or maximu...
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
How To Set session.gc_divisor Properly in PHP? As you know that session.gc_divisor is the frequency ...