Skip to content

Commit

Permalink
Add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
scnerd committed Sep 12, 2022
1 parent e950657 commit b7b834d
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Documentation Status](https://readthedocs.org/projects/docker-printer/badge/?version=latest)](https://docker-printer.readthedocs.io/en/latest/?badge=latest)
[![Documentation Status](https://readthedocs.org/projects/docker-printer/badge/?version=latest)](https://docker-printer.readthedocs.io/en/latest/?badge=latest) ![PyPI](https://img.shields.io/pypi/v/docker-printer)

# Docker-Printer

Expand All @@ -12,4 +12,11 @@ Regular multi-stage dockerfiles and `docker build` commands are incredibly power

# Getting Started and Documentation

```
pip install docker-printer
docker-printer init
```

See the [documentation](https://docker-printer.readthedocs.io/en/latest/#) for how to get started.

There are also example docker constructs provided in the `/examples` folder.
14 changes: 12 additions & 2 deletions docker_printer/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import subprocess
import textwrap
from pathlib import Path

import click

Expand Down Expand Up @@ -61,8 +63,9 @@ def build(name):


@cli.command()
def init():
base_dir = config_dir(default_to_local=True)
@click.argument("path", default=Path(), type=click.Path(exists=True, dir_okay=True))
def init(path):
base_dir = path / "docker-printer"
if base_dir.exists():
click.echo(f"{base_dir} already exists, cannot initialize new project")
raise click.Abort()
Expand All @@ -72,3 +75,10 @@ def init():
(base_dir / "templates").mkdir(exist_ok=False, parents=False)
(base_dir / "targets.yml.jinja2").touch(exist_ok=False)
(base_dir / "builds.yml.jinja2").touch(exist_ok=False)
(base_dir / ".gitignore").write_text(
textwrap.dedent(
"""
*.rendered.yml
""".lstrip()
)
)
29 changes: 29 additions & 0 deletions examples/fastapi_app/Dockerfile.synth
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# syntax = docker/dockerfile:1.2

ARG PYTHON_VERSION=3.9
ARG BASE_OS=slim

FROM tiangolo/uvicorn-gunicorn-fastapi:python${PYTHON_VERSION}-${BASE_OS} AS base

ENV APP_DIR="/app"

RUN mkdir -p ${APP_DIR}
WORKDIR ${APP_DIR}

FROM base AS dev

# Install Python dependencies
# Requirement file: ./requirements.txt

COPY ./requirements.txt ./requirements.txt

RUN \
pip install --no-cache-dir -r ./requirements.txt && \
rm ./requirements.txt

FROM dev AS prod

# Add necessary source code into the image for production execution
COPY ./app ${APP_DIR}

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
Empty file.
38 changes: 38 additions & 0 deletions examples/fastapi_app/docker-bake.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"group": {
"default": {
"targets": [
"dev",
"prod"
]
}
},
"target": {
"dev": {
"dockerfile": "Dockerfile.synth",
"tags": [
"my_app:dev"
],
"target": "dev",
"cache-from": [
"type=docker"
],
"output": [
"type=docker"
]
},
"prod": {
"dockerfile": "Dockerfile.synth",
"tags": [
"my_app:prod"
],
"target": "prod",
"cache-from": [
"type=docker"
],
"output": [
"type=docker"
]
}
}
}
2 changes: 2 additions & 0 deletions examples/fastapi_app/docker-printer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.rendered.yml
5 changes: 5 additions & 0 deletions examples/fastapi_app/docker-printer/builds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: default
image: my_app
build_args:
cache-from: [type=docker]
output: [type=docker]
11 changes: 11 additions & 0 deletions examples/fastapi_app/docker-printer/modules/app-code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: app-code
priority: -100
depends_on:
- base
template:
file: stage.Dockerfile.jinja2
variables:
instructions:
- "# Add necessary source code into the image for production execution"
- COPY ./app ${APP_DIR}
command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
10 changes: 10 additions & 0 deletions examples/fastapi_app/docker-printer/modules/app-deps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: app-deps
priority: 70
depends_on:
- base
template:
file: pip-install.Dockerfile.jinja2
variables:
requirement_files:
- source: ./requirements.txt
destination: ./requirements.txt
13 changes: 13 additions & 0 deletions examples/fastapi_app/docker-printer/modules/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: base
priority: 100
template:
variables:
base: tiangolo/uvicorn-gunicorn-fastapi:python${PYTHON_VERSION}-${BASE_OS}
env:
APP_DIR: /app
instructions:
- RUN mkdir -p ${APP_DIR}
- WORKDIR ${APP_DIR}
image_args:
PYTHON_VERSION: 3.9
BASE_OS: slim
10 changes: 10 additions & 0 deletions examples/fastapi_app/docker-printer/targets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: dev
modules:
- base
- app-deps

- name: prod
extends:
- dev
modules:
- app-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "stage.Dockerfile.jinja2" %}

{% block instructions -%}
# Install Python dependencies
{% for req in requirement_files -%}
# Requirement file: {{ req['source'] }}
{%- endfor %}
{% if requirements -%}
# Other requirements: {% for req in requirements -%}{{ req }}{{ ", " if not loop.last else "" }}{% endfor %}
{% endif %}

{% for req in requirement_files -%}
COPY {{ req['source'] }} {{ req['destination'] }}
{% endfor %}

RUN {% if cache %}--mount=type=cache,target=/var/cache/pip{% endif %} \
pip install {% if cache %}--cache-dir /var/cache/pip{% else %}--no-cache-dir{% endif %} {% for req in requirement_files %}-r {{ req['destination'] }}{% endfor %} {% for req in requirements %}{{ req }}{{ " " if not loop.last else "" }}{% endfor %}{% for req in requirement_files %} && \
rm {{ req['destination'] }}{% endfor %}
{% endblock %}
1 change: 1 addition & 0 deletions examples/fastapi_app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is where you would put any additional requirements beyond what's installed in the base docker image

0 comments on commit b7b834d

Please sign in to comment.