Every time you use docker run
it will rebuild your container. Instead use docker exec
to run a command in an existing container. Though unfortunately, exec will not allow you to specify new volumes either. As far as I know there is currently no way to reassign volumes in an existing container.
Your best bet is just to rebuild the container. It doesn't take long and they are meant to be fairly disposable. Alternatively, you could use volumes to point to a parent directory with all the code you'll ever need, or just use volumes to mount both directories:
docker run --name nodejs-environment \
-v $(pwd)/first_source:/first_source \
-v $(pwd)/second_source:/second_source \
-it node:9.3.0 alpine sh
But if you really need exactly this functionality, reassigning volumes after the container is already built, you could accomplish it with some hacky use of symbolic links:
ln -s first_source/ link
docker run --name nodejs-environment -v $(pwd)/link:/code -it node:9.3.0 alpine sh
Once you have finished with the first volume, you can swap out the symbolic link:
rm link
ln -s second_source/ link
Note that if you exited the terminal from docker run
, it will stop your container. So you would have to restart your container to exec into it again:
docker start nodejs-environment
docker exec -it nodejs-environment sh