If you’ve been following this series (last part here), we now have docker installed, but what do we do next? Create our first containers of course!
I think we need to make it a bit more interesting though as just creating containers is a bit meaningless, in real life we’re actually going to do something with them. The scenario is that we want a few copies of our simple python web application. To achieve this we need to use a few simple docker commands:
- Create a new container
- Install an application inside of it
- Store it as an image
- Duplicate it more than once and make these available to other clients
- Test each instance to ensure they are unique and accessible
The good thing here is that all of the above steps are repeatable with whatever application you wish to install inside your containers. This is just a simple way to help get your head around the concepts and commands.
We start by creating our first empty Ubuntu container. The –i connects us to the shell of the container (interactive).
$ sudo docker run -i -t --name="firstcontainer" ubuntu:14.04 /bin/bash
Then in this case we need to install the python and web.py dependencies INSIDE of the container. This could be modified for any required dependencies or apps.
$ apt-get update
$ apt-get install -y python python-webpy
Within the container, create a new python script:
$ sudo mkdir /home/test1 $ sudo vi /home/test1/app.py
The contents of the script are:
#!/usr/bin/python
import web,sys
urls = ( '/', 'index' )
app = web.application(urls, globals())
class index: def GET(self): argumentone = sys.argv[2] greeting = "Hello World, the test message is " + argumentone return greeting
if __name__ == '__main__' : app = web.application(urls, globals()) app.run()
Exit the container, back to the Native OS:
$ exit
Confirm the name of your container (the last container run):
$ sudo docker ps –l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f711ff0fd695 ubuntu:14.04 /bin/bash 32 minutes ago Exit 0 firstcontainer
Create a new image from your docker called testpython1
$ sudo docker commit firstcontainer testpython:0.1
Confirm you can see the image and get the image ID:
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
testpython 0.1 fcb365f7591b 2 minutes ago 247.8 MB
Finally, start up 3 instances of your web application:
$ sudo docker run -d -p 8081:8081 fcb365f7591b python /home/test1/app.py 8081 "instance1" $ sudo docker run -d -p 8082:8082 fcb365f7591b python /home/test1/app.py 8082 "instance2" $ sudo docker run -d -p 8083:8083 fcb365f7591b python /home/test1/app.py 8083 "instance3"
Open a browser on your network and connect to http://dockerserverip:8081
Try the same for the other two port numbers. Note we now have a system running 3 separate containers which could then be load balanced using a third party tool, or even run completely different content. Cool huh?
Next, how to mount a drive into your container…