Skip to content

Commit

Permalink
feat: add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghanzhilian committed Jan 3, 2024
1 parent a05156a commit 81751a7
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
package-lock.json
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BASE_URL=http://localhost:3000
MONGODB_URL=mongodb://db:27017/choiceshop
ACCESS_TOKEN_SECRET=h1n0U6LHJtCZuWitwjn3oLd5qCRIgUFtemnjTrpfZLzVZ3ff0f
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 加载基础镜像
FROM node:18-alpine

# 创建工作目录
RUN rm -rf /app
RUN mkdir /app
WORKDIR /app

# 设置环境变量
ENV MONGODB_URL "mongodb://db:27017/choiceshop"

# 安装项目依赖
COPY . /app
RUN npm install --registry https://registry.npm.taobao.org
RUN npm run build

# 对外暴露端口
EXPOSE 3000

# 启动 Image 时执行命令
CMD npm run start
8 changes: 4 additions & 4 deletions app/(main)/(client-layout)/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export default async function Home({ searchParams }) {
<DiscountSlider currentCategory={currentCategory} />
<Categories
childCategories={{ categories: childCategories, title: '分类' }}
color={currentCategory.colors?.start}
name={currentCategory.name}
color={currentCategory?.colors?.start}
name={currentCategory?.name}
homePage
/>
<BannerOne data={bannerOneType} />
<BestSellsSlider categorySlug={currentCategory.slug} />
<BestSellsSlider categorySlug={currentCategory?.slug} />
<BannerTwo data={bannerTwoType} />
<MostFavouraiteProducts categorySlug={currentCategory.slug} />
<MostFavouraiteProducts categorySlug={currentCategory?.slug} />
</div>
</main>
)
Expand Down
2 changes: 1 addition & 1 deletion components/AddressBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const BasicAddressBar = ({ addressModalProps }) => {
<Icons.Location2 className="icon" />
<span>请选择您所在的城市</span>

<Icons.ArrowLeft className="mr-auto icon" />
<Icons.ArrowRight2 className="mr-auto icon" />
</button>
)
} else if (isAddress) {
Expand Down
2 changes: 1 addition & 1 deletion components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { default as DashboardLayout } from './Layouts/DashboardLayout'
export { default as Logo } from './svgs/logo.svg'
export { default as OrderEmpty } from './svgs/order-empty.svg'
export { default as EmptySearch } from './svgs/empty-search.svg'
export { default as LogoH } from './svgs/Logo-h.svg'
export { default as LogoH } from './svgs/logo-h.svg'
export { default as LogoChinese } from './svgs/logoChinese.svg'
export { default as FreeShippingSvg } from './svgs/freeShipping.svg'
export { default as Person } from './svgs/person.svg'
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3"
services:
api:
build:
context: .
dockerfile: Dockerfile
restart: always
volumes:
- .:/app
ports:
- "3000:3000"
environment:
PORT: 3000
depends_on:
- db
db:
image: 'mongo:4.4.13'
ports:
- "27017:27017"
38 changes: 13 additions & 25 deletions helpers/db.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import mongoose from 'mongoose'

mongoose.Promise = global.Promise
const connection = {}
import mongoose, { connection } from 'mongoose'

async function connect() {
if (connection.isConnected) {
console.log('already connected')
return
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState
if (connection.isConnected === 1) {
console.log('use previous connection')
return
}
// await mongoose.disconnect()
}

try {
await mongoose.set('strictQuery', false)
const db = await mongoose.connect(process.env.MONGODB_URL)
console.log('new connection')
connection.isConnected = db.connections[0].readyState
} catch (error) {
console.log(error)
process.exit(1)
if (!connection.readyState) {
await mongoose
.connect(process.env.MONGODB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(c => {
console.log('Mongo Connected: ')
})
.catch(error => {
console.log('error: ', error)
})
}
}

Expand Down
5 changes: 0 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
env: {
BASE_URL: 'http://localhost:3000',
MONGODB_URL: 'mongodb://localhost:27017/exercise-full-next',
ACCESS_TOKEN_SECRET: 'h1n0U6LHJtCZuWitwjn3oLd5qCRIgUFtemnjTrpfZLzVZ3ff0f',
},
images: {
remotePatterns: [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"js-cookie": "^3.0.1",
"jsonwebtoken": "^8.5.1",
"moment-jalaali": "^0.10.0",
"mongoose": "^6.3.5",
"mongoose": "^7.2.3",
"next": "14.0.3",
"nprogress": "^0.2.0",
"react": "^18",
Expand Down

0 comments on commit 81751a7

Please sign in to comment.