Docker build ADD vs RUN curl

19,944

ADD is executed in docker host.

The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

RUN is executed inside your container.

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

Specifically the command RUN curl -o file.txt http://X.X.X.X/path/to/file/file.txt executes curl that must have already been installed in the image we are using. If the curl command has not been installed (and is not present in the base image) the entire RUN command fails. Instead the command ADD url can be performed even without having installed curl (or analogues) inside the container just because it is executed by the host (it uses the Go libraries with which it is written docker) during the creation of our image.


Is http://X.X.X.X/path/to/file/file.txt accessible outside of your docker container?

Edit: as confirmed by the author of the question:

My docker host lives behind a firewall that has a proxy set in the /etc/default/docker file. So while I wanted to grab a file internal to the network I'm on, the proxy caused it to look outside the network.

Share:
19,944
Erik Nguyen
Author by

Erik Nguyen

Updated on July 19, 2022

Comments

  • Erik Nguyen
    Erik Nguyen almost 2 years

    If I run a dockerfile where I have a command like

    RUN curl -o file.txt http://X.X.X.X/path/to/file/file.txt
    

    the build works, whereas if I use

    ADD http://X.X.X.X/path/to/file/file.txt file.txt
    

    The build fails and it complains about

    Got HTTP status code >= 400: 503 Service Unavailable
    

    Is there something about ADD that I'm not understanding?

    Edit the file is also accessible through the docker host.

  • Erik Nguyen
    Erik Nguyen almost 9 years
    Oh didn't know that, good to know. And yes, it is definitely accessible through curl on the docker host.
  • Erik Nguyen
    Erik Nguyen almost 9 years
    Thank you fox91 for the hint as to the cause. My docker host lives behind a firewall that has a proxy set in the /etc/default/docker file. So while I wanted to grab a file internal to the network I'm on, the proxy caused it to look outside the network. The fact that ADD is executed using the docker host environment was important in diagnosing this issue.
  • prashant
    prashant over 4 years
    can you add reference/documentation to it
  • fox91
    fox91 over 4 years
    Hi @prashant, I've updated the answer with a more complete and in-depth explanation.
  • fox91
    fox91 over 4 years
    Hey @ErikNguyen, can you confirm my answer? Thanks