Skip to content

Commit

Permalink
Merge branch 'feature/docker' into development
Browse files Browse the repository at this point in the history
* feature/docker:
  How to build and run the project with Docker in README
  New ENVS to configure Postgres on building docker image and using docker container
  Ignore some files from root user inside the docker container
  Added Dockerfile image from haskell:8.6.5 with dbmate and pgsql, a docker-compose with a daemon entrypoint
  • Loading branch information
Pacific01 committed Mar 20, 2020
2 parents bc57b76 + 9f7ef98 commit dc060b5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .env.default
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
### APP ########################################################################
DATABASE_URL="postgres://Dandoh:[email protected]:5432/webhaskell?sslmode=disable"
JWT_SECRET="my_jwt_secret"

### PostgreSQL #################################################################
POSTGRES_PASSWORD="dandoh"
POSTGRES_USER="Dandoh"
POSTGRES_DB="webhaskell"
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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-*
Expand Down Expand Up @@ -32,4 +37,4 @@ cabal.project.local~
linkedin.yaml
*.jar
.env
screenshot.png
screenshot.png
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dc060b5

Please sign in to comment.