Alex
Alex

Reputation: 44265

How can I copy a file to a docker image?

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?

Upvotes: 2

Views: 4592

Answers (2)

dejanualex
dejanualex

Reputation: 4328

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

Upvotes: 6

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9174

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}

Upvotes: 0

Related Questions