10

I'm having a scenario wherein I need to copy a .tar file into docker container and extract the content to a specific folder inside the container. So do we have any specific commands to handle this Scenario?

Rajesh
  • 4,273
  • 1
  • 32
  • 33

3 Answers3

8

Just use ADD instruction in Dockerfile. Here is the example:

FROM ubuntu:18.04

ADD ./file.tar /root/

CMD "/bin/bash"

And then:

docker run -it --rm ubuntu:test
root@a6ce756694a1:/# ls /root/
test.file
root@a6ce756694a1:/#
Sergio
  • 823
  • 1
  • 10
  • 24
  • Beware (from official docs): "Whether a file is identified as a recognized compression format or not is done solely based on the contents of the file, not the name of the file. For example, if an empty file happens to end with .tar.gz this will not be recognized as a compressed file and will not generate any kind of decompression error message, rather the file will simply be copied to the destination." Basically `ADD` can silently "fail" if the provided `file.tar` is not actually a tar archive (as would be the case with an unresolved Git LFS pointer) – Gillespie Aug 02 '23 at 17:47
2

There are a few options, depending on your exact use case:

1. COPY directive inside Dockerfile

See documentation, this command allows you to, well, copy the file from host into your image into specified path. So every time a new container is up, it will be there (obviously, as it's part of the image).

2. Mounting a volume

If you want to pass your .tar file into specific container you could mount the volume coming from your host (or create one explicitly using docker volume create from your CLI) using --mount or -v commands like this:

$ docker run \
  --name mycontainer \
  --mount source=/path/to/folder/containing/tar/file,target=/target \
  myimage

After this operation /path/to/folder/containing/tar/file will be available to container under /target, so you can use the file via path /target/myfile.tar

I would advise for this option, as this is the more tunable approach.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
  • Thanks @Szymon Maszke – Rajesh Feb 07 '19 at 15:25
  • Mark the answer solving your issue as accepted, it will help others encountering the same problem. And provide more information (like your `Dockerfile`) next time, cheers. – Szymon Maszke Feb 07 '19 at 15:30
  • In order to get a tar from say a nexus repository will the same approach work ? I think here we will need to use ADD rather than COPY but I have read that ADD is highly discouraged. – thealchemist Sep 01 '21 at 07:05
  • @thealchemist yes, if you want to unpack tar INTP specyficznie directory you might use ADD. – Szymon Maszke Sep 01 '21 at 10:37
2

Assuming your tar file is relative to the context directory at build/distributions/my-app.tar, the following will copy the tar file to the WORKDIR, untar it, then delete it.

WORKDIR /usr/share
COPY --chown=daemon:daemon build/distributions/my-app.tar .
RUN tar -xf my-app.tar && rm my-app.tar
F. P. Freely
  • 1,026
  • 14
  • 24
  • 1
    `COPY` + `RUN` has the advantage over the top voted answer in that it will detect failures. This situation can arise if i.e. a Git LFS pointer fails to resolve in which case `ADD` will just silently add the LFS pointer instead of failing to unpack it – Gillespie Aug 02 '23 at 17:46