I would like to pass a file from the host system to a container at runtime. I want to run a CLI tool within a container and use the file as an argument to the CLI tool. Is it possible to modify the following command: docker run -it --rm --name <container-name>
to achieve what I want to do. The docker cp
command doesn’t work for what I need since it doesn’t run from within the container and I need to pass the file name as an argument.
Asked
Active
Viewed 6,871 times
3

JustKash
- 687
- 2
- 13
- 28
-
Does this answer your question? [Copying files from host to Docker container](https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container) – leopal Feb 20 '20 at 07:10
-
`docker cp` command doesn’t quite do what I want since it is used from the host system. – JustKash Feb 20 '20 at 07:37
1 Answers
14
I usually use the -v "$PWD:$PWD" -w "$PWD"
trick. Run container and volume mount the current host working directory into the container at the same path and set working directory to same path.
So for example if I want to transcode a wav file on the host to a mp3 file usig ffmpeg running in a container I would do:
docker run --rm -v "$PWD:$PWD" -w "$PWD" mwader/static-ffmpeg:4.2.2 -i file.wav file.mp3
You can also add -u $UID:$GROUPS
if your unsure what default user the image runs as.

Mattias Wadman
- 11,172
- 2
- 42
- 57
-
(Consider whether it's better to just run the tool directly on the host, outside of Docker: the command line glue to get a Docker container to work on local files is longer than the command itself, especially if you do need to also manually specify the user ID.) – David Maze Feb 20 '20 at 11:47
-
1Ah yes should! use docker if it's easier because packaging, try different versions etc. For a shell you could make it smooth with an alias `alias ffmpeg='docker run --rm -v "$PWD:$PWD" -w "$PWD" mwader/static-ffmpeg:4.2.2'` – Mattias Wadman Feb 20 '20 at 11:51