From f9055646ce1725ac7919934356fb90f2ba63da17 Mon Sep 17 00:00:00 2001 From: Shub1427 Date: Sat, 3 Aug 2019 00:40:42 +0530 Subject: [PATCH 1/4] feat: easy setup for Postgre on Linux with Docker --- docker-postgres.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 docker-postgres.sh diff --git a/docker-postgres.sh b/docker-postgres.sh new file mode 100755 index 0000000..da38713 --- /dev/null +++ b/docker-postgres.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +docker run \ + --name spam-watch-postgres \ + -p 5432:5432 \ + -e POSTGRES_PASSWORD=sub123 \ + -e POSTGRES_USER="spam-watch" \ + -e POSTGRES_DB="spam-watch-db" \ + postgres \ No newline at end of file From 18a6ad70469ed6b7748dfeaa2394ae216b94cb52 Mon Sep 17 00:00:00 2001 From: Shub1427 Date: Sat, 3 Aug 2019 20:14:00 +0530 Subject: [PATCH 2/4] feat: add proper docker compose file --- .gitignore | 3 +++ Dockerfile | 14 ++++++++++++++ README.md | 14 ++++++++++++++ docker-compose.yml | 28 ++++++++++++++++++++++++++++ docker-postgres.sh | 9 --------- example.config.toml | 6 ++++++ 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml delete mode 100755 docker-postgres.sh diff --git a/.gitignore b/.gitignore index 1fcf09e..230f12b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ **/*.rs.bk .idea config.toml + +# Remove Secrets +secrets/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e561b55 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# This is dev version to make the Cargo builds fast and should re-build and run the whole +# app everytime the container is restarted, as Rust doesn't has a proper way of hot reloading +FROM rust:latest + +WORKDIR /app/spam-watch-api + +VOLUME ./ /app/spam-watch-api + +# This will cache the library builds, which will ultimately help in +# re-running our apps faster. +RUN cargo build + +CMD ["cargo", "run"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f4707c --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Spam Watch API + +API server for Spam Watch Bot + +## Running via Docker + +Before running `docker-compose` please make sure you have added: +* Postgres password in `/secrets/.postgre-passwd` +* Created `config.toml` from `example.config.toml`. + +``` +docker-compose up [-d] +``` + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..70056df --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +version: "3.7" + +secrets: + postgres_passwd: + file: ./secrets/.postgres-passwd + +services: + postgres: + image: postgres:11.4-alpine + # for production we can remove this exposed ports, as it helps in security + # while debugging for db in prod, should be done via a client container. + ports: + - '5432:5432' + secrets: + - postgres_passwd + environment: + POSTGRES_USER: SpamWatchAPI + POSTGRES_DB: SpamWatchAPI + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_passwd + api: + build: + context: ./ + dockerfile: Dockerfile + ports: + - '6345:6345' + volumes: + - ./:/app/spam-watch-api + diff --git a/docker-postgres.sh b/docker-postgres.sh deleted file mode 100755 index da38713..0000000 --- a/docker-postgres.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -docker run \ - --name spam-watch-postgres \ - -p 5432:5432 \ - -e POSTGRES_PASSWORD=sub123 \ - -e POSTGRES_USER="spam-watch" \ - -e POSTGRES_DB="spam-watch-db" \ - postgres \ No newline at end of file diff --git a/example.config.toml b/example.config.toml index c06f536..0175c13 100644 --- a/example.config.toml +++ b/example.config.toml @@ -8,3 +8,9 @@ host = "127.0.0.1" password = "password" username = "SpamWatchAPI" name = "SpamWatchAPI" + +[server] +# this is required for docker containers, to be available from +# bridge, for any exposed port +host = "0.0.0.0" +port = 6345 From c9fd99c5496643f6c8d09fab42ac66e86e9c22e1 Mon Sep 17 00:00:00 2001 From: Shub1427 Date: Fri, 16 Aug 2019 20:42:59 +0530 Subject: [PATCH 3/4] fix: Volume mount error on image build --- .dockerignore | 2 ++ Dockerfile | 7 +++++-- Dockerfile.prod | 17 +++++++++++++++++ README.md | 6 +++--- docker-compose.prod.yml | 26 ++++++++++++++++++++++++++ docker-compose.yml | 2 +- 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile.prod create mode 100644 docker-compose.prod.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..342ee83 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +target/ +Dockerfile diff --git a/Dockerfile b/Dockerfile index e561b55..9608ed6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,13 @@ # This is dev version to make the Cargo builds fast and should re-build and run the whole # app everytime the container is restarted, as Rust doesn't has a proper way of hot reloading + +# Keeping Rust to it's latest version for having the latest Rust Dependency. +# Once async/await is introduced, we can hard-code the version. FROM rust:latest -WORKDIR /app/spam-watch-api +WORKDIR /app/spamwatch-api -VOLUME ./ /app/spam-watch-api +COPY . /app/spamwatch-api # This will cache the library builds, which will ultimately help in # re-running our apps faster. diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..ac61b71 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,17 @@ +# This is dev version to make the Cargo builds fast and should re-build and run the whole +# app everytime the container is restarted, as Rust doesn't has a proper way of hot reloading + +# Keeping Rust to it's latest version for having the latest Rust Dependency. +# Once async/await is introduced, we can hard-code the version. +FROM rust:1.36 + +WORKDIR /app/spamwatch-api + +COPY ./ /app/spamwatch-api + +# This will cache the library builds, which will ultimately help in +# re-running our apps faster. +RUN cargo build --release + +CMD ["cargo", "run"] + diff --git a/README.md b/README.md index 9f4707c..6e729d2 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# Spam Watch API +# SpamWatchAPI -API server for Spam Watch Bot +API server for SpamWatch ## Running via Docker Before running `docker-compose` please make sure you have added: -* Postgres password in `/secrets/.postgre-passwd` +* Postgres password in `/secrets/.postgres-passwd` * Created `config.toml` from `example.config.toml`. ``` diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..2f64c18 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,26 @@ +version: "3.7" + +secrets: + postgres_passwd: + file: ./secrets/.postgres-passwd + +services: + postgres: + image: postgres:11.4-alpine + # for production we can remove this exposed ports, as it helps in security + # while debugging for db in prod, should be done via a client container. + ports: + - '5432:5432' + secrets: + - postgres_passwd + environment: + POSTGRES_USER: SpamWatchAPI + POSTGRES_DB: SpamWatchAPI + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_passwd + api: + build: + context: ./ + dockerfile: Dockerfile + ports: + - '6345:6345' + diff --git a/docker-compose.yml b/docker-compose.yml index 70056df..4ee8528 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,5 +24,5 @@ services: ports: - '6345:6345' volumes: - - ./:/app/spam-watch-api + - ${PWD}:/app/spamwatch-api From 6b803a5cc5d54bf6f8480115cce8fbd92bba40e9 Mon Sep 17 00:00:00 2001 From: Shub1427 Date: Fri, 16 Aug 2019 22:12:59 +0530 Subject: [PATCH 4/4] fix: api service dependency for postgres --- Dockerfile | 2 ++ Dockerfile.prod | 7 ------- README.md | 3 ++- docker-compose.prod.yml | 6 ++---- docker-compose.yml | 2 ++ example.config.toml | 1 + 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9608ed6..4ba3602 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,7 @@ COPY . /app/spamwatch-api # re-running our apps faster. RUN cargo build +ENV RUST_BACKTRACE 1 + CMD ["cargo", "run"] diff --git a/Dockerfile.prod b/Dockerfile.prod index ac61b71..8866819 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -1,16 +1,9 @@ -# This is dev version to make the Cargo builds fast and should re-build and run the whole -# app everytime the container is restarted, as Rust doesn't has a proper way of hot reloading - -# Keeping Rust to it's latest version for having the latest Rust Dependency. -# Once async/await is introduced, we can hard-code the version. FROM rust:1.36 WORKDIR /app/spamwatch-api COPY ./ /app/spamwatch-api -# This will cache the library builds, which will ultimately help in -# re-running our apps faster. RUN cargo build --release CMD ["cargo", "run"] diff --git a/README.md b/README.md index 6e729d2..d350292 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ API server for SpamWatch Before running `docker-compose` please make sure you have added: * Postgres password in `/secrets/.postgres-passwd` -* Created `config.toml` from `example.config.toml`. +* Create `config.toml` from `example.config.toml`. + - Do change the hostname for [database] config to `host = "postgres"` ``` docker-compose up [-d] diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 2f64c18..e75dbdd 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -7,10 +7,8 @@ secrets: services: postgres: image: postgres:11.4-alpine - # for production we can remove this exposed ports, as it helps in security - # while debugging for db in prod, should be done via a client container. - ports: - - '5432:5432' + # for production we don't need to expose ports + - '5432' secrets: - postgres_passwd environment: diff --git a/docker-compose.yml b/docker-compose.yml index 4ee8528..41872ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,8 @@ services: build: context: ./ dockerfile: Dockerfile + depends_on: + - postgres ports: - '6345:6345' volumes: diff --git a/example.config.toml b/example.config.toml index 0175c13..233dc66 100644 --- a/example.config.toml +++ b/example.config.toml @@ -5,6 +5,7 @@ masterid = 777000 [database] host = "127.0.0.1" +port = 5432 password = "password" username = "SpamWatchAPI" name = "SpamWatchAPI"