If you’re planning to use Iron.io with Python, their new docker workflow is pretty cool. In this blog post, I shall walk you through the setup.
Setup Docker
The first step would be to install Docker. Here’s a pretty nice installation guide from Digital Ocean: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-getting-started 🙂
If you’re not using Ubuntu (eg. Windows or OS X), you might consider using Ubuntu in a virtualbox. I usually use Vagrant to do that.
If you’re using another Linux distro, please go through the official docker docs, I’m sure it’s quite easy 🙂
Once the setup is complete, run the following command to confirm that Docker has successfully installed and currently running:
You would see something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
root@vagrant-ubuntu-trusty-64:/home/ubuntu/share# docker info Containers: 0 Images: 111 Storage Driver: aufs Root Dir: /var/lib/docker/aufs Backing Filesystem: extfs Dirs: 111 Dirperm1 Supported: false Execution Driver: native-0.2 Logging Driver: json-file Kernel Version: 3.13.0-36-generic Operating System: Ubuntu 14.04.1 LTS CPUs: 1 Total Memory: 490 MiB Name: vagrant-ubuntu-trusty-64 ID: 3UAF:4GIJ:CDSD:4ICM:KNZA:OSN4:QPQE:Y3JA:5BQY:UFO3:4EVI:DTC4 WARNING: No swap limit support |
Installing PIP Dependencies
Now it’s the time to install the dependencies for our projects. Change directory to our project root and use the requirements.txt file to install the packages. Please note we shall install them into a separate directory than the usual system path.
|
docker run --rm -v "$PWD":/worker -w /worker iron/images:python-2.7 pip install -t packages -r requirements.txt |
Here we have asked docker to run a new container based on the “iron/images:python-2.7” image. Inside the container we run the pip command. The “-t” flag would install packages in a separate target directory.
Since this is the first time we’re using this image, docker will need to pull it first. Then it will continue to install the dependencies. Once it’s done, feel free to examine the packages directory.
Locally Running Workers
The command is similar, except we have the packages installed in “packages” directory. So we need to set the PYTHONPATH env variable.
|
docker run --rm -v "$PWD":/worker -w /worker -e PYTHONPATH="packages" iron/images:python-2.7 python main.py welcome |
That would work 🙂
Bundling workers
To bundle the workers, we need to first zip everything:
|
zip -r helloworld.zip main.py app framework packages |
You must remember to zip everything in, including the packages directory.
Now, we can start using the iron cli. In case you don’t have it installed, it’s easy:
|
curl -sSL http://get.iron.io/cli | sh |
Now let’s upload the zip file:
|
iron --env testing worker upload --zip helloworld.zip --name helloworker iron/images:python-2.7 PYTHONPATH="packages" python main.py helloworld |
I think the command is pretty self explanatory. However, for the env argument, you need a file named “iron.json” which would contain the project id and tokens for your different environments. Here’s a sample:
|
{ "testing": { "token": "<secret>", "project_id": "<project id>" } } |
Also note, we’re using PYTHONPATH=”packages” before invoking Python.
If everything goes right, your worker will be packaged and uploaded. Try running it to see how it goes.