Docker build ADD vs RUN curl
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 theDockerfile
.
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.
Erik Nguyen
Updated on July 19, 2022Comments
-
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 almost 9 yearsOh didn't know that, good to know. And yes, it is definitely accessible through curl on the docker host.
-
Erik Nguyen almost 9 yearsThank 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 over 4 yearscan you add reference/documentation to it
-
fox91 over 4 yearsHi @prashant, I've updated the answer with a more complete and in-depth explanation.
-
fox91 over 4 yearsHey @ErikNguyen, can you confirm my answer? Thanks