Skip to content

Commit

Permalink
chore: add docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Devesh Rawat committed Jan 11, 2024
1 parent e08b6b9 commit ada7522
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
target/
tests/
Dockerfile
scripts/
migrations/
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

env:
CARGO_TERM_COLOR: always
SQLX_VERSION: 0.7.1
SQLX_VERSION: 0.7.3
SQLX_FEATURES: "rustls,postgres"

# A workflow run is made up of one or more jobs, which run in parallel by default
Expand Down
27 changes: 24 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
FROM rust:latest
FROM lukemathwalker/cargo-chef:latest-rust-latest as chef
WORKDIR /app
RUN apt update && apt install lld clang -y
FROM chef as planner
COPY . .
# Compute a lock-like file for our project
RUN cargo chef prepare --recipe-path recipe.json
FROM chef as builder
COPY --from=planner /app/recipe.json recipe.json
# Build our project dependencies, not our application!
RUN cargo chef cook --release --recipe-path recipe.json
# Up to this point, if our dependency tree stays the same,
# all layers should be cached.
COPY . .
ENV SQLX_OFFLINE true
RUN cargo build --release
# Build our project
RUN cargo build --release --bin newsLetter
FROM debian:latest AS runtime
WORKDIR /app
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends openssl ca-certificates \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/newsLetter newsLetter
COPY configuration configuration
ENV APP_ENVIRONMENT production
ENTRYPOINT ["./target/release/newsLetter"]
ENTRYPOINT ["./newsLetter"]
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3.8"

services:
newsletter:
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000" # Adjust the port as needed
environment:
- APP_ENVIRONMENT=production
depends_on:
- postgres
command: ["./newsLetter"]

postgres:
image: postgres:latest
environment:
POSTGRES_DB: newsletter
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
19 changes: 19 additions & 0 deletions spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: newsLetter

region: BLR1

services:
- name: newsLetter
dockerfile_path: Dockerfile
source_dir: .
github:
branch: main
deploy_on_push: true
repo: PrognosticatorR/news_letter
health_check:
http_path: /health_check
http_port: 8000
instance_count: 1
instance_size_slug: basic-xxs
routes:
- path: /
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use newsLetter::configuration::get_configuration;
use newsLetter::startup::run;
use newsLetter::telemetry;
use secrecy::ExposeSecret;
use sqlx::postgres::PgPool;
use sqlx::postgres::PgPoolOptions;
use std::net::TcpListener;

#[tokio::main]
Expand All @@ -14,14 +14,16 @@ async fn main() -> std::io::Result<()> {
let subscriber = telemetry::get_subscriber("newsLetter".into(), "info".into(), std::io::stdout);
telemetry::init_subscriber(subscriber);
let configuration = get_configuration().expect("Failed to read configuration.");
let connection_pool =
PgPool::connect_lazy(configuration.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");
let connection_pool = PgPoolOptions::new()
.acquire_timeout(std::time::Duration::from_secs(5))
.max_connections(20)
.connect_lazy(configuration.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");
let address = format!(
"{}:{}",
configuration.application.host, configuration.application.port
);
println!("listening on: {}", address);
println!(" Server is up and running on : {}", address);
let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await?;
Ok(())
Expand Down

0 comments on commit ada7522

Please sign in to comment.