Sweta Sharma
Sweta Sharma

Reputation: 2764

How to download a file from URL using Dockerfile

I'm writing a docker file to install a certain library. The first step it does is download the library from a URL. I'm not sure if it's possible in docker.

I need to install the library on RedHat System. http://service.sap.com/download is the URL I need to download the library. How can I write Dockerfile for the same?

Upvotes: 11

Views: 26317

Answers (2)

savageGoat
savageGoat

Reputation: 1546

You run a RUN command depending on the programs available in your system. If you have wget on your system, you just put the command in your Dockerfile:

RUN wget http://your.destination/file

If you need that file to move to a location in your image, you keep using the RUN command with mv, if the file is outside your image, you can use the COPY command.

To resume, downloading your file from the system

[CLI] wget http://your.destination/file
[DOCKERFILE] COPY file .

Downloading a file with docker

[DOCKERFILE] RUN wget http://your.destination/file
[DOCKERFILE] RUN mv file my/destination/folder

Upvotes: 1

sage
sage

Reputation: 5514

I recommend using ADD, as @David Maze commented and as @nicobo commented on the first answer.

I think that this is the best answer for many of us, since it does not force download of wget or similar into the Docker image. Here is the example I just used for CMake:

ADD https://github.com/Kitware/CMake/releases/download/v3.27.6/cmake-3.27.6-linux-x86_64.sh /tmp/cmake.sh

RUN mkdir /opt/cmake && bash /tmp/cmake.sh --prefix=/opt/cmake --skip-license && rm /tmp/cmake.sh

Upvotes: 11

Related Questions