diff --git a/.env.default b/.env.default index 382a61a..804b1ae 100644 --- a/.env.default +++ b/.env.default @@ -1,2 +1,8 @@ +### APP ######################################################################## DATABASE_URL="postgres://Dandoh:dandoh@127.0.0.1:5432/webhaskell?sslmode=disable" JWT_SECRET="my_jwt_secret" + +### PostgreSQL ################################################################# +POSTGRES_PASSWORD="dandoh" +POSTGRES_USER="Dandoh" +POSTGRES_DB="webhaskell" diff --git a/.gitignore b/.gitignore index 9973e49..35100db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ .stack-work/ li-scraper.cabal *~ +## Docker root user files +.bash_history +.psql_history +.stack/ +stack.yaml.lock ### Haskell template dist dist-* @@ -32,4 +37,4 @@ cabal.project.local~ linkedin.yaml *.jar .env -screenshot.png \ No newline at end of file +screenshot.png diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..343b7c2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM haskell:8.6.5 + +### Install PostgreSQL +RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list +RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - +RUN apt-get update +RUN apt-get -y install postgresql-11 +RUN apt-get install libpq-dev + +ARG pg_password +ARG pg_user +ARG pg_db +USER postgres +RUN /etc/init.d/postgresql start && psql --command "CREATE USER $pg_user WITH SUPERUSER PASSWORD '$pg_password';" && createdb -O $pg_user $pg_db +USER root + +### Install dbmate +RUN curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/download/v1.7.0/dbmate-linux-amd64 +RUN chmod +x /usr/local/bin/dbmate + +WORKDIR /root/ + +COPY ./entrypoint.sh /opt/entrypoint.sh +RUN chmod +x /opt/entrypoint.sh +ENTRYPOINT ["/opt/entrypoint.sh"] + diff --git a/README.md b/README.md index 0655262..2828dc3 100644 --- a/README.md +++ b/README.md @@ -56,3 +56,27 @@ This boilerplate wires up: ```terminal $ stack run ``` + +## Running on Docker +- Feed in you database & secret in `.env`: + ```terminal + $ cp .env.default .env + ``` +- (Optional) Edit anything you need in the .env file +- Create and start docker containers + ```terminal + $ docker-compose up -d + ``` +- Enter inside the docker workspace container + ```terminal + $ docker exec -it web-haskell-graphql-postgres-boilerplate_workspace_1 /bin/bash + ``` +- Migrations + ```terminal + $ dbmate up + ``` +- Run webserver + ```terminal + $ stack run + ``` +- Now you can visit: http://localhost:8080/ in your local machine. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..91357fc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3.7' + +services: + + workspace: + build: + context: ./ + dockerfile: Dockerfile + args: + pg_password: ${POSTGRES_PASSWORD} + pg_user: ${POSTGRES_USER} + pg_db: ${POSTGRES_DB} + volumes: + - ./:/root/ + ports: + - 8080:8080 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..0158ef1 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -x + +pid=0 + +# SIGTERM-handler +term_handler() { + if [ $pid -ne 0 ]; then + kill -SIGTERM "$pid" + wait "$pid" + fi + exit 143; # 128 + 15 -- SIGTERM +} + +# setup handlers +# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler +trap 'kill ${!}; term_handler' SIGTERM + +# Start PostgreSQL service +/etc/init.d/postgresql start + +# wait forever +while true +do + tail -f /dev/null & wait ${!} +done +