This post is very simple, nothing fancy. I just wanted to add one more post to my blog 🙂
The scenario is: you have a docker container on which you are doing some python development and you want to use an IDE installed on your host machine.
In my case the host machine is a MAC and I am using Xcode. I know that Xcode and python are not exactly friends, but still, I can use it for syntax highlighting. Better than nothing.
In any case, for anyone interested, the only thing necessary is to mount a host folder as a data volume on your container.
Here is how to do that
$ docker run -it -v <host folder>:<container folder> <docker image>
For example, I have a docker image called tensorflow/tutorials where I learn tensorflow
$ docker run -it -v /Users/me/Documents/My_Apps/TensorFlow/Code:/Code tensorflow
When I attach to the newly created docker container (test_container), I can go to shared folder and do any changes and they will be visible on the host computer
$ docker attach test_container
[test_container] cd /Code
[test_container /Code] touch test_file
[test_container /Code] CTRL-P CTRL-Q
$ cd ~/Documents/My_Apps/TensorFlow/Code
$ ls -a test_file
-rw-r--r-- 1 me staff 0 Jul 5 17:01 test_file
I am not sure how one can mount a host volume to an existing container. What I would do is commit my container and then run the new image with the mount instruction. Here are the commands
$ docker run -it ubuntu:14.04 /bin/bash
This will create a new container based on the ubuntu:14.04 image, without any mount points.
After you have done some work on it and the time has come to mount a volume, commit the container
$ docker commit -m "Before mounting host volume" insane_varahamihira ubuntu:14.04.01
A new images has been created
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04.01 c0fd900985b4 5 minutes ago 196.6 MB
Now run a new container from that image, while mounting a host volume to it
$ docker run -it -v /Users/me/Documents/My_Apps/TensorFlow/Code/:/Code c0fd900985b4 /bin/bash
Done.
The side effect of this is that you will now have two working container and you will most likely want to get rid of the one without the mount volume
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b3a7bbaf38d c0fd900985b4 "/bin/bash" 8 minutes ago Exited (127) 1 seconds ago big_jennings
f7215b9cc415 ubuntu:14.04 "/bin/bash" 12 minutes ago Exited (127) 2 minutes ago insane_varahamihira
$ docker rm insane_varahamihira
If one has a better solution, please share it in the comments.