Skip to content

Commit 7787924

Browse files
committed
updated dockerfile
1 parent 4b98e05 commit 7787924

File tree

4 files changed

+196
-22
lines changed

4 files changed

+196
-22
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Build and Create Docker Images
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
# Use docker.io for Docker Hub if empty
10+
REGISTRY: ghcr.io
11+
# github.repository as <account>/<repo>
12+
IMAGE_NAME: ${{ github.repository }}
13+
14+
jobs:
15+
build-and-deploy:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
22+
- name: Setup docker
23+
uses: docker/setup-buildx-action@v2.7.0
24+
25+
- name: Login to Docker Hub
26+
uses: docker/login-action@v2
27+
with:
28+
registry: ${{ env.REGISTRY }}
29+
username: ${{ github.actor }}
30+
password: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Build and push backend Docker image
33+
uses: docker/build-push-action@v2
34+
with:
35+
context: ./backend
36+
file: ./backend/Dockerfile
37+
push: ${{ github.event_name != 'pull_request' }}
38+
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-backend:latest
39+
platforms: linux/amd64,linux/arm64,linux/arm/v8
40+
41+
- name: Build and push frontend Docker image
42+
uses: docker/build-push-action@v2
43+
with:
44+
context: ./frontend
45+
file: ./frontend/Dockerfile
46+
push: ${{ github.event_name != 'pull_request' }}
47+
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend:latest
48+
platforms: linux/amd64,linux/arm64,linux/arm/v8
49+
50+
# Install the cosign tool except on PR
51+
# https://github.com/sigstore/cosign-installer
52+
# - name: Install cosign
53+
# if: github.event_name != 'pull_request'
54+
# uses: sigstore/cosign-installer@v3.5.0
55+
# - name: Sign the published Docker image
56+
# if: ${{ github.event_name != 'pull_request' }}
57+
# env:
58+
# COSIGN_EXPERIMENTAL: "true"
59+
# # This step uses the identity token to provision an ephemeral certificate
60+
# # against the sigstore community Fulcio instance.
61+
# run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest

README.md

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,36 @@ Create a `.env` file in the backend directory and add the following environment
5353
PORT=5001
5454
```
5555

56-
4. **Start MongoDB**:
57-
Ensure MongoDB is running. You can start MongoDB using the following command:
56+
## How to Start MongoDB
5857

59-
```bash
60-
mongod --dbpath ~/mongodb/data/db
61-
```
58+
To start MongoDB, ensure you have MongoDB installed on your machine. You can start MongoDB using the following command:
59+
60+
```bash
61+
mongod --dbpath ~/mongodb/data/db
62+
```
63+
64+
If you encounter any issues with the data directory, create the directory and set the appropriate permissions:
6265

63-
5. **Start the Backend Server**:
66+
```bash
67+
mkdir -p ~/mongodb/data/db
68+
sudo chown -R `id -un` ~/mongodb/data/db
69+
```
6470

71+
4. **Start the Backend Server**:
6572
Navigate to the backend directory and start the server.
6673
```bash
6774
cd backend
6875
npm start
6976
```
7077

71-
6. **Start the Frontend Application**:
78+
5. **Start the Frontend Application**:
7279

7380
Navigate to the frontend directory and start the React application.
7481
```bash
7582
cd frontend
7683
npm start
7784
```
7885

79-
## How to Start MongoDB
80-
81-
To start MongoDB, ensure you have MongoDB installed on your machine. You can start MongoDB using the following command:
82-
83-
```bash
84-
mongod --dbpath ~/mongodb/data/db
85-
```
86-
87-
If you encounter any issues with the data directory, create the directory and set the appropriate permissions:
88-
89-
```bash
90-
mkdir -p ~/mongodb/data/db
91-
sudo chown -R `id -un` ~/mongodb/data/db
92-
```
93-
9486
## API Endpoints
9587
The backend server provides the following API endpoints:
9688

@@ -132,3 +124,72 @@ The backend server provides the following API endpoints:
132124
3. A list of all employees will be displayed. Verify the details of the newly added employee in the list.
133125

134126
By following these steps, you can successfully run the Talent-Sphere application, add new employees, and verify their details.
127+
128+
## How to Build and Run Docker Images
129+
130+
### Prerequisites
131+
132+
- Docker installed on your machine.
133+
134+
### Steps to Build and Run Docker Images
135+
136+
1. **Set Up Environment Variables**:
137+
138+
Ensure you have the necessary environment variables set up in the `.env` files for both the frontend and backend.
139+
140+
#### Frontend `.env` File
141+
142+
Create a `.env` file in the frontend directory with the following content:
143+
144+
```env
145+
PORT=3000
146+
REACT_APP_API_URL=http://localhost:5001
147+
```
148+
149+
#### Backend `.env` File
150+
151+
Create a `.env` file in the backend directory with the following content:
152+
153+
```env
154+
MONGODB_URI=mongodb://localhost:27017/talent-sphere
155+
PORT=5001
156+
REACT_APP_API_URL=http://localhost:5001
157+
```
158+
159+
2. **Build and Run Backend Docker Image**:
160+
161+
Navigate to the backend directory and build the Docker image:
162+
163+
```bash
164+
cd backend
165+
docker build -t backend-image .
166+
```
167+
168+
Run the Docker container:
169+
170+
```bash
171+
docker run -p 5001:5001 backend-image
172+
```
173+
174+
3. **Build and Run Frontend Docker Image**:
175+
176+
Navigate to the frontend directory and build the Docker image:
177+
178+
```bash
179+
cd frontend
180+
docker build -t frontend-image .
181+
```
182+
183+
Run the Docker container:
184+
185+
```bash
186+
docker run -p 3000:3000 frontend-image
187+
```
188+
189+
By following these steps, you can build and run Docker images for both the frontend and backend of the Talent-Sphere application, running on ports 3000 and 5001 respectively.
190+
191+
### Summary:
192+
193+
- Added sections to the README to explain how to build and run Docker images for the frontend and backend.
194+
- Mentioned updating the `.env` files before running the images.
195+
- Provided detailed steps for building and running the Docker images.

backend/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Node.js image as the base image
2+
FROM node:20-alpine
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json
8+
COPY package*.json ./
9+
10+
# Install dependencies
11+
RUN npm install
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Expose the port the app runs on
17+
EXPOSE 5001
18+
19+
# Start the application
20+
CMD ["npm", "start"]

frontend/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Stage 1: Build the React app
2+
FROM node:20-alpine AS build
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json
8+
COPY package*.json ./
9+
10+
# Install dependencies
11+
RUN npm install
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Build the React app
17+
RUN npm run build
18+
19+
# Stage 2: Serve the React app using a lightweight web server
20+
FROM node:20-alpine
21+
22+
# Install serve to serve the build directory
23+
RUN npm install -g serve
24+
25+
# Copy the build files from the previous stage
26+
COPY --from=build /app/build /app/build
27+
28+
# Expose the port the app runs on
29+
EXPOSE 3000
30+
31+
# Start the application
32+
CMD ["serve", "-s", "/app/build"]

0 commit comments

Comments
 (0)