소스 검색

build docker

appflowy 3 년 전
부모
커밋
6e51173335

+ 4 - 6
.dockerignore

@@ -1,7 +1,5 @@
-.env
-.dockerignore
-doc/
-scripts/
-rust-lib/target/
+frontend/app_flowy/
+frontend/scripts/
+frontend/rust-lib/target
 backend/target/
-app_flowy/
+shared-lib/target/

+ 1 - 3
backend/.dockerignore

@@ -6,6 +6,4 @@ deploy/
 tests/
 Dockerfile
 scripts/
-migrations/
-app_flowy/
-rust-lib/target/
+migrations/

+ 0 - 1
backend/.env.example

@@ -1 +0,0 @@
-DATABASE_URL="postgres://postgres:password@localhost:5433/flowy"

+ 0 - 1
backend/Cargo.toml

@@ -97,7 +97,6 @@ parking_lot = "0.11"
 once_cell = "1.7.2"
 linkify = "0.5.0"
 futures-util = "0.3.15"
-
 backend = { path = ".", features = ["flowy_test"]}
 flowy-sdk = { path = "../frontend/rust-lib/flowy-sdk", features = ["http_server"] }
 flowy-user = { path = "../frontend/rust-lib/flowy-user", features = ["http_server"] }

+ 6 - 23
backend/Dockerfile

@@ -1,28 +1,11 @@
-FROM lukemathwalker/cargo-chef:latest-rust-1.53.0 as chef
+FROM rust:1.56.1
 WORKDIR /app
-
-FROM chef as planner
 COPY . .
-RUN cargo chef prepare  --recipe-path recipe.json
 
-FROM chef as builder
-COPY --from=planner /app/recipe.json recipe.json
-# Build dependencies
-RUN cargo chef cook --release --recipe-path recipe.json
-COPY . .
 ENV SQLX_OFFLINE true
