Skip to main content

Collection of utilities that simplify the process of deploying webapps inside containers.

Project description

docker-dna

PyPI Badge License Badge Checks Badge

See this project on GitHub. Also available via PyPI.

Motivation

The docker-dna library is a collection of utilities that together simplify the process of deploying webapps inside containers, all the while making sure the process is efficient, customizable, and extensible.

Without docker-dna

The standard procedure for accomplishing the task of deploying webapps inside containers may look something like this:

  1. Copy the relevant files to your webserver
  2. Build and tag the container image
  3. Find an open port on your machine to use in the next step
  4. Run the container image you just built, exposing whatever port the front-end runs on
  5. Write the webserver configuration to proxy your domain endpoint to the port you just exposed
  6. Reload the webserver to host your front-end
  7. Run certbot to get an SSL certificate for your domain endpoint

This sounds tedious, and even if you were to script it, it'd be a lot of scripting to do and chances are it'd be hard to maintain if something goes wrong in the future (which, you know it will in this field of work).

With docker-dna

The process boils down to the following:

  1. Receive a git webhook, and checkout the referenced commit
  2. Call dna.build_image
  3. Figure out what domain(s) you want to deploy this app to
  4. Call dna.run_deploy

That's it! That handles your first deploy, second deploy, one hundredth deploy... you get the idea.

What you need

To use this library, you need to install docker, nginx, and certbot on your machine. Then, read the quickstart to see an example of how to get up and running with the docker-dna library!

You may also want to check out the dna.utils.Logger class for some logging utilities.

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.

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.

Contributing

Guide coming soon, for now just use common sense! And the GitHub Flow.

License

This project is licensed under the Apache License 2.0. See LICENSE for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for docker-dna, version 0.6.5
Filename, size File type Python version Upload date Hashes
Filename, size docker_dna-0.6.5-py3-none-any.whl (25.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size docker-dna-0.6.5.tar.gz (20.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page