From a70c80a5573629f396b1047ea82bdfe3837e7943 Mon Sep 17 00:00:00 2001 From: Ben Melchior Date: Tue, 15 Apr 2025 20:26:06 +0200 Subject: [PATCH] Add Docker support with Dockerfile and docker-compose.yml - Introduced a Dockerfile for building the application using a multi-stage build process. - Added docker-compose.yml to define services for the application and PostgreSQL database. - Updated README.md with instructions for running the application using Docker and Docker Compose. --- Dockerfile | 33 +++++++++++++++++++++++++++++++++ README.md | 28 ++++++++++++++++++++++++++++ docker-compose.yml | 31 +++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d4bcd27 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM rust:1.76-slim-bullseye as builder + +WORKDIR /usr/src/app +COPY . . + +# Build the application +RUN cargo build --release + +# Create a new stage with a minimal image +FROM debian:bullseye-slim + +WORKDIR /usr/local/bin + +# Install necessary runtime dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Copy the built binary from the builder stage +COPY --from=builder /usr/src/app/target/release/hospitalapi . + +# Create a directory for the .env file +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Copy the .env file +COPY .env . + +# Expose the port the app runs on +EXPOSE 3000 + +# Run the application +CMD ["./hospitalapi"] \ No newline at end of file diff --git a/README.md b/README.md index 6d457f0..38a2b9f 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,34 @@ All system events are logged with timestamps in the format: [YYYY-MM-DD HH:MM:SS] Message ``` +## Docker Setup + +### Prerequisites + +- Docker +- Docker Compose + +### Running with Docker + +1. Make sure you have a `.env` file in the project root with the necessary configuration. + +2. Build and start the containers: + ```bash + docker-compose up -d + ``` + +3. To view logs: + ```bash + docker-compose logs -f + ``` + +4. To stop the application: + ```bash + docker-compose down + ``` + +The application will be available at http://localhost:3000. + ## License This project is licensed under the GNU General Public License v3.0 (GPL-3.0). This license ensures that: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..72d1746 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: '3.8' + +services: + app: + build: . + ports: + - "3000:3000" + depends_on: + - db + environment: + - DATABASE_URL=postgres://postgres:postgres@db/hospitaldb + - HOST=0.0.0.0 + - PORT=3000 + volumes: + - ./.env:/usr/src/app/.env + restart: unless-stopped + + db: + image: postgres:14 + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=hospitaldb + volumes: + - postgres_data:/var/lib/postgresql/data + ports: + - "5432:5432" + restart: unless-stopped + +volumes: + postgres_data: \ No newline at end of file