Quickstart

A rudimentary example of how the docker-dna library may be used. We will demonstrate the basic features of the app by creating a simple Flask app.

Important

Before continuing, make sure you’ve installed docker, nginx, and certbot for your distribution.

First, create a venv and activate it.

python3 -m venv env
source env/bin/activate

Install docker-dna and whatever other tools you need.

env/bin/pip install docker-dna flask gunicorn

Write a simple Flask app that accepts the URL to an image and deploys it.

app.py

from flask import Flask, jsonify
from dna import DNA

app = Flask(__name__)
dna = DNA("demo_dna")

@app.route("/deploy/<image>/<name>")
def deploy(image, name):
    """Pulls the ``image`` and deploys it to a service called ``name``.
    Sets up a webserver configuration to forward ``name``.example.com to
    the deployed app. Assumes the front-end runs on port 80.

    :param image: the name/url of the image to pull and deploy
    :type image: str
    :param name: the name to give this service
    :type name: str
    """
    
    dna.pull_image(image, name)
    dna.run_deploy(name, image, port="80")
    dna.add_domain(name, f"{name}.example.com")

    return jsonify({
        "success": True,
        "url": f"{name}.example.com",
    })

if __name__ == "__main__":
    app.run(host="0.0.0.0")

Run your app. Note that DNA requires elevated privileges as of now to interface with docker, nginx, and certbot, as all of these are privileged applications.

sudo env/bin/python app.py

Deploy an empty nginx container.

curl http://example.com:5000/nginx/dna-nginx

Visit https://dna-nginx.example.com/ to see your shiny new nginx service!

For more complete sample implementations, see Sample Usages in the docs.