-# Build our project
-RUN cargo build --release --bin backend
-
-FROM debian:bullseye-slim AS runtime
-WORKDIR /app
-RUN apt-get update -y \
-    && apt-get install -y --no-install-recommends openssl \
-    # Clean up
-    && apt-get autoremove -y \
-    && apt-get clean -y \
-    && rm -rf /var/lib/apt/lists/*
-COPY --from=builder /app/target/release/backend backend
-COPY configuration configuration
+WORKDIR /app/backend
+RUN RUSTFLAGS="-C opt-level=2" cargo build --release --bin backend
+RUN cp target/release/backend backend
 ENV APP_ENVIRONMENT production
-ENTRYPOINT ["./backend"]
+EXPOSE 8000
+ENTRYPOINT ["./backend"]

+ 5 - 1
backend/scripts/Makefile → backend/Makefile

@@ -1,10 +1,14 @@
+ROOT = "./scripts"
+
 .PHONY:  init_database  add_migrations run_migrations reset_db
 
 init_database:
 	${ROOT}/init_database.sh
 
 run_docker:
-	source $(shell pwd)/env.sh && docker-compose up -d db && docker-compose up -d backend
+	source $(ROOT)/env.sh && docker-compose up -d db
+	source $(ROOT)/env.sh && docker-compose up -d backend
+	source $(ROOT)/env.sh && docker-compose logs backend
 
 reset_db:
 	#diesel database reset

+ 0 - 9
backend/configuration/base.example.yaml

@@ -1,9 +0,0 @@
-application:
-  port: 8000
-  host: 0.0.0.0
-database:
-  host: "localhost"
-  port: 5433
-  username: "postgres"
-  password: "password"
-  database_name: "flowy"

+ 1 - 0
backend/configuration/production.yaml

@@ -1,4 +1,5 @@
 application:
   host: 0.0.0.0
 database:
+  host: "db"
   require_ssl: false

+ 15 - 12
backend/docker-compose.yml

@@ -1,20 +1,23 @@
 version: '3'
 services:
+  db:
+    image: 'postgres:9.6-alpine'
+    environment:
+      - POSTGRES_USER=${POSTGRES_USER}
+      - POSTGRES_DB=${POSTGRES_DB}
+      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
+#    network_mode: "host"
+    ports:
+      - 5433:5432
   backend:
-    build:
-      context: .
-      dockerfile: Dockerfile
     environment:
-      APP_ENVIRONMENT: "production"
+      - APP_ENVIRONMENT=production
+      - DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"
+    build:
+      context: ../
+      dockerfile: ./backend/Dockerfile
+    image: flowy_backend:${BACKEND_VERSION}
     depends_on:
       - db
     ports:
       - 8000:8000
-  db:
-    image: 'postgres:14'
-    environment:
-      POSTGRES_USER: "${POSTGRES_USER}"
-      POSTGRES_DB: "${POSTGRES_DB}"
-      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
-    ports:
-      - 5433:5432

+ 7 - 0
backend/scripts/env.sh

@@ -1,9 +1,16 @@
 #!/bin/bash
 
+export POSTGRES_USER=postgres
+export POSTGRES_PASSWORD=password
+export POSTGRES_PORT=5433
+export POSTGRES_HOST=localhost
+export POSTGRES_DB=flowy
+
 export DB_USER="${POSTGRES_USER:=postgres}"
 export DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
 export DB_PORT="${POSTGRES_PORT:=5433}"
 export DB_HOST="${POSTGRES_HOST:=localhost}"
 export DB_NAME="${POSTGRES_DB:=flowy}"
 
+export BACKEND_VERSION="v0.0.1"
 export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}

+ 8 - 3
backend/scripts/init_database.sh

@@ -18,7 +18,11 @@ if ! [ -x "$(command -v sqlx)" ]; then
   exit 1
 fi
 
-source env.sh
+DB_USER="${POSTGRES_USER:=postgres}"
+DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
+DB_PORT="${POSTGRES_PORT:=5433}"
+DB_HOST="${POSTGRES_HOST:=localhost}"
+DB_NAME="${POSTGRES_DB:=flowy}"
 
 if [[ -z "${SKIP_DOCKER}" ]]
 then
@@ -34,14 +38,15 @@ then
       -e POSTGRES_PASSWORD=${DB_PASSWORD} \
       -e POSTGRES_DB="${DB_NAME}" \
       -p "${DB_PORT}":5432 \
-      -d postgres \
-      --name "postgres_$(date '+%s')" \
+      -d \
+      --name "flowy_postgres_$(date '+%s')" \
       postgres -N 1000
 fi
 
 
 # Keep pinging Postgres until it's ready to accept commands
 until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
+
   >&2 echo "Postgres is still unavailable - sleeping"
   sleep 1
 done

+ 1 - 0
backend/src/application.rs

@@ -32,6 +32,7 @@ pub struct Application {
 
 impl Application {
     pub async fn build(configuration: Settings, app_ctx: AppContext) -> Result<Self, std::io::Error> {
+        println!("🚀🚀🚀 {:?}", configuration);
         let address = format!("{}:{}", configuration.application.host, configuration.application.port);
         let listener = TcpListener::bind(&address)?;
         let port = listener.local_addr().unwrap().port();

+ 2 - 2
backend/src/config/configuration.rs

@@ -2,7 +2,7 @@ use serde_aux::field_attributes::deserialize_number_from_string;
 use sqlx::postgres::{PgConnectOptions, PgSslMode};
 use std::convert::{TryFrom, TryInto};
 
-#[derive(serde::Deserialize, Clone)]
+#[derive(serde::Deserialize, Clone, Debug)]
 pub struct Settings {
     pub database: DatabaseSettings,
     pub application: ApplicationSettings,
@@ -16,7 +16,7 @@ pub struct Settings {
 // any network interface. So using 127.0.0.1 for our local development and set
 // it to 0.0.0.0 in our Docker images.
 //
-#[derive(serde::Deserialize, Clone)]
+#[derive(serde::Deserialize, Clone, Debug)]
 pub struct ApplicationSettings {
     #[serde(deserialize_with = "deserialize_number_from_string")]
     pub port: u16,