Skip to content

Commit

Permalink
fix: docker build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rashed091 committed Jun 9, 2024
1 parent a13ebc4 commit b3d8f75
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
13 changes: 10 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
DATABASE=postgres
BACKEND_CONTAINER_NAME=backend
MIGRATION_CONTAINER_NAME=migration

HOST=0.0.0.0
PORT=5001
DATABASE_URL="postgres://postgres:admin@localhost:5432/demo"
DATABASE_CONTAINER_NAME=localdb
BACKEND_CONTAINER_NAME=backend

POSTGRES_DB=fakes
POSTGRES_PASSWORD=admin
POSTGRES_USER=postgres
POSTGRES_DB_PORT=5432
DATABASE_URL="${DATABASE}://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE}:${POSTGRES_DB_PORT}/${POSTGRES_DB}"
27 changes: 7 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
######################################-Variables-##########################################
# version can be latest or specific like 1.73.0
ARG RUST_VERSION=latest
ARG APP_NAME="unknown"
# version can be latest or specific like 1.78.0-slim
ARG RUST_VERSION="1.78.0-slim"
ARG APP_NAME="rustful"
ARG PORT=5001

######################################-Chef-##########################################
Expand All @@ -11,8 +11,7 @@ FROM rust:${RUST_VERSION} as chef

WORKDIR /app

RUN apt update && apt upgrade --no-install-recommends -y && \
apt install --no-install-recommends pkg-config libssl-dev libpq-dev -y
RUN apt update && apt upgrade --no-install-recommends -y && apt install --no-install-recommends pkg-config libssl-dev libpq-dev -y

# Install diesel CLI for migration
RUN cargo install diesel_cli --no-default-features --features postgres
Expand Down Expand Up @@ -50,22 +49,11 @@ CMD ["diesel", "migration", "run"]
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage. We do not need the Rust toolchain to run the binary!
FROM gcr.io/distroless/cc-debian12 as final
FROM debian:stable-slim as final

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
# ARG UID=10001
# RUN adduser \
# --disabled-password \
# --gecos "" \
# --home "/nonexistent" \
# --shell "/sbin/nologin" \
# --no-create-home \
# --uid "${UID}" \
# appuser
# USER appuser
RUN apt update && apt upgrade --no-install-recommends -y && apt install libpq-dev -y

COPY --from=builder /app/.env /app/.env
COPY --from=builder /app/target/release/${APP_NAME} /app/${APP_NAME}
Expand All @@ -74,7 +62,6 @@ COPY --from=builder /app/target/release/${APP_NAME} /app/${APP_NAME}
EXPOSE ${PORT}

# What the container should run when it is started.
# CMD ["bash", "-c", "./author diesel migration run && ./author"]
CMD ["${APP_NAME}"]
CMD ["./rustful"]

#------------------------------------------End-----------------------------------------------
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ setup:
cargo install cargo-watch
cargo install diesel_cli --no-default-features --features postgres

db:
services:
@echo "Starting all services"
docker compose up

data:
@echo "Starting postgres container"
docker compose up -d db

Expand All @@ -30,20 +34,20 @@ compose:
run: is_db_running
cargo watch -q -c -w src/ -x run

compile: has_postgres
compile: is_db_running
cargo build --release

build:
docker build \
docker image build \
--build-arg APP_NAME=$(APP_NAME) \
--build-arg PORT=$(PORT) \
--tag $(DOCKER_HUB_REPO)/$(APP_NAME):$(VERSION) .

tag: build
docker tag $(DOCKER_HUB_REPO)/$(APP_NAME):$(VERSION) $(DOCKER_HUB_REPO)/$(APP_NAME):latest
docker image tag $(DOCKER_HUB_REPO)/$(APP_NAME):$(VERSION) $(DOCKER_HUB_REPO)/$(APP_NAME):latest

push: tag
docker push $(DOCKER_HUB_REPO)/$(APP_NAME):latest
docker image push $(DOCKER_HUB_REPO)/$(APP_NAME):latest

multi:
./build.sh
Expand Down
5 changes: 2 additions & 3 deletions check_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ if [ -z "$container_name" ]; then
fi

# Use docker ps to filter for the container and get its container ID
container_id=$(docker ps -q --filter name=^/$(container_name)$ --filter status=running)
container_id=$(docker ps -q --filter name=^/$container_name$ --filter status=running)

# Check if the container ID is empty (meaning the container is not running)
if [ -z "$container_id" ]; then
echo "$container_name is not running."
echo "Error: Please make sure $container_name is running."
echo "Error: $container_name is not running. Please make sure $container_name is running."
exit 2
else
echo "$container_name is running."
Expand Down
52 changes: 33 additions & 19 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
name: rustful


services:
db:
postgres:
image: postgres:latest
container_name: ${DATABASE_CONTAINER_NAME}
container_name: ${DATABASE}
restart: always
user: postgres
user: ${POSTGRES_USER}
secrets:
- db-password
- postgres_password
volumes:
- db-data:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=demo
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
expose:
- 5432
ports:
- 5432:5432
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 30s
timeout: 10s
retries: 3
test: [ "CMD", "pg_isready"]
start_period: 5s
start_interval: 10s
interval: 10s
timeout: 5s
retries: 5

migration:
build:
context: .
target: migration
container_name: ${MIGRATION_CONTAINER_NAME}
depends_on:
postgres:
condition: service_healthy

backend:
image: mrasheduzzaman/rust-service:latest
build:
context: .
target: final
container_name: ${BACKEND_CONTAINER_NAME}
restart: always
ports:
- 5001:5001
depends_on:
db:
postgres:
condition: service_healthy
migration:
condition: service_healthy
restart: true



volumes:
db-data:
postgres-data:


secrets:
db-password:
postgres_password:
file: db/pass.txt

0 comments on commit b3d8f75

Please sign in to comment.