A template geared towards bundling applications with Docker, and deploying them to a server to be run by systemd via Docker Compose.
Please allow me to start by saying, if you haven't read How We Deploy Python Code on the Nylas blog, please take the time to do so, it's an excellent read that covers a common problem.
When deploying (usually, Python-based) programs, teammates and I have 1 of 3 choices, which vary based upon needs:
- Docker Compose with systemd - Daemons with less direct interfacing to a server.
- dh-virtualenv - Daemons with more direct interfacing to a server (e.g. Network routes.)
- Snappy - Applications with more direct interfacing to a client (e.g. X11.)
For the purpose of this repository, the goal is #1: the higher-level needs may change, but the lower-level needs do not. This is because I tend to use systemd and Docker Compose for running 1-n Docker containers, together as a multi-container application in a single file (e.g. Tying MQTT, WebSockets, and Nginx together with Docker), or multiple Docker Compose-based applications independently, or together (e.g. running a Django application in a separate compose definition file that ties back to Nginx.)
In most cases, the only files that you'll need to edit are inside the app/
directory:
launch.bash
- Used for starting your program. You'll notice that the initial point in this file starts withapp/main.py
main.py
- Initial program included to demonstrate usage. You can delete this program, and modifylaunch.bash
to suit your needs..dbt/
- Configuration for this repository,Docker bundling template
config.yaml
- Run and synchronization parameters for scripts to run. Parameters are explained in the next section.docker
- Used for modifying your run-time environment.docker-compose-develop.yaml
- For developing.docker-compose.yaml
- For deploying.
setup
- This entire directory relates to the setup and installation process required for your application.main.bash
(optional) - Used for extending the installation process, and not a necessary edit.modules.txt
- Python modules (1 per line.)packages.txt
- System packages (1 per line.)
app
- Application settings used by various scripts.container_name
- Name of your container (i.e. for direct calls viadocker
C.L.I. and what appears when you rundocker ps
)image_name
- The name of your Docker image.image_version
- The version of your Docker image.run_service
- Which service indocker-compose.yaml
anddocker-compose-develop.yaml
should be run.
build
- used bybin/build.bash
clean
- attempt to remove dangling Docker images and exited containers after each build.base
- Base of your Docker image.image
- Which image to base off of (e.g. Ubuntu or CentOS.)version
- For example, if you were using Ubuntu as your base image and wanted one of the recent Long Term Support versions, valid choices would be16.04
or18.04
.
sync
(optional) - used bybin/sync.bash
for live synchronization via SSH.user
- remote username.host
- remote host.path
- remote path to synchronize to.
Used to build your Docker image, and has 2 modes to choose from, via argument passed at command line:
bin/build.bash develop
(default) - Assembles prerequisites, but will not bundle your application.bin/build.bash deploy
- Bundles everything together.
Mostly used for developing, but can also be used for testing a production run before passing it over to systemd
. Like aforementioned build.bash
(which is automatically called by run.bash
), there are 2 modes to choose from, via argument passed at command line:
bin/run.bash develop
(default) - Connects host volume to container, and drops you into a Bash shell.bin/run.bash deploy
- Run identically tosystemd
configuration, where your application is built into the Docker image.
Builds image for deploy mode, then saves this image as app.tar
next to app/
. You can then upload app.tar
and install it manually with docker load --input app.tar
, or automate uploading and installation across a cluster of servers using a powerful tool like Ansible.
Useful for copying edits in real-time to a remote server, and developing (or deploying.)
- Louis T. Getterman IV
- Have an improvement? Your name goes here!
Written with StackEdit.