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.
    – jonrsharpe
    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?
    – Alex
    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
    – Alex
    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
    – jonrsharpe
    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
    – dejanualex
    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

5
  • Thanks for the answer, but I get an error "unknown flag: --mount"
    – Alex
    Jun 10, 2020 at 8:09
  • @Alex then you should check what version of Docker you're running, I see --mount listed under docker run --help
    – jonrsharpe
    Jun 10, 2020 at 8:10
  • 1
    For me it is the -z option! Maybe you can mention that in the answer for older docker versions
    – Alex
    Jun 10, 2020 at 8:13
  • @Alex it's been mount since at least 17.06, so instead you should be upgrading.
    – jonrsharpe
    Jun 10, 2020 at 8:15
  • I wish I could update ;-) But it is working! That is all that counts
    – Alex
    Jun 10, 2020 at 8:16
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
  • --mount has been recommended over -v/--volume for a while now
    – jonrsharpe
    Jun 10, 2020 at 8:26
  • 1
    @jonrsharpe I know but the guy said it did not worked for him so gave the alternative ways to do it. Jun 10, 2020 at 8:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.