Skip to content

Commit a5ee67b

Browse files
committed
Optimize docker workflow
1 parent 2adbb47 commit a5ee67b

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
lines changed

.env.default

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
### APP ########################################################################
2-
DATABASE_URL="postgres://Dandoh:dandoh@127.0.0.1:5432/webhaskell?sslmode=disable"
3-
JWT_SECRET="my_jwt_secret"
2+
DATABASE_URL=postgres://Dandoh:dandoh@127.0.0.1:5432/webhaskell?sslmode=disable
3+
JWT_SECRET=my_jwt_secret
44

55
### Docker ENV - PostgreSQL ####################################################
6-
POSTGRES_PASSWORD="dandoh"
7-
POSTGRES_USER="Dandoh"
8-
POSTGRES_DB="webhaskell"
6+
POSTGRES_PASSWORD=dandoh
7+
POSTGRES_USER=Dandoh
8+
POSTGRES_DB=webhaskell

Dockerfile

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
FROM haskell:8.6.5
1+
FROM haskell:8.8.3
22

3-
### Install PostgreSQL
4-
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
5-
RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
6-
RUN apt-get update
7-
RUN apt-get -y install postgresql-11
8-
RUN apt-get install libpq-dev
3+
### Install libpq-dev for postgres haskell lib to be built & entr for detecting changes and restarting stack
4+
RUN apt-get update \
5+
&& apt-get -y install libpq-dev entr \
6+
&& rm -rf /var/lib/apt/lists/*
97

10-
ARG pg_password
11-
ARG pg_user
12-
ARG pg_db
13-
USER postgres
14-
RUN /etc/init.d/postgresql start && psql --command "CREATE USER $pg_user WITH SUPERUSER PASSWORD '$pg_password';" && createdb -O $pg_user $pg_db
15-
USER root
8+
WORKDIR /root/
169

17-
### Install dbmate
18-
RUN curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/download/v1.7.0/dbmate-linux-amd64
19-
RUN chmod +x /usr/local/bin/dbmate
10+
## Prebuild dependencies
11+
COPY stack.* package.yaml /root/
12+
RUN stack build --only-dependencies
2013

21-
WORKDIR /root/
2214

2315
COPY ./entrypoint.sh /opt/entrypoint.sh
2416
RUN chmod +x /opt/entrypoint.sh
25-
ENTRYPOINT ["/opt/entrypoint.sh"]
2617

18+
COPY . /root
19+
20+
ENTRYPOINT ["/opt/entrypoint.sh"]

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,19 @@ You can also access Graphql Playground at [http://localhost:8080/graphiql](http:
7171
$ cp .env.default .env
7272
```
7373
- (Optional) Edit anything you need in the .env file
74+
7475
- Create and start docker containers
7576
```terminal
76-
$ docker-compose up -d
77-
```
78-
- Enter inside the docker workspace container
79-
```terminal
80-
$ docker exec -it web-haskell-graphql-postgres-boilerplate_workspace_1 /bin/bash
81-
```
82-
- Migrations
83-
```terminal
84-
$ dbmate up
85-
```
86-
- Run webserver
87-
```terminal
88-
$ stack run
77+
$ docker-compose up
8978
```
79+
9080
- Now you can visit: http://localhost:8080/ in your local machine.
9181

82+
- Migrations will automatically run, and you can run them manually anytime using
83+
```terminal
84+
$ docker-compose up dbmate
85+
```
86+
- Stack will restart whenever you change any .hs file, thanks to [entr](http://eradman.com/entrproject/)
9287

9388

9489
## Contributors
@@ -97,3 +92,4 @@ PR are more than welcome. The only note is we use `ormolu` to format codes.
9792

9893
- [Nhan Thai](https://github.com/dandoh)
9994
- [Pacific01](https://github.com/Pacific01)
95+
- [Emad Shaaban](https://github.com/emadshaaban92)

docker-compose.yml

+36-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,43 @@ version: '3.7'
22

33
services:
44

5+
db:
6+
image: postgres:12.2
7+
environment:
8+
- POSTGRES_PASSWORD
9+
- POSTGRES_USER
10+
- POSTGRES_DB
11+
volumes:
12+
- db-data:/var/lib/postgresql/data
13+
514
workspace:
6-
build:
7-
context: ./
8-
dockerfile: Dockerfile
9-
args:
10-
pg_password: ${POSTGRES_PASSWORD}
11-
pg_user: ${POSTGRES_USER}
12-
pg_db: ${POSTGRES_DB}
15+
build: ./
16+
depends_on:
17+
- db
18+
environment:
19+
- DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
20+
- JWT_SECRET
1321
volumes:
14-
- ./:/root/
22+
- ./app:/root/app
23+
- ./images:/root/images
24+
- ./src:/root/src
25+
- ./.env:/root/.env
26+
- ./graphiql.html:/root/graphiql.html
27+
- ./schema.graphql:/root/schema.graphql
28+
- ./package.yaml:/root/package.yaml
29+
- ./stack.yaml:/root/stack.yaml
1530
ports:
1631
- 8080:8080
32+
33+
dbmate:
34+
image: amacneil/dbmate:v1.8.0
35+
environment:
36+
- DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
37+
depends_on:
38+
- db
39+
volumes:
40+
- ./db:/db/
41+
command: --wait up
42+
43+
volumes:
44+
db-data:

entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ trap 'kill ${!}; term_handler' SIGTERM
2222
# wait forever
2323
while true
2424
do
25-
tail -f /dev/null & wait ${!}
25+
ls -d **/*.hs | entr -d -r stack run
2626
done
2727

stack.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
# resolver: ./custom-snapshot.yaml
1919
# resolver: https://example.com/snapshots/2018-01-01.yaml
20-
resolver: lts-15.9
20+
resolver: lts-15.12
2121

2222
# User packages to be built.
2323
# Various formats can be used as shown in the example below.

0 commit comments

Comments
 (0)