From a5ee67bdee7d5a92124df7aa4e13e49005fb7f68 Mon Sep 17 00:00:00 2001 From: Emad Shaaban Date: Wed, 13 May 2020 18:04:11 +0200 Subject: [PATCH] Optimize docker workflow --- .env.default | 10 +++++----- Dockerfile | 30 ++++++++++++------------------ README.md | 22 +++++++++------------- docker-compose.yml | 44 ++++++++++++++++++++++++++++++++++++-------- entrypoint.sh | 2 +- stack.yaml | 2 +- 6 files changed, 64 insertions(+), 46 deletions(-) diff --git a/.env.default b/.env.default index 738bec2..f6bcee9 100644 --- a/.env.default +++ b/.env.default @@ -1,8 +1,8 @@ ### APP ######################################################################## -DATABASE_URL="postgres://Dandoh:dandoh@127.0.0.1:5432/webhaskell?sslmode=disable" -JWT_SECRET="my_jwt_secret" +DATABASE_URL=postgres://Dandoh:dandoh@127.0.0.1:5432/webhaskell?sslmode=disable +JWT_SECRET=my_jwt_secret ### Docker ENV - PostgreSQL #################################################### -POSTGRES_PASSWORD="dandoh" -POSTGRES_USER="Dandoh" -POSTGRES_DB="webhaskell" +POSTGRES_PASSWORD=dandoh +POSTGRES_USER=Dandoh +POSTGRES_DB=webhaskell diff --git a/Dockerfile b/Dockerfile index 343b7c2..749f772 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,20 @@ -FROM haskell:8.6.5 +FROM haskell:8.8.3 -### 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 +### Install libpq-dev for postgres haskell lib to be built & entr for detecting changes and restarting stack +RUN apt-get update \ + && apt-get -y install libpq-dev entr \ + && rm -rf /var/lib/apt/lists/* -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 +WORKDIR /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 +## Prebuild dependencies +COPY stack.* package.yaml /root/ +RUN stack build --only-dependencies -WORKDIR /root/ COPY ./entrypoint.sh /opt/entrypoint.sh RUN chmod +x /opt/entrypoint.sh -ENTRYPOINT ["/opt/entrypoint.sh"] +COPY . /root + +ENTRYPOINT ["/opt/entrypoint.sh"] diff --git a/README.md b/README.md index ef11d1a..a7aec0e 100644 --- a/README.md +++ b/README.md @@ -71,24 +71,19 @@ You can also access Graphql Playground at [http://localhost:8080/graphiql](http: $ 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 + $ docker-compose up ``` + - Now you can visit: http://localhost:8080/ in your local machine. +- Migrations will automatically run, and you can run them manually anytime using + ```terminal + $ docker-compose up dbmate + ``` +- Stack will restart whenever you change any .hs file, thanks to [entr](http://eradman.com/entrproject/) ## Contributors @@ -97,3 +92,4 @@ PR are more than welcome. The only note is we use `ormolu` to format codes. - [Nhan Thai](https://github.com/dandoh) - [Pacific01](https://github.com/Pacific01) +- [Emad Shaaban](https://github.com/emadshaaban92) diff --git a/docker-compose.yml b/docker-compose.yml index 91357fc..beb17cc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,15 +2,43 @@ version: '3.7' services: + db: + image: postgres:12.2 + environment: + - POSTGRES_PASSWORD + - POSTGRES_USER + - POSTGRES_DB + volumes: + - db-data:/var/lib/postgresql/data + workspace: - build: - context: ./ - dockerfile: Dockerfile - args: - pg_password: ${POSTGRES_PASSWORD} - pg_user: ${POSTGRES_USER} - pg_db: ${POSTGRES_DB} + build: ./ + depends_on: + - db + environment: + - DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable + - JWT_SECRET volumes: - - ./:/root/ + - ./app:/root/app + - ./images:/root/images + - ./src:/root/src + - ./.env:/root/.env + - ./graphiql.html:/root/graphiql.html + - ./schema.graphql:/root/schema.graphql + - ./package.yaml:/root/package.yaml + - ./stack.yaml:/root/stack.yaml ports: - 8080:8080 + + dbmate: + image: amacneil/dbmate:v1.8.0 + environment: + - DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable + depends_on: + - db + volumes: + - ./db:/db/ + command: --wait up + +volumes: + db-data: \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 0158ef1..f228b49 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -22,6 +22,6 @@ trap 'kill ${!}; term_handler' SIGTERM # wait forever while true do - tail -f /dev/null & wait ${!} + ls -d **/*.hs | entr -d -r stack run done diff --git a/stack.yaml b/stack.yaml index 0a8f9e3..3aa38a6 100644 --- a/stack.yaml +++ b/stack.yaml @@ -17,7 +17,7 @@ # # resolver: ./custom-snapshot.yaml # resolver: https://example.com/snapshots/2018-01-01.yaml -resolver: lts-15.9 +resolver: lts-15.12 # User packages to be built. # Various formats can be used as shown in the example below.