Skip to content

Containerize CLI app #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git
docs
tests
build
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: CI

on:
push:
branches: [main]
tags: [v*]
pull_request:
branches: [main]
release:
types: [published]

env:
REGISTRY: ghcr.io
REPOSITORY: ${{ github.repository }}

jobs:
docker-image:
name: Build and push Docker image
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}

- name: Log in to the Container registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push ${{ env.REGISTRY }}/${{ env.REPOSITORY }} image
uses: docker/build-push-action@v3
with:
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ github.event_name != 'pull_request' }}
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# ------------------------------------
# Nuild stage
# ------------------------------------
FROM composer/composer:2 as build
WORKDIR /app

# Install dependencies
COPY composer.json ./
RUN composer install --no-dev --no-autoloader --no-scripts --ignore-platform-reqs
COPY . .
RUN composer install --no-dev --optimize-autoloader --ignore-platform-reqs


# ------------------------------------
# Runtime stage
# ------------------------------------
FROM php:7-fpm-alpine as runtime

# System dependencies
RUN apk add --update libxslt-dev
RUN docker-php-ext-install xsl soap

# Copy source code
WORKDIR /app
COPY --from=build /app .

ENTRYPOINT ["bin/cfditopdf"]
CMD ["--help"]
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Container image
IMAGE_REPO ?= ghcr.io/phpcfdi/cfditopdf
IMAGE_TAG ?= latest

.DEFAULT_GOAL := help
.PHONY: build run help

build: ## Build the container image
docker build -t $(IMAGE_REPO):$(IMAGE_TAG) .

run: ## Run container passing `cmd` make argument as the containr command
@docker run --rm \
--volume $(shell pwd)/data:/data \
--user $(shell id -u ${USER}):$(shell id -g ${USER}) \
$(IMAGE_REPO):$(IMAGE_TAG) \
$(cmd)

help: ## Print this help.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ Use [composer](https://getcomposer.org/), so please run
composer require phpcfdi/cfditopdf
```

## Run with Docker

Running as a Docker container lets you use this CLI anywhere where Docker is installed without worrying about about any dependencies, not even PHP itself.

```shell
# This will mount the current working directory
# into a the /data directory inside the container
docker run --rm \
--volume $PWD:/data \
--user $(id -u):$(id -g) \
ghcr.io/phpcfdi/cfditopdf:latest \
/data/my-cfdi.xml \
/data/my-cfdi-output.pdf
```

You can optionally set an alias in your shell to simplify running the container (to make this alias permanent add the alias to your .bashrc fille).

```shell
alias cfditopdf='docker run --rm --volume $PWD:/data --user $(id -u):$(id -g) ghcr.io/phpcfdi/cfditopdf:latest'

# Then, execute just as `cfditopdf` (see usage in the next section)

cfditopdf --help
cfditopdf /data/my-cfdi.xml /data/my-cfdi-output.pdf
```

## Basic usage from CLI

```text
Expand Down