1

First off, I really lack a lot of knowledge regarding Docker itself and its structure. I know that it'd be way more beneficial to learn the basics first, but I do require this to work in order to move on to other things for now.

So within a Dockerfile I installed wget & used it to download a file from a website, authentification & download are successful. However, when I later try move said file it can't be found, and it doesn't show up using e.g explorer either (path was specified) I thought it might have something to do with RUN & how it executes the wget command; I read that the Id can be used to copy it to harddrive, but how'd I do that within a Dockerfile?

RUN wget -P ./path/to/somewhere http://something.com/file.txt --username xyz --password bluh

ADD ./path/to/somewhere/file.txt /mainDirectory

Download is shown & log-in is successful, but as I mentioned I am having trouble using that file later on as it's not to be located on the harddrive. Probably a basic error, but I'd really appreciate some input that might lead to a solution.

Obviously the error is produced when trying to execute ADD as there is no file to move. I am trying to find a way to mount a volume in order to store it, but so far in vain.

Edit: Though the question is similiar to the "move to harddrive" one, I am searching for ways to get the id of the container created within the Dockerfile in order to move it; while the thread provides such answers, I haven't had any luck using them within the Dockerfile itself.

Cath
  • 460
  • 6
  • 21
  • Possible duplicate of [Docker - copy file from container to host](https://stackoverflow.com/questions/22049212/docker-copy-file-from-container-to-host) – dimo414 Jul 04 '17 at 06:19
  • I realized, that's why I am asking how one would move them; I know it's possible using cp but I haven't been able to find the id of the container used yet. – Cath Jul 04 '17 at 06:37
  • Are you trying to use the file within the Docker container, or on your host? – jdno Jul 04 '17 at 06:41
  • The file is supposed to be downloaded and moved to the main directory then, where it'll be used by the host. So far I am stuck at moving the file, as it only existed within the virtual machine. – Cath Jul 04 '17 at 06:45
  • See the question linked above. If your question is actually "How do I find the ID of an image or container" you should ask *that* question. Or just read the documentation... – dimo414 Jul 04 '17 at 06:50
  • I tried both; the solutions I found work when entered in the command line, but I am not sure on how far that differs from the Dockerfile code - I can't seem to get it to work in there, as the commands are often unrecognised. – Cath Jul 04 '17 at 06:57
  • Then share the exact commands you've tried, and the exact error messages you're getting. The more context and details you share, the better we can help you. Nowhere in your question do you mention that you've tried `docker cp`. See [How to Ask](https://stackoverflow.com/help/how-to-ask). – dimo414 Jul 04 '17 at 07:01

2 Answers2

3

Short answer is that it's not possible.

The Dockerfile builds an image, which you can run as a short-lived container. During the build, you don't have (write) access to the host and its file system. Which kinda makes sense, since you want to build an immutable image from which to run ephemeral containers.

What you can do is run a container, and mount a path from your host as a volume into the container. This is the only way how you can share files between the host and a container.

Here is an example how you could do this with the sherylynn/wget image:

docker run -v /path/on/host:/path/in/container sherylynn/wget wget -O /path/in/container/file http://my.url.com

The -v HOST:CONTAINER parameter allows you to specify a path on the host that is mounted inside the container at a specified location.

For wget, I would prefer -O over -P when downloading a single file, since it makes it really explicit where your download ends up. When you point -O to the location of the volume, the downloaded file ends up on the host system (in the folder you mounted).

Since I have no idea what your image or your environment looks like, you might need to tweak one or two things to work well with your own image. As a general recommendation: For basic commands like wget or curl, you can find pre-made images on Docker Hub. This can be quite useful when you need to set up a Continuous Integration pipeline or so, where you want to use wget or curl but can't execute it directly.

jdno
  • 4,104
  • 1
  • 22
  • 27
  • Thanks a lot! Sadly I can't change the environment since it's a task I was given, but trying the command resulted in an "Illegal Option" error, followed by returned non-zero code 2. I assume I can still use the parameters like --auth-no-challenge for authentification? – Cath Jul 04 '17 at 07:13
  • 1
    Yes, it's your plain ol' `wget` bundled/distributed in Docker. You can use `docker run sherylynn/wget wget --help` to see a list of all available options (which should be exactly the same as your local ones). – jdno Jul 04 '17 at 07:17
  • Does the same apply to a Dockerfile? I guess I'd have to use 'VOLUME' instead, but does it allow the same functionality? – Cath Jul 04 '17 at 07:18
  • 1
    No. `VOLUME` exposes a path inside a **container** (a _running_ image) to **other containers**. `docker build` is like installing an operating system: It sets up everything you need to be able to start programs (i.e. `docker run`) or share files (i.e. volumes), but you can't do that while the system is still installing. – jdno Jul 04 '17 at 07:30
  • I see. So it's simply not possible to download a file to harddrive within a Dockerfile? – Cath Jul 04 '17 at 07:34
  • 1
    Not to your host, no. There is an ongoing discussion about volumes during a Docker build, but nothing has been decided yet: https://github.com/moby/moby/issues/14080. The use cases for this feature are totally different from what you are trying to achieve, though. I strongly recommend that you catch up on at least some basics to understand how to accomplish whatever it is you are trying to do... – jdno Jul 04 '17 at 07:47
  • Thanks, I guess I'll try. – Cath Jul 04 '17 at 07:50
0

Use wget -O instead of -P for specific file download

for e.g.,

RUN wget -O /tmp/new_file.txt http://something.com --username xyz --password bluh/new_file.txt

Thanks

Navin a.s
  • 416
  • 2
  • 8
  • 19
  • Thanks for the answer, though it seems like it doesn't change the fact that the file is gone after the VM "closes". – Cath Jul 04 '17 at 07:01