From a2847bd1e16fa559287be81e956be155c3dd90b4 Mon Sep 17 00:00:00 2001 From: Jubayer Date: Mon, 21 Oct 2024 18:32:58 +0600 Subject: [PATCH] optimize dockerfile and compose Signed-off-by: Jubayer --- Dockerfile | 14 +++++--------- docker-compose.yml | 15 ++++----------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 213beab..96d9dec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Use the official Node.js image. -FROM node:18 +FROM node:18-slim # Set the working directory. WORKDIR /var/www/ims-nest-api-starter @@ -9,19 +9,15 @@ ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.s RUN chmod +x /usr/local/bin/wait-for-it # Copy package.json and package-lock.json to the working directory. -COPY package.json package-lock.json ./ - -# Copy all project files to the working directory, excluding those in .dockerignore. -COPY . . - -# Copy the Docker-specific environment file. -COPY .env.docker .env +COPY package*.json ./ # Install app dependencies. -RUN npm cache clean --force && rm -rf node_modules && npm install && npm rebuild bcrypt +RUN npm install # Expose the application port. EXPOSE 8000 +COPY . . + # Set the entrypoint command to wait for the database and then start the application CMD wait-for-it db:5432 -- npm run start:dev --preserveWatchOutput \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index e6bafa9..d72d1de 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,23 +1,17 @@ services: app: - image: ims-nest-api-starter container_name: ims-nest-api-starter build: context: . dockerfile: Dockerfile + env_file: .env.docker ports: - "8000:8000" volumes: - - .:/var/www/ims-nest-api-starter - - node_modules:/var/www/ims-nest-api-starter/node_modules + - ./src:/var/www/ims-nest-api-starter/src + - ./test:/var/www/ims-nest-api-starter/test depends_on: - db - environment: - - DB_HOST=db - - DB_PORT=5432 - - DB_NAME=ims_nest - - DB_USERNAME=postgres - - DB_PASSWORD=postgres restart: unless-stopped db: @@ -33,5 +27,4 @@ services: - "5432:5432" volumes: - postgres_data: - node_modules: \ No newline at end of file + postgres_data: \ No newline at end of file