Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerized the project #3

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Dockerfile
.dockerignore
.gitignore
.git
README.md
frontend.dockerfile

build
node_modules
13 changes: 13 additions & 0 deletions client/client.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:alpine3.19

WORKDIR /app

COPY client/package*.json ./

RUN npm install --ignore-platform

COPY client/ .

EXPOSE 5173

CMD [ "npm" , "run" , "dev" ]
66 changes: 33 additions & 33 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "client",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@chakra-ui/react": "^2.10.3",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@tanstack/react-query": "^5.59.15",
"framer-motion": "^11.11.9",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0"
},
"devDependencies": {
"@eslint/js": "^9.11.1",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
"eslint": "^9.11.1",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.12",
"globals": "^15.9.0",
"typescript": "^5.5.3",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8"
}
"name": "client",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@chakra-ui/react": "^2.10.3",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@tanstack/react-query": "^5.59.15",
"framer-motion": "^11.11.9",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0"
},
"devDependencies": {
"@eslint/js": "^9.11.1",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
"eslint": "^9.11.1",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.12",
"globals": "^15.9.0",
"typescript": "^5.5.3",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8"
}
}
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
backend:
container_name: go-server
build:
context: .
dockerfile: server/server.dockerfile
environment:
MONGODB_URI: <db_connection_string>
PORT: "5000"
ENV: production
ports:
- "5000:5000"

healthcheck:
interval: 2s
test: "exit 0"

frontend:
container_name: react-frontend
build:
context: .
dockerfile: client/client.dockerfile

ports:
- "5173:5173"
depends_on:
backend:
condition: service_healthy
3 changes: 3 additions & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tmp
backend.dockerfile
.dockerignore
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions main.go → server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/gofiber/fiber/v2/middleware/cors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -27,10 +27,10 @@ func main() {

if os.Getenv("ENV") != "production" {
// Load the .env file if not in production
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file:", err)
}
// err := godotenv.Load(".env")
// if err != nil {
// log.Fatal("Error loading .env file:", err)
// }
}

MONGODB_URI := os.Getenv("MONGODB_URI")
Expand All @@ -54,10 +54,10 @@ func main() {

app := fiber.New()

// app.Use(cors.New(cors.Config{
// AllowOrigins: "http://localhost:5173",
// AllowHeaders: "Origin,Content-Type,Accept",
// }))
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:5173",
AllowHeaders: "Origin,Content-Type,Accept",
}))

app.Get("/api/todos", getTodos)
app.Post("/api/todos", createTodo)
Expand All @@ -70,7 +70,7 @@ func main() {
}

if os.Getenv("ENV") == "production" {
app.Static("/", "./client/dist")
app.Static("/", "./dist")
}

log.Fatal(app.Listen("0.0.0.0:" + port))
Expand Down
21 changes: 21 additions & 0 deletions server/server.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# use official Golang image
FROM golang:alpine3.20

# set working directory
WORKDIR /app
RUN mkdir dist

# Copy the source code
COPY server/ .
COPY client/dist ./dist
# Download and install the dependencies
RUN go mod tidy

# Build the Go app
RUN go build -o todoApp .

#EXPOSE the port
EXPOSE 5000

# Run the executable
CMD ["./todoApp"]
Loading