Dockerfile: copy zip and open it
Asked Answered
C

2

7

I am trying to build a Windows docker image which will copy my software to the image and unzip it. I am working on Windows 10 host. The steps are:

  1. Prepare file Dockerfile. with the following lines:

    FROM mcr.microsoft.com/windows/servercore:ltsc2019

    COPY image.zip c:\image.zip

    CMD ["powershell.exe", "Expand-Archive -LiteralPath 'C:\image.zip' -DestinationPath 'c:\'"

  2. Prepare a zip file called image.zip with some files.

  3. Run command:

    docker build -t test3 .

  4. At this point the image is built. image.zip was copied to the image.

  5. Run the container:

    docker run --rm -it test3 powershell
    
  6. From the container powershell run:

    dir
    

At this point, I expect to see the content of "image.zip" which has been extracted during the build. But I don't, there is just "image.zip".

Conformance answered 30/11, 2020 at 0:57 Comment(3)
The command you provide on the docker run command line runs instead of the image's CMD. Do you mean to RUN the command to extract the archive?Partida
No, I want the file to be unzipped during the BUILD process, not the RUN. I guess this is what I am trying to find out: how to unzip a file as part of the build process using DOCKERFILE.Conformance
RUN does run as part of the build; CMD specifies the command that should be run when the container is started. See also Difference between RUN and CMD in a Dockerfile.Partida
C
9

Found a way to do it using .tar file insead of .zip and using "ADD" instead of "COPY":

The DOCKERFILE. now looks like this:

FROM mcr.microsoft.com/windows/servercore:ltsc2019
ADD test3.tar c:/

The ADD command extracts the archive.

Conformance answered 30/11, 2020 at 1:21 Comment(0)
E
7

that's true ADD instruction extract the archived files but not all formates .. for example ADD or COPY instruction does not support extracting .zip files but .tar files and some more.

I suggest to do the following:

  1. copy the zip file from it's source manually to the working directory which contains the Dockerfile
  2. use ADD instruction to copy the zip file from the source to the destination
  3. user RUN instruction after ADD instruction to extract the zip file. like:
ADD <file_name.zip> /<destination> 
RUN unzip <file_name.zip>
  1. after that continue writing your Dockerfile instructions.
Edva answered 19/3, 2021 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.