Reputation: 395
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.
# 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.
# 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)
Upvotes: 0
Views: 4778
Reputation: 5547
The whole idea behind containers is to encapsulate small tasks that are reusable. The containers should be transient, meaning, I should be able to delete the container and create new one without loosing data (all data should be outside the container)
If your containers follow this approach, you can run in the following way.
docker run -v <from2>:/<to> <NAME> bash -c "<my-bash-command>"
docker run -v <from3>:/<to> <NAME> bash -c "<my-bash-command>"
From the nature of the question and what you are trying to do I can understand that the container has internal state on which you depend on the subsequent commands, and this is the root-cause of the problem.
From the commands that are shared, I don't see anything that is depending between the containers, (ex. volumes, ports, etc.), so nothing preventing you to run the containers as follows:
# First Time
docker run -dit -v <from1>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"
# Any following time:
docker run -dit -v <from2>:/<to> --name <NAME2> <IMAGE>
docker exec <NAME2> bash -c "<my-bash-command>"
If you have dependancies, maybe the dependancies should be in another container and then both the running container and the new container can link to the dependency container and consume the information that is required. You can use file system, network services, etc. to link the containers.
Upvotes: 0