Skip to content

Commit

Permalink
Implemented multi-stage docker build for client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
shaikahmadnawaz committed Nov 8, 2023
1 parent 548f13e commit ba9f0ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
24 changes: 15 additions & 9 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
# Layer 1: Telling Docker to use the node:14-alpine image as the base image for the container.
FROM node:14-alpine
# Stage 1

FROM node:14-alpine as builder

# Layer 2: Telling Docker to create a directory called `app` in the container and set it as the working directory.
WORKDIR /app

# Layer 3: Copying the package.json file from the root of the project to the `app` directory in the container.
COPY package.json .

# Layer 4: Installing the dependencies listed in the package.json file.
RUN npm install

# Layer 5: Copying all the files from the root of the project to the `app` directory in the container.
COPY . .

# Layer 6: Telling Docker that the container will listen on port 3000.
# Stage 2

FROM node:14-alpine as runner

WORKDIR /app

COPY --from=builder /app .

EXPOSE 3000

# Layer 7: Telling Docker to run the `npm start` command when the container is started.
CMD ["npm", "start"]
CMD ["npm", "start"]

# To build the Docker image, run the following command from the client directory:
# docker build -t react-client .
20 changes: 5 additions & 15 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# The version of the docker-compose.
version: "3"
# Telling docker-compose to build the client and server images and run them in containers.
version: "3.8"

services:
client:
# Telling docker-compose to build the client image using the Dockerfile in the client directory.
Expand All @@ -10,19 +9,10 @@ services:
# Mapping the port 3000 on the host machine to the port 3000 on the container.
ports:
- "3000:3000"
# Mapping the client directory on the host machine to the /app directory on the container.
# volumes:
# - ./client:/app
# - ./client/node_modules:/app/node_modules
# Allows you to run container in detached mode.
stdin_open: true
tty: true

server:
# Telling docker-compose to build the client image using the Dockerfile in the server directory.
# Telling docker-compose to build the server image using the Dockerfile in the server directory.
build: ./server
container_name: booktalks-server
ports:
- "5000:5000"
# volumes:
# - ./server:/app
# - ./server/node_modules:/app/node_modules
- "5000:5000"
16 changes: 12 additions & 4 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:14
# Stage 1

FROM node:14-alpine as builder

# We use nodemon to restart the server every time there's a change
RUN npm install -g nodemon

WORKDIR /app
Expand All @@ -11,7 +12,14 @@ RUN npm install

COPY . .

# Stage 2

FROM node:14-alpine as runner

WORKDIR /app

COPY --from=builder /app .

EXPOSE 5000

# Use script specified in package,json
CMD ["npm", "run", "dev"]
CMD ["npm", "run", "dev"]

0 comments on commit ba9f0ad

Please sign in to comment.