diff --git a/.github/workflows/docker-push-image.yml b/.github/workflows/docker-push-image.yml index cf9e46f..37c7c97 100644 --- a/.github/workflows/docker-push-image.yml +++ b/.github/workflows/docker-push-image.yml @@ -28,11 +28,22 @@ jobs: username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push Docker image + - name: Build and push Debian image uses: docker/build-push-action@v7 with: context: ./docker/ + file: ./docker/Dockerfile push: true tags: | opensips/opensips-cli:latest ghcr.io/opensips/opensips-cli:latest + + - name: Build and push Alpine image + uses: docker/build-push-action@v7 + with: + context: ./docker/ + file: ./docker/Dockerfile.alpine + push: true + tags: | + opensips/opensips-cli:alpine + ghcr.io/opensips/opensips-cli:alpine diff --git a/docker/Dockerfile b/docker/Dockerfile index 60e2e26..561dcc6 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,27 +1,20 @@ -FROM python:slim-trixie -LABEL maintainer="Razvan Crainea " - -USER root +ARG BASE_IMAGE=python:slim-trixie -# Set Environment Variables -ENV DEBIAN_FRONTEND noninteractive +FROM ${BASE_IMAGE} AS builder -#install basic components RUN apt-get -y update -qq && \ - apt-get -y install git + apt-get -y --no-install-recommends install git && \ + git clone https://github.com/OpenSIPS/opensips-cli.git /usr/src/opensips-cli && \ + python3 -m pip install --no-cache-dir /usr/src/opensips-cli -#add keyserver, repository -RUN git clone https://github.com/OpenSIPS/opensips-cli.git /usr/src/opensips-cli && \ - cd /usr/src/opensips-cli && \ - python3 -m pip install . && \ - cd / && rm -rf /usr/src/opensips-cli +FROM ${BASE_IMAGE} +LABEL maintainer="Razvan Crainea " -RUN apt-get purge -y git && \ - apt-get autoremove -y && \ - apt-get clean +COPY --from=builder /usr/local/lib /usr/local/lib +COPY --from=builder /usr/local/bin/opensips-cli /usr/local/bin/opensips-cli -ADD "run.sh" "/run.sh" +RUN python3 -m compileall -q /usr/local/lib -ENV PYTHONPATH /usr/lib/python3/dist-packages +COPY run.sh /run.sh -ENTRYPOINT ["/run.sh", "-o", "communication_type=http"] +ENTRYPOINT [ "/run.sh", "-o", "communication_type=http" ] diff --git a/docker/Dockerfile.alpine b/docker/Dockerfile.alpine new file mode 100644 index 0000000..f03de0f --- /dev/null +++ b/docker/Dockerfile.alpine @@ -0,0 +1,19 @@ +ARG BASE_IMAGE=python:alpine + +FROM ${BASE_IMAGE} AS builder + +RUN apk add --no-cache git && \ + git clone https://github.com/OpenSIPS/opensips-cli.git /usr/src/opensips-cli && \ + python3 -m pip install --no-cache-dir /usr/src/opensips-cli + +FROM ${BASE_IMAGE} +LABEL maintainer="Razvan Crainea " + +COPY --from=builder /usr/local/lib /usr/local/lib +COPY --from=builder /usr/local/bin/opensips-cli /usr/local/bin/opensips-cli + +RUN python3 -m compileall -q /usr/local/lib + +COPY run.sh /run.sh + +ENTRYPOINT [ "/run.sh", "-o", "communication_type=http" ] diff --git a/docker/Makefile b/docker/Makefile index bb406f0..849fc3a 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -3,11 +3,18 @@ OPENSIPS_DOCKER_TAG ?= latest all: build start -.PHONY: build start -build: - docker build \ +.PHONY: build build-debian build-alpine start +build-debian: + docker build -f Dockerfile \ --tag="opensips/opensips-cli:$(OPENSIPS_DOCKER_TAG)" \ . +build: build-debian + +build-alpine: + docker build -f Dockerfile.alpine \ + --tag="opensips/opensips-cli:alpine-$(OPENSIPS_DOCKER_TAG)" \ + . + start: docker run -d --name $(NAME) opensips/opensips-cli:$(OPENSIPS_DOCKER_TAG) diff --git a/docker/docker.md b/docker/docker.md index f689daf..cf3750a 100644 --- a/docker/docker.md +++ b/docker/docker.md @@ -4,13 +4,19 @@ Docker recipe for running [OpenSIPS Command Line Interface](https://github.com/OpenSIPS/opensips-cli). ## Building the image -You can build the docker image by running: +You can build the Debian-based docker image by running: ``` -make build +make build-debian ``` -This command will build a docker image with OpenSIPS CLI master version taken from -the git repository +Or the Alpine-based image (smaller footprint) with: +``` +make build-alpine +``` + +`make build` is an alias for `make build-debian`. + +All variants install OpenSIPS CLI from the latest master branch on GitHub. ## Parameters @@ -25,13 +31,20 @@ Meaning of the parameters is as it follows: will end up in opensips-cli config file, in the `default` section, as `KEY: VALUE` lines * `CMD` - the command used to run; if the `CMD` ends with `.sh` extension, it -will be run as a bash script, if the `CMD` ends with `.py` extension, it is -run as a python script, otherwise it is run as a `opensips-cli` command +will be run as a POSIX shell (`sh`) script, if the `CMD` ends with `.py` +extension, it is run as a python script, otherwise it is run as a +`opensips-cli` command * `PARAMS` - optional additional parameters passed to `CMD` ## Run -To run a bash script, simply pass the connector followed by the bash script: +The entrypoint script (`run.sh`) is POSIX `sh`-compatible and requires no +additional shell. As a result, the Alpine image ships no extra packages beyond +the base Python Alpine image. Shell scripts passed as `CMD` must also be POSIX +`sh`-compatible, or include their own `#!/bin/bash` shebang with bash installed +separately. + +To run a shell script, simply pass the connector followed by the script: ``` docker run -d --name opensips-cli opensips/opensips-cli:latest \ -o url=http://8.8.8.8:8888/mi script.sh diff --git a/docker/run.sh b/docker/run.sh index 6d7e199..d19acb1 100755 --- a/docker/run.sh +++ b/docker/run.sh @@ -1,18 +1,17 @@ -#!/bin/bash +#!/bin/sh -OPTS= CMD= PARAMS= CFG=/etc/opensips-cli.cfg echo "[default]" > "$CFG" -while [[ $# -gt 0 ]]; do +while [ $# -gt 0 ]; do case "$1" in -o|--option) shift - P=$(cut -d'=' -f1 <<<"$1") - V=$(cut -d'=' -f2- <<<"$1") + P=$(echo "$1" | cut -d'=' -f1) + V=$(echo "$1" | cut -d'=' -f2-) echo "$P: $V" >> "$CFG" ;; *) @@ -26,12 +25,10 @@ while [[ $# -gt 0 ]]; do shift done -if [[ $CMD == *.py ]]; then - TOOL=python3 -elif [[ $CMD == *.sh ]]; then - TOOL=bash -else - TOOL=opensips-cli -fi +case "$CMD" in +*.py) TOOL=python3 ;; +*.sh) TOOL=sh ;; +*) TOOL=opensips-cli ;; +esac exec $TOOL $CMD $PARAMS