2

Is it possible to change the volume local path?

Using Kitematic, I can do it. But I don't find the way to do it from cli.

For instance, I run docker using: docker run --name nodejs-environment -v $(pwd):/code -it node:9.3.0 alpine sh

I would like to reuse the container but change the volume, if possible.

efxzsh
  • 53
  • 1
  • 6

1 Answers1

2

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
Zac Delventhal
  • 3,543
  • 3
  • 20
  • 26