2

I am pulling a docker image and run this docker image on a Linux environment like

docker pull ${IMAGE}
# I need to copy the file BEFORE I run the thing
docker run ... ${IMAGE}

But how can I copy a file from the host to the docker image that I am about to run, so that when it runs it can use this file from the host?

I looked at docker cp but this seems to use a docker container ID which I do not have. I also do not want to create a new image. I need the docker container have access to one single file on the host system.

Or the other way around also would work: How can I access a file on the host system from within the docker container?

13
  • 3
    Write a Dockerfile that lets you build a new image with the file in it from the base image? If the base image is runnable without the file you could alternatively run the base image, copy in the file via a bind or volume mount and commit the change as a new image. Commented Jun 10, 2020 at 7:56
  • That is not the solution. I need to have access to one dynamic file. I do not want to build a new docker image. Maybe it is impossible what I want to do? Commented Jun 10, 2020 at 7:57
  • I do not want to commit any new change. This is a one- run example. It runs once, then gets discarded Commented Jun 10, 2020 at 7:58
  • 1
    Then just do the first part of the second suggestion, see e.g. docs.docker.com/storage/bind-mounts Commented Jun 10, 2020 at 7:59
  • 1
    When you start the container , just mount a volume (with the file already in): docker run --mount type=bind,source=<HOST_PATH>,target=<CONTAINER_PATH> IMAGE Commented Jun 10, 2020 at 8:05

2 Answers 2

6

If it helps you can try to just mount a volume (with the file already in) when you start the container:

docker run -v <HOST_PATH>:<CONTAINER_PATH> <IMAGE_NAME>

or using mount:

docker run --mount type=bind,source=<HOST_PATH>,target=<CONTAINER_PATH> <IMAGE>

Example bellow:

enter image description here

Documentation about bind-mount and volumes: https://docs.docker.com/storage/volumes/

docker version: Docker version 18.09.1, build 4c52b90

As a side note:

Bind-mount = file/dir from host referenced by full path on host machine, bind-mount can be modified by any process besides Docker. The advantage is that the if the file/dir doesn't exist on host, docker engine will create it on the host

Volume = the host filesystem also stores volumes, but the difference is that Docker completely manages them and stores them in docker's storage directory on host machine

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer, but I get an error "unknown flag: --mount"
@Alex then you should check what version of Docker you're running, I see --mount listed under docker run --help
For me it is the -z option! Maybe you can mention that in the answer for older docker versions
@Alex it's been mount since at least 17.06, so instead you should be upgrading.
I wish I could update ;-) But it is working! That is all that counts
0

Please use something like this.

docker run --rm -it --volume="<your_file_path_on_Host>:/<CONTAINER_PATH>" ${IMAGE_NAME}

or another way

docker run --rm -it -v "<your_file_path_on_Host>:/<CONTAINER_PATH>" ${IMAGE_NAME}

2 Comments

--mount has been recommended over -v/--volume for a while now
@jonrsharpe I know but the guy said it did not worked for him so gave the alternative ways to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.