Docker – Mount Volume while executing the container

  docker

I use a docker container, where i dynamically want to mount a volume. So i want every time i invoke "exec" to mount a different host-path. this is currently not possible.

My current method (Static):

# First Time
docker run -dit -v <from1>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"

# Any following time:
docker stop <NAME>
docker rm <NAME>
docker run -dit -v <from2>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"

So currently i have to stop, remove and recreate the entire container just because i have a different "from" path.

I hope there is a way that i could create and already start the container in the background, and just during a command execution mount the volume.

Example (pseudo code, this wont work)

# First Time
docker run -dit --name <NAME> <IMAGE>
docker exec -v <from1>:/<to> <NAME> bash -c "<my-bash-command>"

# Any following time:
docker exec -v <from2>:/<to> <NAME> bash -c "<my-bash-command>"
docker exec -v <from3>:/<to> <NAME> bash -c "<my-bash-command>"
...

Is there a solution for this? Because i need to keep the same container and i dont want to create a new container every time a run a command (as i will use persistent data inside the container, which get tossed away if i remove the container)

Source: Docker Questions

LEAVE A COMMENT