From 68f9cb550901decb4e6ff73ea3260c270299a0b9 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Fri, 16 Jul 2021 02:21:22 +0700 Subject: [PATCH] build: setup docker compose --- api/.env.example | 7 +++ api/.gitignore | 4 +- api/Dockerfile | 2 + client/Dockerfile | 2 + database/postgres/.gitignore | 5 ++ database/postgres/Dockerfile | 13 ++++++ database/postgres/self-signed-ssl | 58 +++++++++++++++++++++++ database/redis/.gitignore | 2 + docker-compose.yml | 76 +++++++++++++++++++++++++++++++ 9 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 api/.env.example create mode 100644 database/postgres/.gitignore create mode 100644 database/postgres/Dockerfile create mode 100644 database/postgres/self-signed-ssl create mode 100644 database/redis/.gitignore create mode 100644 docker-compose.yml diff --git a/api/.env.example b/api/.env.example new file mode 100644 index 0000000..4c6d416 --- /dev/null +++ b/api/.env.example @@ -0,0 +1,7 @@ +ENV=development +PORT=5000 + +DATABASE_URL=postgres://postgres:password@localhost:5432/jokesbapak2 +REDIS_URL=redis://@localhost:6379 + +SENTRY_DSN= \ No newline at end of file diff --git a/api/.gitignore b/api/.gitignore index 94859b2..81220ee 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -18,4 +18,6 @@ vendor/ .env # Heroku bin directory -bin \ No newline at end of file +bin + +main \ No newline at end of file diff --git a/api/Dockerfile b/api/Dockerfile index 6d822c4..81c6454 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -7,4 +7,6 @@ COPY . . RUN go mod download RUN go build -v main.go +EXPOSE ${PORT} + CMD ["./main"] \ No newline at end of file diff --git a/client/Dockerfile b/client/Dockerfile index a907119..5ddafac 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -8,4 +8,6 @@ RUN npm install -g yarn RUN yarn install RUN yarn build +EXPOSE ${PORT} + CMD ["yarn", "preview"] \ No newline at end of file diff --git a/database/postgres/.gitignore b/database/postgres/.gitignore new file mode 100644 index 0000000..242621b --- /dev/null +++ b/database/postgres/.gitignore @@ -0,0 +1,5 @@ +data +.crt +.key +.cnf +.dh \ No newline at end of file diff --git a/database/postgres/Dockerfile b/database/postgres/Dockerfile new file mode 100644 index 0000000..c9620d4 --- /dev/null +++ b/database/postgres/Dockerfile @@ -0,0 +1,13 @@ +FROM postgres:13.3-alpine + +WORKDIR /var/lib/postgresql + +COPY . . + +RUN apk add openssl + +RUN chmod +x self-signed-ssl +RUN ./self-signed-ssl + +RUN chown postgres /var/lib/postgresql/server.key && \ + chmod 600 /var/lib/postgresql/server.key \ No newline at end of file diff --git a/database/postgres/self-signed-ssl b/database/postgres/self-signed-ssl new file mode 100644 index 0000000..4f9b97d --- /dev/null +++ b/database/postgres/self-signed-ssl @@ -0,0 +1,58 @@ +#!/bin/bash + +# COPIED/MODIFIED from the postgres server gen-certs util + +# Generate some test certificates which are used by the regression test suite: +# +# tls/ca.{crt,key} Self signed CA certificate. +# tls/postgres.{crt,key} A certificate with no key usage/policy restrictions. +# tls/client.{crt,key} A certificate restricted for SSL client usage. +# tls/server.{crt,key} A certificate restricted for SSL server usage. +# tls/postgres.dh DH Params file. + +generate_cert() { + local name=$1 + local cn="$2" + local opts="$3" + + local keyfile=${name}.key + local certfile=${name}.crt + + [ -f $keyfile ] || openssl genrsa -out $keyfile 2048 + openssl req \ + -new -sha256 \ + -subj "/O=postgres Test/CN=$cn" \ + -key $keyfile | \ + openssl x509 \ + -req -sha256 \ + -CA ca.crt \ + -CAkey ca.key \ + -CAserial ca.txt \ + -CAcreateserial \ + -days 365 \ + $opts \ + -out $certfile +} + +[ -f ca.key ] || openssl genrsa -out ca.key 4096 +openssl req \ + -x509 -new -nodes -sha256 \ + -key ca.key \ + -days 3650 \ + -subj '/O=postgres Test/CN=Certificate Authority' \ + -out ca.crt + +cat > openssl.cnf <<_END_ +[ server_cert ] +keyUsage = digitalSignature, keyEncipherment +nsCertType = server +[ client_cert ] +keyUsage = digitalSignature, keyEncipherment +nsCertType = client +_END_ + +generate_cert server "Server-only" "-extfile openssl.cnf -extensions server_cert" +generate_cert client "Client-only" "-extfile openssl.cnf -extensions client_cert" +generate_cert postgres "Generic-cert" + +[ -f postgres.dh ] || openssl dhparam -out postgres.dh 2048 \ No newline at end of file diff --git a/database/redis/.gitignore b/database/redis/.gitignore new file mode 100644 index 0000000..787382c --- /dev/null +++ b/database/redis/.gitignore @@ -0,0 +1,2 @@ +data +etc \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dd659c1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,76 @@ +services: + api: + build: ./api/ + restart: always + env_file: ./api/.env + ports: + - 5000:5000 + depends_on: + - db + - cache + # Uncomment these if you want to have it on + # volumes: + # ./api:/app + + client: + build: ./client/ + restart: always + env_file: ./client/.env + ports: + - 3000:3000 + depends_on: + - api + # Uncomment these if you want to have it on + # volumes: + # ./client:/app + + db: + build: ./database/postgres/ + command: > + -c ssl=on + -c ssl_cert_file=/var/lib/postgresql/server.crt + -c ssl_key_file=/var/lib/postgresql/server.key + restart: always + ports: + - 5432:5432 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password + POSTGRES_DB: jokesbapak2 + PGDATA: /data/postgres + # I got this key from somewhere. It works when you run it locally. + POSTGRES_SSL_CA_CERT: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURjekNDQWx1Z0F3SUJBZ0lVR3lDaElvR3g0 + volumes: + - ./database/postgres/data:/data/postgres + + cache: + image: redis:6.2.4-alpine + restart: always + ports: + - 6379:6379 + volumes: + - ./database/redis/etc:/usr/local/etc/redis + - ./database/redis/data:/data + + cache-admin: + image: rediscommander/redis-commander:latest + restart: always + environment: + REDIS_PORT: 6379 + REDIS_HOST: redis + ports: + - 2084:8081 + depends_on: + - cache + + db-admin: + image: sosedoff/pgweb:0.11.8 + restart: always + ports: + - 2086:8081 + links: + - postgres:postgres + environment: + DATABASE_URL: postgres://postgres:password@db/jokesbapak2 + depends_on: + - db \ No newline at end of file