Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a file into a Docker container to be used with the container?

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.

like image 706
JustKash Avatar asked Oct 24 '25 14:10

JustKash


1 Answers

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.

like image 67
Mattias Wadman Avatar answered Oct 26 '25 06:10

Mattias Wadman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!