From 386d56a20e2ba1f98bde896897cc4e96b6dd75dc Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sat, 13 Apr 2019 23:49:49 +0200 Subject: Add a basic docker file --- .dockerignore | 6 ++++++ .gitignore | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 30 +++++++++++++++++++++++++++++ docker-run.sh | 12 ++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 docker-run.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..939fd11 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +Dockerfile +.dockerignore +examples/ +.gitignore +README.md +travelynx.conf diff --git a/.gitignore b/.gitignore index f93666b..29532a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,62 @@ /travelynx.conf + +# Created by https://www.gitignore.io/api/vim,perl +# Edit at https://www.gitignore.io/?templates=vim,perl + +### Perl ### +!Build/ +.last_cover_stats +/META.yml +/META.json +/MYMETA.* +*.o +*.pm.tdy +*.bs + +# Devel::Cover +cover_db/ + +# Devel::NYTProf +nytprof.out + +# Dizt::Zilla +/.build/ + +# Module::Build +_build/ +Build +Build.bat + +# Module::Install +inc/ + +# ExtUtils::MakeMaker +/blib/ +/_eumm/ +/*.gz +/Makefile +/Makefile.old +/MANIFEST.bak +/pm_to_blib +/*.zip + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# End of https://www.gitignore.io/api/vim,perl diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b8dd2d7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM debian:stretch + +RUN apt-get update && apt-get install -y \ + cpanminus \ + build-essential \ + libpq-dev \ + git \ + ssmtp + +RUN cpanm -in \ + Cache::File \ + Crypt::Eksblowfish \ + DateTime \ + DateTime::Format::Strptime \ + DBI \ + DBD::Pg \ + Email::Sender \ + Geo::Distance \ + Geo::Distance::XS \ + Mojolicious \ + Mojolicious::Plugin::Authentication \ + Travel::Status::DE::IRIS \ + UUID::Tiny \ + JSON + +COPY . /app +WORKDIR /app + + +CMD /app/docker-run.sh \ No newline at end of file diff --git a/docker-run.sh b/docker-run.sh new file mode 100755 index 0000000..b438a19 --- /dev/null +++ b/docker-run.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -eu + +if [ ! -f travelynx.conf ] +then + echo "The configuration file is missing" + exit 1 +fi + +perl index.pl database migrate + +exec /usr/local/bin/hypnotoad -f index.pl \ No newline at end of file -- cgit v1.2.3 From 45410607d3c1142ade18f884f3c2a4a2a18a1b73 Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sun, 14 Apr 2019 01:00:40 +0200 Subject: dockerfile: remove cpanm (+workdir) & build-essentials to save space --- Dockerfile | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index b8dd2d7..8d50a06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,14 @@ FROM debian:stretch -RUN apt-get update && apt-get install -y \ +ARG DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install --no-install-recommends -y \ cpanminus \ build-essential \ libpq-dev \ git \ - ssmtp - -RUN cpanm -in \ + ssmtp \ + && cpanm -in \ Cache::File \ Crypt::Eksblowfish \ DateTime \ @@ -21,10 +22,14 @@ RUN cpanm -in \ Mojolicious::Plugin::Authentication \ Travel::Status::DE::IRIS \ UUID::Tiny \ - JSON + JSON \ + && rm -rf ~/.cpanm \ + && apt-get purge -y \ + build-essential \ + cpanminus \ + && apt-get autoremove -y COPY . /app WORKDIR /app - CMD /app/docker-run.sh \ No newline at end of file -- cgit v1.2.3 From f17c76a4fb780fc6d00cb040f274a93de3087a6c Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sun, 14 Apr 2019 01:18:11 +0200 Subject: dockerfile: use debian:stretch-slim and cpan --no-man-pages --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8d50a06..167294b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM debian:stretch +FROM debian:stretch-slim ARG DEBIAN_FRONTEND=noninteractive @@ -8,7 +8,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ libpq-dev \ git \ ssmtp \ - && cpanm -in \ + && cpanm -in --no-man-pages \ Cache::File \ Crypt::Eksblowfish \ DateTime \ -- cgit v1.2.3 From d2ff8ddddfd0e0dc0a2c81e4ddeda1c3890d5a4e Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sun, 14 Apr 2019 02:00:05 +0200 Subject: dockerfile: use exec form to support ^C --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 167294b..dedd46c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,4 +32,4 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ COPY . /app WORKDIR /app -CMD /app/docker-run.sh \ No newline at end of file +CMD ["/app/docker-run.sh"] \ No newline at end of file -- cgit v1.2.3 From b64597e908edec03e622cf8bb7eaed5aacf177a5 Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Tue, 16 Apr 2019 18:48:32 +0200 Subject: config: mail server setup --- docker-run.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docker-run.sh b/docker-run.sh index b438a19..f147b77 100755 --- a/docker-run.sh +++ b/docker-run.sh @@ -7,6 +7,16 @@ then exit 1 fi +if [ \ + "${TRAVELYNX_MAIL_DISABLE:-0}" -eq 0 \ + -a "${TRAVELYNX_MAIL_HOST:-unset}" != "unset" \ +] +then + export EMAIL_SENDER_TRANSPORT=SMTP + export EMAIL_SENDER_TRANSPORT_HOST=${TRAVELYNX_MAIL_HOST} + export EMAIL_SENDER_TRANSPORT_PORT=${TRAVELYNX_MAIL_PORT:-25} +fi + perl index.pl database migrate -exec /usr/local/bin/hypnotoad -f index.pl \ No newline at end of file +exec /usr/local/bin/hypnotoad -f index.pl -- cgit v1.2.3 From 7df47bdc4e7d7a5f9a14a95069da454f6b890e77 Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sun, 21 Apr 2019 17:50:36 +0200 Subject: Install Mojo::Pg --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index dedd46c..b7067b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ Travel::Status::DE::IRIS \ UUID::Tiny \ JSON \ + Mojo::Pg \ && rm -rf ~/.cpanm \ && apt-get purge -y \ build-essential \ @@ -32,4 +33,4 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ COPY . /app WORKDIR /app -CMD ["/app/docker-run.sh"] \ No newline at end of file +CMD ["/app/docker-run.sh"] -- cgit v1.2.3 From 46e85324df762672718c0a6a5ca60fb247e89e5d Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Sun, 21 Apr 2019 23:13:03 +0200 Subject: docker: add docker-compose configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔥docker🔥compose🔥 --- docker-compose.yml | 29 +++++++++++++++++++++++++++++ docker-run.sh | 20 +++++++++++++++++++- examples/docker/postgres-init.sh | 8 ++++++++ examples/docker/travelynx.conf | 26 ++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 docker-compose.yml create mode 100644 examples/docker/postgres-init.sh create mode 100644 examples/docker/travelynx.conf diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ac482e0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: "3.6" +x-common-env: &common-env + TRAVELYNX_DB_HOST: database + TRAVELYNX_DB_NAME: travelynx + TRAVELYNX_DB_USERNAME: travelynx + TRAVELYNX_DB_PASSWORD: travelynx +services: + database: + image: postgres:11 + networks: + - backend + environment: + <<: *common-env + volumes: + - ./examples/docker/postgres-init.sh:/docker-entrypoint-initdb.d/init.sh + travelynx: + build: . + ports: + - "8000:8093" + networks: + - backend + volumes: + - ./examples/docker/travelynx.conf:/app/travelynx.conf + environment: + <<: *common-env + TRAVELYNX_MAIL_DISABLE: 1 + TRAVELYNX_SECRET: 12345678 +networks: + backend: diff --git a/docker-run.sh b/docker-run.sh index f147b77..29ffa7b 100755 --- a/docker-run.sh +++ b/docker-run.sh @@ -1,6 +1,22 @@ -#!/bin/sh +#!/bin/bash set -eu +WAIT_DB_HOST=${TRAVELYNX_DB_HOST} +WAIT_DB_PORT=5432 + +wait_for_db() { + set +e + for i in $(seq 1 ${WAIT_DB_TIMEOUT:-5}) + do + (echo >/dev/tcp/${WAIT_DB_HOST}/${WAIT_DB_PORT}) &>/dev/null + if [ $? -eq 0 ]; then + break + fi + sleep 1 + done + set -e +} + if [ ! -f travelynx.conf ] then echo "The configuration file is missing" @@ -17,6 +33,8 @@ then export EMAIL_SENDER_TRANSPORT_PORT=${TRAVELYNX_MAIL_PORT:-25} fi +wait_for_db + perl index.pl database migrate exec /usr/local/bin/hypnotoad -f index.pl diff --git a/examples/docker/postgres-init.sh b/examples/docker/postgres-init.sh new file mode 100644 index 0000000..a8c59d1 --- /dev/null +++ b/examples/docker/postgres-init.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE USER ${TRAVELYNX_DB_USERNAME} PASSWORD '${TRAVELYNX_DB_PASSWORD}'; + CREATE DATABASE ${TRAVELYNX_DB_NAME}; + GRANT ALL PRIVILEGES ON DATABASE ${TRAVELYNX_DB_NAME} TO ${TRAVELYNX_DB_USERNAME}; +EOSQL \ No newline at end of file diff --git a/examples/docker/travelynx.conf b/examples/docker/travelynx.conf new file mode 100644 index 0000000..8f3f56f --- /dev/null +++ b/examples/docker/travelynx.conf @@ -0,0 +1,26 @@ +{ + cache => { + schedule => $ENV{TRAVELYNX_IRIS_CACHE} // '/var/cache/travelynx/iris', + realtime => $ENV{TRAVELYNX_IRISRT_CACHE} // '/var/cache/travelynx/iris-rt', + }, + db => { + host => $ENV{TRAVELYNX_DB_HOST} // 'die("Please set TRAVELYNX_DB_HOST")', + database => $ENV{TRAVELYNX_DB_NAME} // 'travelynx', + user => $ENV{TRAVELYNX_DB_USERNAME} // 'travelynx', + password => $ENV{TRAVELYNX_DB_PASSWORD} // die("Please set TRAVELYNX_DB_PASSWORD"), + }, + hypnotoad => { + accepts => $ENV{TRAVELYNX_HYPNOTOAD_ACCEPTS} // 100, + clients => $ENV{TRAVELYNX_HYPNOTOAD_CLIENTS} // 10, + listen => [ $ENV{TRALELYNX_HYPNOTOAD_LISTEN} // 'http://*:8093' ], + pid_file => '/tmp/travelynx.pid', + workers => $ENV{TRAVELYNX_HYPNOTOAD_WORKERS} // 2, + spare => $ENV{TRAVELYNX_HYPNOTOAD_SPARE} // 2, + }, + mail => { + disabled => $ENV{TRAVELYNX_MAIL_DISABLE} // 0, + }, + secrets => [ + $ENV{TRAVELYNX_SECRET} // die("Please set TRAVELYNX_SECRET"), + ], +}; -- cgit v1.2.3 From 2b5780499c3e00e1969c461b864994757a21a8d8 Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Fri, 3 May 2019 22:50:59 +0200 Subject: use cpanfile to install dependencies, run worker in another docker container --- .dockerignore | 1 - Dockerfile | 23 +++++------------------ docker-compose.yml | 27 +++++++++++++++++++-------- docker-run.sh | 50 ++++++++++++++++++++++++++++++++++---------------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/.dockerignore b/.dockerignore index 939fd11..2a11f34 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,5 @@ Dockerfile .dockerignore -examples/ .gitignore README.md travelynx.conf diff --git a/Dockerfile b/Dockerfile index b7067b9..289660a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,28 +2,16 @@ FROM debian:stretch-slim ARG DEBIAN_FRONTEND=noninteractive +COPY cpanfile /app/cpanfile +WORKDIR /app + RUN apt-get update && apt-get install --no-install-recommends -y \ cpanminus \ build-essential \ libpq-dev \ git \ - ssmtp \ - && cpanm -in --no-man-pages \ - Cache::File \ - Crypt::Eksblowfish \ - DateTime \ - DateTime::Format::Strptime \ - DBI \ - DBD::Pg \ - Email::Sender \ - Geo::Distance \ - Geo::Distance::XS \ - Mojolicious \ - Mojolicious::Plugin::Authentication \ - Travel::Status::DE::IRIS \ - UUID::Tiny \ - JSON \ - Mojo::Pg \ + cron \ + && cpanm -in --no-man-pages --installdeps . \ && rm -rf ~/.cpanm \ && apt-get purge -y \ build-essential \ @@ -31,6 +19,5 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && apt-get autoremove -y COPY . /app -WORKDIR /app CMD ["/app/docker-run.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index ac482e0..0b7336d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,6 +4,17 @@ x-common-env: &common-env TRAVELYNX_DB_NAME: travelynx TRAVELYNX_DB_USERNAME: travelynx TRAVELYNX_DB_PASSWORD: travelynx + TRAVELYNX_SECRET: 12345678 + TRAVELYNX_MAIL_DISABLE: 1 + MOJO_MODE: development + +x-common-config: &common-config + volumes: + - ./examples/docker/travelynx.conf:/app/travelynx.conf + build: . + networks: + - backend + services: database: image: postgres:11 @@ -14,16 +25,16 @@ services: volumes: - ./examples/docker/postgres-init.sh:/docker-entrypoint-initdb.d/init.sh travelynx: - build: . + <<: *common-config ports: - "8000:8093" - networks: - - backend - volumes: - - ./examples/docker/travelynx.conf:/app/travelynx.conf environment: <<: *common-env - TRAVELYNX_MAIL_DISABLE: 1 - TRAVELYNX_SECRET: 12345678 + cron: + <<: *common-config + environment: + <<: *common-env + CRON: 1 + networks: - backend: + backend: \ No newline at end of file diff --git a/docker-run.sh b/docker-run.sh index 29ffa7b..696aa35 100755 --- a/docker-run.sh +++ b/docker-run.sh @@ -4,6 +4,14 @@ set -eu WAIT_DB_HOST=${TRAVELYNX_DB_HOST} WAIT_DB_PORT=5432 +check_config() { + if [ ! -f travelynx.conf ] + then + echo "The configuration file is missing" + exit 1 + fi +} + wait_for_db() { set +e for i in $(seq 1 ${WAIT_DB_TIMEOUT:-5}) @@ -11,30 +19,40 @@ wait_for_db() { (echo >/dev/tcp/${WAIT_DB_HOST}/${WAIT_DB_PORT}) &>/dev/null if [ $? -eq 0 ]; then break + else + echo "Can't reach DB @ ${WAIT_DB_HOST}:${WAIT_DB_PORT}" fi sleep 1 done set -e } -if [ ! -f travelynx.conf ] -then - echo "The configuration file is missing" - exit 1 -fi +run_app() { + if [ \ + "${TRAVELYNX_MAIL_DISABLE:-0}" -eq 0 \ + -a "${TRAVELYNX_MAIL_HOST:-unset}" != "unset" \ + ] + then + export EMAIL_SENDER_TRANSPORT=SMTP + export EMAIL_SENDER_TRANSPORT_HOST=${TRAVELYNX_MAIL_HOST} + export EMAIL_SENDER_TRANSPORT_PORT=${TRAVELYNX_MAIL_PORT:-25} + fi -if [ \ - "${TRAVELYNX_MAIL_DISABLE:-0}" -eq 0 \ - -a "${TRAVELYNX_MAIL_HOST:-unset}" != "unset" \ -] -then - export EMAIL_SENDER_TRANSPORT=SMTP - export EMAIL_SENDER_TRANSPORT_HOST=${TRAVELYNX_MAIL_HOST} - export EMAIL_SENDER_TRANSPORT_PORT=${TRAVELYNX_MAIL_PORT:-25} -fi + perl index.pl database migrate + exec /usr/local/bin/hypnotoad -f index.pl +} + +run_cron() { + exec perl index.pl worker +} + +check_config wait_for_db -perl index.pl database migrate +if [ "${CRON:-0}" -ne "0" ] +then + run_cron +fi -exec /usr/local/bin/hypnotoad -f index.pl +run_app \ No newline at end of file -- cgit v1.2.3