Docker is an open platform for distributed application for developer and sysadmin. It helps to help seamless cycle of build, ship and run. In this post context I would like to share how docker helped me in prototyping or learning using particular stacks without complication of OS dependency and polluting my host environment by installing many softwares in the host system.
In this example, I would like to share how we can run a python server. In following this tutorial, I am assuming docker is already installed in your system. I will not guiding the installation process and explaining too detail for every commands because it is easily accessible in docker documentation itself. I am running this tutorial using Mac OSX host.
To avoid confusion, all command started with
$ means the command that is run on docker host while prefix
$$ means command that is run in docker container. Let's get our hand dirty.
$ docker run -it -p 3000:3000 ubuntu bash
The command will run a docker container using ubuntu image, after the container started, execute bash and attach and interact to it. The
-p argument will expose port 3000 in docker container to port 3000 in host OS (or virtual machine port if you are running non linux host). If you got an error such as
Cannot connect to Docker daemon, it is probably because docker service has not been started. In Ubuntu host you can run the service by executing
$ sudo service docker start
Another possibilities is the docker-machine is not started if you are using non linux host. For non linux host, docker will need linux virtual machine to run docker.
$ docker-machine start
After started, we need to run this command, to make sure docker command can connect to the daemon.
$ eval $(docker-machine env)
You can add the previous command to .bash_profile, so that it will be executed automatically every time we open new terminal
$ echo "eval \$docker-machine env)" > ~/.bash_profile
Going back to the ubuntu bash prompt, we can run any command as if it is a real ubuntu host
$$ apt-get update && apt-get install python -y
This will install python on top our ubuntu container
$$ cd && echo "Hello world" > hello.txt && python -m SimpleHTTPServer 3000
Let us create a text file contains
Hello world message in current docker user home directory. After that run python server on port 3000 to serve the current directory. If you remember, earlier when running this docker container we are exposing port 3000 to port 3000 of docker host, which means, request to port 3000 of docker host will be forwarded to this container.
Let us open browser and go to http://localhost:3000/hello.txt if you are on linux host. Otherwise run this command to get your docker host IP address:
$ docker-machine ip
Open the browser using the given ip address. For my case it is http://192.168.99.100:3000/hello.txt. It might be different in your setup.
For either linux and non linux host, you will get
Hello world message in your browser.
Congratulation for running your first container successfully.
However, we ran and set up the container manually. Could there be a better way to do? Yeah it is.
Previously we built the container from bare ubuntu image and install python manually. Docker host a registry contains a lot of docker images that is ready to use. Instead of setting up a container from scratch we can go through shortcut by using image that will suit our need.
Here we will run our python server again, but without the need to do everything from scratch
$ echo “Hi there” >> hi.txt
$ docker run -it --rm --name my-python-server -v $(pwd):/root -p 3000:3000 -w /root python:2.7 python -m SimpleHTTPServer 3000
The command will run docker container using python version 2.7 image and named it
my-python-server. The container will create a volume that link current directory (pwd) to /root folder in container. As before port 3000 of the container will be exposed to docker host port.
-w argument will set the docker container working directory to /root folder. After the container run, we ask to execute script
python -m SimpleHTTPserver 3000 which will run python http server serving current directory in port 3000. When we exit from the container, it will be autoremoved because we gave
That's all for now. I will spare some more sharing on another post.