Tag Archive for 1.2.0

Docker Part 3 – HOWTO Create a Simple Python Web App in Docker

Docker Logo

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…

Docker Part 2 – HOWTO Remove / Delete Docker Containers

Docker Logo

So you have been messing with docker for a few minutes or hours, and now you have a bunch of either running or stopped containers you no longer need. How do you get rid of them?

Removing Single Containers

To remove a single docker container, you simply start by listing all of the docker containers (started or stopped) to ensure you know which one to delete:

$ sudo docker ps –a


Then remove the chosen container:

$ sudo docker rm <container name>


If the container is currently running you can simply add –f to stop and remove the container in a single command:

$ docker rm -f <container name>


Unless it’s paused, then you will get an error something like the following:

Error response from daemon: Could not kill running container, cannot remove - Container e4f28eccb0cbcfbf4d78104bfe3e84039f62c5073f7301f8a39bb77a9598ae72 is paused. Unpause the container before stopping


This is easy to resolve. The “docker pause” command was added as of Docker 1.0, allowing for better resource utilisation if you have containers you don’t currently need to be wasting CPU cycles. As of Docker 1.1, running containers are also paused during commit activities, to ensure a consistent file system. Simply check the ID of the VM (with a ps command), unpause it, then remove:

sudo docker ps
sudo docker unpause <container id>
sudo docker rm -f <container id>

 

Removing Multiple Containers

Sometimes we have built up a number of containers and we just want to scrub the lot in one go. If you want to remove all containers (running or not), first you need to generate a list of all of the container IDs, then you pass that list to the docker rm command as follows:

sudo docker rm -f $(sudo docker ps -aq)


Alternatively if you wish to remove only the non-running containers:

sudo docker rm $(sudo docker ps -q)

 

That’ll do for now, but in the next post I will go into how to install your first app…

Docker Part 3 – HOWTO Create a Simple Python Web App in Docker

Docker Part 1 – Introduction and HOWTO Install Docker on Ubuntu 14.04 LTS

Docker Logo

So my background is in VMware virtualisation, but I hear the buzz these days is that I may as well forget everything I know, and that it’s all about containers! 🙂

Joking aside, containers definitely compliment virtualisation, giving you more portability, granular control, and the flexibility to spin up new application instances in milliseconds. For an excellent introduction to docker and containers I recommend you check out Greg Ferro’s post here.  I decided it was about time I had a bit of a play with Docker, and at the same time I thought I may as well document my process and some of the basics for any others in the same position. I will try to keep each post as short as possible, breaking things down into manageable chunks (whilst avoiding the dreaded TLDR!).

Most of my recent Linux experience is with Ubuntu, which works out quite well as apparently this is the preferred distro for a very large proportion of Docker users and images. CoreOS is a very interesting proposition too, but one step at a time…

All of the examples in this post and series are based on:

  • Ubuntu 14.04.1 LTS
  • Docker 1.2.0

I find it very interesting to note the rate at which the docker development team are providing new releases.

  • Docker 1.2.0     22 Aug 2014
  • Docker 1.1.0     03 Jul 2014
  • Docker 1.0       09 Jun 2014
  • Docker 0.9       10 Mar 2014
  • Docker 0.8       04 Feb 2014
  • Docker 0.7       26 Nov 2013
  • Docker 0.6       23 Aug 2013

Not only are they very often, but that appear to be accelerating, with a new point release every month for the last three! This is great as it means more features, bug fixes etc, but it also means that anything you develop and test today may already be out of date by the time you come to deploy it to production next month! Docker’s official line on long term support can be found here, but from the look of it you will never be allowed to fall more than 12 months behind. This suggests that the aggressive release schedule to date is likely a pattern that will continue for the foreseeable:
https://www.docker.com/enterprise/support/

Before you follow any of the steps below, I also highly recommend you check out the link below for your first taste of using the Docker CLI:
https://www.docker.com/tryit

If like me, you like to look up a decent book on a subject to help you get your head around it, I also suggest checking out one of the first publications on the subject (I’m reading it at the moment and will try to get a review done once finished):
The Docker Book: Containerization is the new virtualization

So as for the install, it really is incredibly simple. Personally I like to always create my Ubuntu Server VMs as “Minimal” installs. This is simply done by hitting F4 during base OS install. I’ll assume you can do the basic config of your Ubuntu VM by configuring a static IP address, hostname, DNS, installing VMware tools, completing the latest system updates, etc.

The install process is also detailed here:
https://docs.docker.com/installation/ubuntulinux/

To ensure you have your repositories fully up to date:

sudo apt-get update


One you have docker installed log into the console and run the following to install docker and its dependencies:

sudo apt-get install docker.io
sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io


I would prefer using the latest version of docker, which means some extra commands:

sudo apt-get install apt-transport-https
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
sudo sh -c "echo deb https://get.docker.io/ubuntu docker main\ > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install lxc-docker


As per my previous post you also need to do:

sudo apt-get install apparmor


Check your upgrade has worked by confirming the current version:

sudo docker version


Then you can test the install by creating your first container:

sudo docker run -i -t ubuntu /bin/bash


That’s it!

In the next post I will describe the ways to remove all of the containers you will inevitably create now that you can see the awesome power of Docker! 🙂

Docker Part 2 – HOWTO Remove / Delete Docker Containers

%d bloggers like this: