Docker cheat sheet: running files

I am learning a little bit of Docker for a couple of personal projects. One of the things that has been difficult to me is the documentation of all the possibilities of Docker. Don't get me wrong, Docker has so many options and features because it is so powerful.

Docker container are basically little virtual machines that run processes in sandbox mode. From the point of view of the process, it is the only process in the machine and can only access memory and disk allocated by Docker. To run this processes I first need an image. For this cheat sheet I will use TensorFlow:

docker pull tensorflow/tensorflow

Now that I have an image I can create a container what will run the process:

docker run -it tensorflow/tensorflow bash

The -it options open an interactive shell to interact with the new machine, so we can use the underlying Ubuntu image to execute commands. It gets kind of boring if we not have a script to run, but I can run scripts inside the container that already exists in my local machine. Imagine for a second that I have a script named to run some example of machine learning in my folder /usr/myuser/tensorflow or any other folder, then I can go to my file location and run:

docker run -it -v $(pwd):/root tensorflow/tensorflow bash

to open a shell in my container and my file will be immediately accesible in the shell. The -v option allows to mount a folder from the local machine to the container file system. The $(pwd) prints the current location of the shell to the argument, thus, assuming it is run in /usr/myuser/tensorflow, it is equivalent to:

docker run -it -v /usr/myuser/tensorflow:/root tensorflow/tensorflow bash

Now, inside the container I can run my python scripts with just python and any output will be printed in the shell. Now you have your own development environment in sandbox mode and it will not interfere, in theory, with any other environment.

To stop the container open a new shell and run docker ps to list all the running containers, locate the container to stop and then just run docker stop <container_name>.