0

I'm trying to build an image of Docker through the API. I have my context in a TAR file containing the Docker file and more files and folders.

The Dockerfile has several "COPY" commands for files and folders that should be placed in the container. But this doesn't work and I don't know if it is correct to reference these files that are inside the TAR so that they can be copied later.

The structure of the TAR is approximately this:

  • context.tar
    • Dockerfile
    • /Code
      • (many py files)
    • entrypoint.sh
    • nginx.conf
    • requeriments.txt
    • ...
    • ...

Then in the Dockerfile there are some lines like this:

COPY nginx.conf /etc/nginx/conf.d/

COPY ./Code /api

Are these copies correct? The api interprets the TAR as the root path? The problem is that it doesn't give any error, nor in the logs appears anything, simply the final image is left halfway in the construction, I think when it have to copy the files from the tar to the container.

EDIT 1:

The truth is that it still doesn't work well, but I've noticed that by launching TWICE IN A RUN the build WORKS. The first time it is half-hearted, and the second time it succeeds in completing the process correctly. I'm afraid it's some kind of BUG.

Another thing I've done is to replace COPY with ADDS, although I think it's basically the same thing.

The TAR file:

TAR File content

Build request via api:

[16:09:05.650][ApiProxy ][Info ] time="2018-07-17T16:09:05+02:00" msg="proxy << POST /build?t=test%3Alatest&nocache=1&dockerfile=Dockerfile\n"

  • 1
    The [API documentation](https://docs.docker.com/engine/api/v1.37/#operation/ImageBuild) suggests your tar file has the right structure; certainly you can only `COPY` things that are in the tar file. Can you share the Dockerfile and the API output up to the point it stops? If you unpack the tar file and run `docker build` does it behave the same? – David Maze Jul 17 '18 at 10:19
  • Thanks @DavidMaze ! Yes, if I unpack the TAR and I run "docker build' it works perfectly. When I submit everything via API within the TAR, it seems that it is not able to perform the tasks. I wouldn't mind sending the Dockerfile alone, but how do I refer to those items inside the Dockerfile once I run the daemon through the API? The working directory will be another one, so I can only think of referring to files by absolute paths.... the truth is that there is no information about this. – Toni Miguel López Jul 17 '18 at 12:14

1 Answers1

1

You can't copy files which are inside one archived directory.

1st Solution : Decompress the content on your host, and do the same commands.

2d Solution: Copy to the container, like COPY/ADD context.tar /tmp and run tar -xvf /tmp/context.tar on your container. Have a good day.

GoA Oz
  • 328
  • 1
  • 13
  • So, for example, the problem is the folder "/Code" and not the files that are at the root of the Tar? I try to copy the whole "/Code" folder. Thank you very much, greetings. – Toni Miguel López Jul 17 '18 at 09:17
  • 1
    If you try to manipulate files inside .tar archive as "context.tar", you can't. You need to decompress it before. simply run tar -xvf context.tar and now you can copy the whole /Code with ADD command. – GoA Oz Jul 17 '18 at 09:22
  • I'm so sorry to insist, but there's something I don't understand, if I send the api a TAR file with only the Dockerfile, even if I have previously decompressed the rest, in that Dockerfile I'll have to refer to the files and folders with absolute paths, right? C:\...\nginx C:\..\..\Code\ and so on... ? Thanks! – Toni Miguel López Jul 17 '18 at 10:15
  • 1
    Except if the Dockerfile is in the same directory. So, for example you can use ADD ./Code /remote/path – GoA Oz Jul 17 '18 at 10:20
  • 1
    The way the `docker build` machinery works internally, it packages the current directory into a tar file, sends that to the Docker daemon, and makes a build request. The question is about how to invoke that API endpoint. In this setup, you can _only_ COPY/ADD things that are inside the tar file. – David Maze Jul 17 '18 at 10:20
  • Yes, for me it was obvious to execute docker build . when we have a DockerFile. – GoA Oz Jul 20 '18 at 15:00