Skip to content

Commit

Permalink
Merge pull request #12 from 9500073161/dev
Browse files Browse the repository at this point in the history
Docker:V2
  • Loading branch information
PrashobVP authored May 22, 2024
2 parents c85b36f + 8ce135f commit 11860db
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
27 changes: 12 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,30 @@ RUN go mod tidy
RUN go build -o /coursemanagement/build/myapp .

# Use a minimal base image for the final stage
FROM debian:bullseye-slim
FROM ubuntu:22.04

# Set the working directory
WORKDIR /coursemanagement

# Install MySQL
# Install MySQL client to test connectivity if needed (optional)
RUN apt-get update && \
apt-get install -y default-mysql-server && \
apt-get install -y mysql-client && \
rm -rf /var/lib/apt/lists/*


# Install MySQL server
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server && \
rm -rf /var/lib/apt/lists/*


# Set environment variables
#ENV MYSQL_ROOT_PASSWORD=1234
#ENV MYSQL_DATABASE=mydatabase
ENV DB_USER=root
ENV DB_PASSWORD=1234
#ENV DB_HOST=localhost
#ENV DB_PORT=3306
#ENV DB_NAME=mydatabase

# Copy the built application from the builder stage
COPY --from=builder /coursemanagement/build/myapp /coursemanagement/build/myapp

# Expose the port on which the application will run
EXPOSE 8080
EXPOSE 3306


# Start MySQL and run the application
CMD service mysql start && \
mysql -u root -p1234 -e "CREATE DATABASE IF NOT EXISTS coursemanagement;" && \
/coursemanagement/build/myapp
CMD /coursemanagement/build/myapp
28 changes: 28 additions & 0 deletions create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE DATABASE IF NOT EXISTS `coursemanagement` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `coursemanagement`;

CREATE TABLE IF NOT EXISTS `courses` (
`id` int NOT NULL COMMENT 'Id',
`name` varchar(255) NOT NULL COMMENT 'Name',
`teacher_id` int NOT NULL COMMENT 'TeacherID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=`utf8`;

CREATE TABLE IF NOT EXISTS `students` (
`id` int NOT NULL COMMENT 'Id',
`name` varchar(255) NOT NULL COMMENT 'Name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=`utf8`;

CREATE TABLE IF NOT EXISTS `teachers` (
`id` int NOT NULL COMMENT 'Id',
`name` varchar(255) NOT NULL COMMENT 'Name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=`utf8`;

CREATE TABLE IF NOT EXISTS `entrollments` (
`id` int NOT NULL COMMENT 'Id',
`course_id` int NOT NULL COMMENT 'Courseid',
`student_id` int NOT NULL COMMENT 'Studentid',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=`utf8`;
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'

services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- db
environment:
- DB_HOST=db
- DB_PORT=3306
- DB_USER=root
- DB_PASSWORD=1234
- DB_NAME=coursemanagement

db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: coursemanagement
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
- ./create_table.sql:/docker-entrypoint-initdb.d/create_table.sql

volumes:
mysql-data:
24 changes: 13 additions & 11 deletions mysqldbmodels/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mysqldbmodels

import (
"errors"
"fmt"
"os"

"gorm.io/driver/mysql"
Expand All @@ -14,24 +15,25 @@ type DBClient struct {
Conn *gorm.DB
}

func InitializeDatabase() (*DBClient,error) {
func InitializeDatabase() (*DBClient, error) {

dbUser := os.Getenv("DB_USER")
dbPassword := os.Getenv("DB_PASSWORD")
if dbUser == "" || dbPassword == "" {
return nil, errors.New("DB_USER or DB_PASSWORD environment variable not set")
}
dbPassword := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
if dbUser == "" || dbPassword == "" {
return nil, errors.New("DB_USER or DB_PASSWORD environment variable not set")
}


// Connect to MySQL database
dsn := dbUser + ":" + dbPassword + "@tcp(host.docker.internal:3306)/coursemanagement?charset=utf8mb4&parseTime=True&loc=Local"
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPassword, dbHost, dbPort, dbName)
var err error
DB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return nil, err
}
client := &DBClient{Conn: DB}
return client,err
return client, err


}

0 comments on commit 11860db

Please sign in to comment.